Simple File or Image Uploads Using PHP HTML
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:
- just copy and paste the full code
- create a file folder named "uploads"
- chmod the "uploads" folder to 755
- 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!