DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Very Simple Php File Upload

  • submit to reddit
        I think this is the minimum necessary to upload a file in php. First, the form, index.php:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />

Next, the php to accept the file, upload.php


$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print "</pre>";



Abagaw Gagaf replied on Tue, 2013/04/23 - 5:03am

another solution may be posted on stackoverflow - php simple file upload script 

Anush Anil Kumar replied on Fri, 2012/09/07 - 2:57pm

Here is a bit more advanced and safer version for beginners PHP File Upload

Snippets Manager replied on Tue, 2011/02/01 - 7:33pm

Great distilling of php file upload, thanks! Interestingly, it's even simpler to completely omit the action="upload.php" (or have an empty string), as the browser will then just reuse the existing php script/url.

Snippets Manager replied on Tue, 2007/06/05 - 10:43am

beware, if the file name (which can be set to whatever the user wants, not in the browser, but when sending the data manually) contains things like ../index.php it can overwrite things you don't want to be overridden.

Roger Douglass replied on Sat, 2007/04/21 - 11:35am

I'm using a Windows path and get a unexpected T_STRING in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\upload\upload.php on line 4