PHP file upload script

Firstly, to let visitors upload a file, you must create a form so enable users to choose a file:
In your html page, copy this:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>

This will enable the user to choose what file to upload and click "Upload File" button.
Choose a file to upload:



If you want to add a file size limit, add this line:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
Now create a file "upload.php" and a directory "uploads/"

In upload.php copy this php code:
$target_path = "uploads/" ;
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
This code will put all uploaded files to uploads/ directory.
$_FILES['uploadedfile']['name'] will get the file name, and add it to the string $target_path.
For example if someone upload a file: myfile.zip , it will save it to: uploads/myfile.zip .

To check if the file was uploaded successfully, add this script:
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

7 comments:

Anonymous said...

sir, pwede bang actual nlang? ung file na mismo, ikaw lang ang nakakaintindi nyan.

Anonymous said...

sir, wala po ba itong kasamang database?

ILyes said...

Please speak english so I can help you.
Thanks.

Cheap Diablo 3 Gold said...

thanks a lot for discussing. i was looking for as well. diablo 3 Gold



Runescape Gold

wei liu said...

This is a great article, supplies the useful information for me,best android phones thanks in this Interested can try.

oliver said...

This script looks outdated. It is using lot of deprecated functions. This php file upload script here is up to date: https://www.cloudways.com/blog/the-basics-of-file-upload-in-php/

Garrick Co Ida said...

Great & Useful Blog. Helped me a lot.
PHP Training in Chennai
PHP Online Training India