PHP Form File Upload
File Upload is very important part in any web application. PHP Provides very easy functions for file upload.
File Upload Example
Below example combines the HTML & PHP Code for file upload.
<!doctype html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" >
Picture<input type="file" name="fiPicture" />
<br />
<input type="submit" value="Upload" name="Upload" />
</form>
<?php
if(isset($_POST['Upload'])){
$name=$_FILES['fiPicture']['name'];
if(move_uploaded_file($_FILES['fiPicture']['tmp_name'],$name)){
echo "<p>file has been Uploaded Successfully</p>";
}else{
echo "<p>Unable to upload file please try again.</p>";
}
}
?>
</body>
</html>
Get File Attributes Example
$_FILES Array provide other attributes such as file type, file size. below example will demonstrate how to get file size, file type
<!doctype html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" >
Picture<input type="file" name="fiPicture" />
<br />
<input type="submit" value="Upload" name="Upload" />
</form>
<?php
if(isset($_POST['Upload'])){
$name=$_FILES['fiPicture']['name'];
$type=$_FILES['fiPicture']['type'];
$size=$_FILES['fiPicture']['size']; //size will come in bytes
echo "Name=".$name."<br />";
echo "Type=".$type."<br />";
echo "Size=".$size."<br />";
}
?>
</body>
</html>
Get File Extension Example
File extension comes with file name. below example will demonstrate how to fetch extension from name with the help of other functions
<!doctype html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" >
Picture<input type="file" name="fiPicture" />
<br />
<input type="submit" value="Upload" name="Upload" />
</form>
<?php
if(isset($_POST['Upload'])){
$name=$_FILES['fiPicture']['name'];
$fileParts=explode(".",$name);
echo "File Extension is=".$fileParts[1]."<br>";
}
?>
</body>
</html>
File Validation Example
Some times we require only image file or doc files with size limits. below example will demonstrate how to accomplish file validation with file type and size.
<!doctype html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" >
Picture<input type="file" name="fiPicture" />
<br />
<input type="submit" value="Upload" name="Upload" />
</form>
<?php
if(isset($_POST['Upload'])){
$name=$_FILES['fiPicture']['name'];
$type=$_FILES['fiPicture']['type'];
$size=$_FILES['fiPicture']['size'];
$fileParts=explode('.',$name); //divide the name into two parts
$ext=strtolower($fileParts[1]); //extract the extension of file and convert it to lower case
if($ext=="jpeg" || $ext=="jpg" || $ext=="png" || $ext=="gif" || $ext=="bmp"){
if($size/1024<=200){ //size limit to 200KB
if(move_uploaded_file($_FILES['fiPicture']['tmp_name'],$name)){
echo "<p>file has been Uploaded Successfully</p>";
}else{
echo "<p>Unable to upload file please try again.</p>";
}
}else{
echo "<p>Please upload less then 200KB image file</p>";
}
}else{
echo "<p>Only Image Files are allowed such as jpg, png, gif & bmp</p>";
}
}
?>
</body>
</html>