create your own

Simple File or Image Uploads Using PHP HTML

80
rate or flag this page

By Alpho011

PHP MySQL References

Build Your Own Database Driven Website Using PHP & MySQL Build Your Own Database Driven Website Using PHP & MySQL
Price: $20.00
List Price: $39.95

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 is 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 alot 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 thats it

Qucik and dirty, using this small sample you can build on more possibilties, such insert the path in the database, emailing the file, whatever you can think of, thank you and read more to come.


File Uploads Survey

Did this help you?

  • Yes
  • No
See results without voting

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

A Friend  says:
9 months 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

Alpho011  says:
9 months ago

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

wei654231 profile image

wei654231  says:
9 months ago

Simple File or Image Uploads Using PHP HTML

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

Scott Z  says:
8 months ago

How do you chmod it to 755?

Alpho011 profile image

Alpho011  says:
8 months ago

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

astray555  says:
8 months ago

Thank you very much for this hub! It's really very useful.. I appreciate it very much and will follow your advice!

vamshi  says:
8 months 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

Alpho011  says:
8 months ago

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

grep php  says:
7 months ago

I prefer classic methods, ajax sucks.

felipe  says:
7 months ago

this file didn´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

Alpho011  says:
6 months ago

felipe

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

Simon  says:
5 months ago

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

Damn girl you smart! ;) keep it up!

Ben  says:
2 months ago

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

tnx

tonyhubb profile image

tonyhubb  says:
2 months ago

Good information! It is very helpful!

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working