Articles: Handling file uploads

In this article we'll describe how to allow a visitor to upload a file, and then check that the file is filetype we accept.
Added: 2003-07-15 21:14:14 - Modified: 2005-12-27 19:23:18 - Level: Beginner
Printer friendly version PDF File
Recommend this article to a friend.

Bookmark this on google Bookmark this on del.icio.us Submit this to digg Bookmark this at yahoo Bookmark this at reddit Bookmark this at furl Search technorati for links to this page Toggle more
1 2

<< Previous page |

<?
header('Content-type: text/plain');
$prefix = "myprefix_";
foreach($_FILES as $value)
{
$size = getimagesize($value['tmp_name']);
echo $value['name'] ." is now known as: ". $value['tmp_name']." - Size: ".$value['size']." - width: ".$size[0]." height: ".$size[1]."\n";
move_uploaded_file($value['tmp_name'], "/path/to/our/upload/dir/".$prefix.$value['name']);
}
?>

To make it simple we set the content type to text/plain, then our output will be simple text. The code snippet provided was especially ment for uploading images, and therefore it will print the size of the file, the width and the height of the image. then it will move the image from the temporary location to a provided path. If you want to use this for other file types then images, you can remove the 2 first lines of the foreach, those dealing with getimagesize().

If you want more information check out http://php.net/manual/en/features.file-upload.php

And that should be it, happy coding.

1 2

<< Previous page |

Related articles:



[Sitemap]