File Upload
PHP provides simple way to upload files to server.
Upload-Form
upload_design.php
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Upload:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
enctype="multipart/form-data"
creates both a binary and an ascii upload but it will generate extra
weight to carry around for every request to your programs.Restrictions on Upload
upload_file.php
<?php
$allowExts = array("gif", "jpeg", "jpg", "png");
$tempf = explode(".", $_FILES["file"]["name"]);
$ext = end($tempf);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($ext, $allowExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
Output :

Upload: Form.png Type: image/x-png Size: 4.0322265625 kB Temp file: C:\xampp\tmp\phpF4C1.tmp
Note :
In upload_file.php , function use for :explode()
:This function use to split a string by string.end()
: This function use to set the internal pointer of an array to its
last element.


