Upload Files From a Form

Hello guys,ξ I decided to do a post on how to upload files from a simple HTML form into your server. First, you will need to set up this form with the proper headings, please see code below.

<form enctype=”multipart/form-data” action=”uploads.php” method=”POST”>

Choose a file to upload: <input name=”uploadedfile” type=”file” /><br />

<input type=”submit” value=”Upload File” /> (Max Size is 2MB)

</form>

As you can see, there is a note stating that the maximum size can be of 2MB’s, you can change this in your php.ini file. Now, you will need to create your uploads.php file, which will receive the file and move it to your desired destination.

//File Upload

$target_path = “uploads/”; //Target Folder

$target_path = $target_path .$timestamp.’_’.basename( $_FILES[‘uploadedfile’][‘name’]);ξ //Final File Destination and name

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!”; //If an error

}

As you can see, I have commented on the code so that it’s more clear to you, feel free to change the address to your desired location. And like always, please feel free to leave any comments, sugestions or questions below.

Thank You,

Gilberto Cortez

error

Enjoy this blog? Please spread the word :)