1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Uploading Files with PHP


2 of 6

Uploading the File
The actual file upload is very simple:
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 else {
 echo "Sorry, there was a problem uploading your file.";
This very small piece of code will upload files sent to it by your HTML form.





  1. The first line $target = "upload/"; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file. So for example, if your file was at www.yours.com/files/upload.php then it would upload files to www.yours.com/files/upload/yourfile.gif. Be sure you remember to create this folder!
  2. We are not using $ok=1; at the moment but we will later in the tutorial.
  3. We then move the uploaded file to where it belongs using move_uploaded_file (). This places it in the directory we specified at the beginning of our script. If this fails the user is given an error message, otherwise they are told that the file has been uploaded.
Related Video
Upload Videos to Google Video
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. PHP File Upload Script - Code for Uploading Files

©2014 About.com. All rights reserved.