PHP - File upload

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 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.

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!

 

 

  • 24 Users Found This Useful
Was this answer helpful?

Related Articles

Flash tutorials to manage control panel

My SQL A guide to creating and modifying MySQL databases in cPanel. Quality: High | Low Change...

Flash tutorials to manage email

Email Accounts Manage the email accounts associated with your domain(s). Quality: High | Low...

Where can I find cPanel tutorials?

Check cPanel docs :http://www.cpanel.net/docs/cpanel/

How do I use .html files to run SSI (Server Side Includes)?

Apache Handlers This is what you would use to specify how to treat file types. By default,...

WordPress SMTP Authentication

SMTP auth support can be added in wordpress through third party plugins. You can follow these...