How to upload files using PHP?

Using PHP, we can upload files from our browser to our hosting. This is the script for that.

First of all, we need to create an HTML form that allows people to choose 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 is what we will be creating next to actually upload the file.

 

The actual file upload 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 upload files sent to it by your HTML form.

 

Where, 

$target = "upload/"; is where we assign the folder that files will be uploaded to.

 

Important: Be sure you remember to create this folder!


Was this answer helpful? 0 Users Found This Useful (0 Votes)