Step 1
This script will allow you to upload files from your browser to your hosting using PHP. The first thing we need to do is create an HTML form that allows people to select the file they want to upload.
<form enctype="multipart/form-data" action="upload.php" method="POST"> Please choose a file: <input name="uploaded" type="file" /><br /> <input type="submit" value="Upload" /> </form>
This form sends data to the file "upload.php", which we will create next to the actual file upload.
Step 2
Actual file download is very simple:
<?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; 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 download files sent to it in 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
Download files at www.yours.com/files/upload/yourfile.gif. Do not forget to create this folder! with rights 777
Step 3
if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; }
Assuming you have not changed the form field in our HTML form (so that it is still loaded by name), this will check to see the file size. If the file is larger than 350k, it is given a file error that is too large, and we set the value of $ ok to 0.
You can change this line to a larger or smaller size if you want by changing 350,000 to another number. Or, if you do not need the file size, just leave these lines
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.
Putting everyone together
<?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { 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."; } } ?>
Obviously, if you allow file downloads, you leave yourself open to people downloading a lot of unwanted things. One precaution does not allow them to upload php, html, cgi, etc. files that may contain malicious code. This provides greater security, but is not a reliable fire protection.