Flag This Hub

Simple File or Image Uploads Using PHP HTML

By


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.


PHP MySQL References

Build Your Own Database Driven Website Using PHP and MySQL
Amazon Price: $12.58
List Price: $39.95

File Uploads Survey

Did this help you?

  • Yes
  • No
See results without voting

Comments

A Friend 3 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 3 years ago

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

wei654231 3 years ago

Simple File or Image Uploads Using PHP HTML

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

Scott Z 3 years ago

How do you chmod it to 755?

Alpho011 3 years ago

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

vamshi 3 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 3 years 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 3 years ago

I prefer classic methods, ajax sucks.

felipe 2 years 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 2 years ago

felipe

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

Simon 2 years ago

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

Damn girl you smart! ;) keep it up!

Ben 2 years 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 2 years ago

Good information! It is very helpful!

Shrikrishna Meena 23 months ago

Thanks for writing this article...

Very helpful...

mahesh 23 months ago

its good

Afsal Rahim 21 months ago

Nice simple code.

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

Alpho011 21 months ago

It gets overridden

Christoph 16 months 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

satish 2 months ago

its nice

Submit a Comment
Members and Guests

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



    Like this Hub?
    Please wait working