Skip to content

Instantly share code, notes, and snippets.

@taterbase
Created May 13, 2012 15:03
Show Gist options
  • Save taterbase/2688850 to your computer and use it in GitHub Desktop.
Save taterbase/2688850 to your computer and use it in GitHub Desktop.
Simple file upload in php
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
@FAlbanni
Copy link

This sucks anyone would be able to upload a .php file and take control of your server, do NOT use it!

@AllenJB
Copy link

AllenJB commented Oct 9, 2021

This is a terrible example of handling file uploads.

It does not check for file upload errors (via the 'errors' element under $_FILES).

The 'name' is specified by the client and should not be trusted. It may also contain characters that are not valid for filenames on the servers filesystem.

There's no handling of duplicate filenames - one file upload could overwrite a previous file upload.

This code does not check the content of the uploaded file. You may be expecting an image to be uploaded, but the client may upload a PHP script instead - if that file is uploaded to a web accessible directory, the client could then execute that PHP script. This would lead to further compromises of your server and/or your hosting being used for malicious purposes (phishing, illegal content).

You should always check the content of uploaded files using the fileinfo extension, mime_content_type(), or a function specific to the expected content type (eg. the type returned by getimagesize() for images)

@Noemi4
Copy link

Noemi4 commented Jan 1, 2022

Thank you very much, this one finally works!

@Noemi4
Copy link

Noemi4 commented Jan 1, 2022

For the ones complaining, the point of this script is that beginners can understand the base code for uploading files, and can add validation afterwards.

@TinySonhh
Copy link

Thank you,

@BawdyAnarchist
Copy link

Late to the party, but THANK YOU for putting a dead simple minimalist version of an upload. The only other guides I saw had all this extra crap that caused errors, required multiple files, etc.

This file just works. You might need to change your download folder, but it's simple and solid.

@sayantanHack
Copy link

Steps to Reproduce

For windows

  1. Run apache server or using Xampp run apache.
  2. Create an upload folder in C:\xampp\htdocs
  3. Also put this upload.php on the same folder C:\xampp\htdocs

For *Nix

  1. Create an upload folder in /var/www/html/
  2. Put the upload.php file in the same folder /var/www/html/
  3. Run Apache service.

@sensboston
Copy link

If someone needs similar (i.e. very simple) but a way more advanced and useful solution, please be my guest: https://github.com/sensboston/uploader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment