ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Simple File or Image Uploads Using PHP HTML

Updated on January 17, 2014

Simple File and Image Uploads

Today we are going to learn how to upload a file or image using PHP and a web form. No knowledge of PHP is needed or HTML as the full file structure is included:

Build a simple HTML form:

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="uploadedfile" id="uploadedfile" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>

Once again nothing fancy, but you will be able to add to the form, so that you can upload the file and email other form contents or store the other form contents in a database.

Page Load:

Once the page loads the form shown above will be rendered, after the Submit button is clicked, here is what happens:

}else{
$path = "uploads/";

$path = $path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
?>
<div style="text-align:center;width:350px;border:1px solid #cccccc;background-color:#f1f1f1;">
"Thanks for your uploaded file!"
</div>
<?php
} else{
echo "There was an error uploading the file, please try again!";
}


}//end if


  • $path = "uploads/"; All we did here is establish a place to put the file
  • move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path), we did what the function reads, we moved the file to the $path, using the temp name (same name)
  • <div style="text-align:center;width:350px;border:1px solid #cccccc;background-color:#f1f1f1;">
    "Thanks for your uploaded file!"
    </div> using a little css we styled a box that give the user some feedback on the a positive upload

Next, you give some feedback just in case the upload fails, using a conditional statement.

"Thanks for your uploaded file!"

Code in full:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php if(isset($_POST['Submit'])){ echo 'The file "'. basename( $_FILES['uploadedfile']['name']). '" has been uploaded!';}else{?>Please Choose a File<?php } ?></title>
</head>

<body>
<?php if(!isset($_POST['Submit'])){?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="uploadedfile" id="uploadedfile" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
}else{
$path = "uploads/";

$path = $path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
?>
<div style="text-align:center;width:350px;border:1px solid #cccccc;background-color:#f1f1f1;">
"Thanks for your uploaded file!"
</div>
<?php
} else{
echo "There was an error uploading the file, please try again!";
}

}//end if
?>
</body>
</html>

Recap:

Told ya, short and sweet! Now in PHP you can do a lot more with the validation of a file before you copy or upload it, such as size, type, etc.

That all can be found referencing the $_FILES superglobal using this book.

No working demo for this how to, but follow this to get started:

  1. just copy and paste the full code
  2. create a file folder named "uploads"
  3. chmod the "uploads" folder to 755
  4. Call the page using your browser and that's it

Using this small sample you can quickly build on more possibilities, such as insert the path in the database, emailing the file, whatever you can think of. Thank you and more to come!


File Uploads Survey

Did this help you?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      A Friend 8 years ago

      Good tutorila, but I think you misunderstood the html attribute (for) of the (label) tag is for.

      http://www.w3schools.com/tags/tag_label.asp

    • Alpho011 profile image
      Author

      Alpho011 8 years ago from Marietta, Georgia

      Thanks that give me incentive to brush up on my HTML, really appreciate it.

    • wei654231 profile image

      wei654231 8 years ago

      Simple File or Image Uploads Using PHP HTML

      what a wonderful hub! I'd not heard of them before, fascinating.

    • profile image

      Scott Z 8 years ago

      How do you chmod it to 755?

    • Alpho011 profile image
      Author

      Alpho011 8 years ago from Marietta, Georgia

      Using your FTP client, depends on the client, but most you can right click and change the file permissions.

    • profile image

      vamshi 8 years ago

      Hi this is vamshi, in ajax how to retrieve without using $_files[file][tmp_name], first page is html page and onclick to javascript page and upload.php , in upload.php how to retrieve temp name of upload image in php.....

      Anybody plzzz help me in this.....

    • Alpho011 profile image
      Author

      Alpho011 8 years ago from Marietta, Georgia

      ok, you have to run the upload script in the background using the http object (AJAX), I am guessing you want the name before you upload right, or do you want to upload with leaving the page (refresh).

    • profile image

      grep php 8 years ago

      I prefer classic methods, ajax sucks.

    • profile image

      felipe 7 years ago

      this file didn&acute;t work for me, can somebody tell me what could happen? the 2 errors were like this:

      Warning: move_uploaded_file(uploads/HONDEX HE7300.pdf) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/ramcote/public_html/es/subir.php on line 23Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpVNOzZb' to 'uploads/HONDEX HE7300.pdf' in /home/ramcote/public_html/es/subir.php on line 23There was an error uploading the file, please try again!

    • Alpho011 profile image
      Author

      Alpho011 7 years ago from Marietta, Georgia

      felipe

      Sorry been out of town in miami, you need to set the folder permissions to 755, (chmod)

    • profile image

      Simon 7 years ago

      WOW! nice and easy! not like some of these upload files which contain files galore!!

      Damn girl you smart! ;) keep it up!

    • profile image

      Ben 7 years ago

      Please send me a code changing a background image using input text ("path") and will show the image to its corresponding input text (path)...

      tnx

    • tonyhubb profile image

      tonyhubb 7 years ago

      Good information! It is very helpful!

    • profile image

      Shrikrishna Meena 6 years ago

      Thanks for writing this article...

      Very helpful...

    • profile image

      mahesh 6 years ago

      its good

    • Afsal Rahim profile image

      Afsal Rahim V 6 years ago from India

      Nice simple code.

      What happens if someone uploads files with the same name? Does the older file get replaced by the new file?

    • Alpho011 profile image
      Author

      Alpho011 6 years ago from Marietta, Georgia

      It gets overridden

    • profile image

      Christoph 6 years ago

      Hi,

      i had to put my folder permissons tp 777. With 755 it is not working, it get this error:

      Warning: move_uploaded_file(uploads/hopper.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /www/htdocs/w005dcde/Christoph/Tests/imageupload.php on line 23

      Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phplSkUDT' to 'uploads/hopper.jpg' in /www/htdocs/w005dcde/Christoph/Tests/imageupload.php on line 23

      There was an error uploading the file, please try again!

      Can someone help me?

      greets

    • profile image

      satish 5 years ago

      its nice

    • profile image

      Ronald 4 years ago

      I love your fantastic article! Will follow your hub.

      Go here and read this one http://ownsports.wordpress.com/2012/07/10/new-targ...

    Click to Rate This Article