2 of 6
Uploading the File
The actual file upload is very simple:
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
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.
- 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!
- We are not using $ok=1; at the moment but we will later in the tutorial.
- 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.