In: PHP
2 Oct 2009In this tutorial you will learn to upload image file using PHP. Together you will learn to restrict file type. In this tutorial I am showing you example in which you can upload only jpg/jpeg images. It a very easy method I am going to show you and hope you will understand.
Start With Form :
<form action="" method="post" enctype="multipart/form-data" name="form1">
<p>
<input name="fileImage" type="file" id="fileImage">
</p>
<p>
<input name="save-image" type="submit" id="save-image" value="Upload Image">
</p>
</form>
Upload Code:
<?php
if(isset($_POST['save-image'])){
//Line Use to check if the buttton was pressed.
$fileName=$_FILES['fileImage']['name'];
//Getting file name and assigning it to $fileName variable
$fileType=$_FILES['fileImage']['type'];
//Getting file type and assigning it to $filetype variable
$fileSize=$_FILES['fileImage']['size'];
//Getting file size and assigning it to $filesize;
$tmpName=$_FILES['fileImage']['tmp_name'];
//Getting temporary name of file and assigning it to $tmpName
$maxsize=1048576;
//Defining maximum file size to 1Mb
$uploadDir=”upload/”;
//Define upload directory
$filePath=$uploadDir.$fileName;
//Defining file path so that we can use it on move_uploaded_file function
if(($fileType==”image/jpg”) || ($fileType==”image/jpeg”) || ($fileType==”image/pjpeg”)){
//Checking file type. Is the file jpg or not?
$result=move_uploaded_file($tmpName, $filePath);
//When file is jpg moving file to the upload directory from temp directory
}
if($result){
//If file move successfully then show message
echo “File Uploaded Successfully”;
}else{
//If file is invalid display message
echo “Invalid File Format”;
}
}
?>
Full Code:
<?php
if(isset($_POST['save-image'])){
$fileName=$_FILES['fileImage']['name'];
$fileType=$_FILES['fileImage']['type'];
$fileSize=$_FILES['fileImage']['size'];
$tmpName=$_FILES['fileImage']['tmp_name'];
$maxsize=1048576;
$uploadDir=”upload/”;
$filePath=$uploadDir.$fileName;
if(($fileType==”image/jpg”) || ($fileType==”image/jpeg”) || ($fileType==”image/pjpeg”)){
$result=move_uploaded_file($tmpName, $filePath);
}
if($result){
echo “File Uploaded Successfully”;
}else{
echo “Invalid File Format”;
}
}
?>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Upload Image</title>
</head>
<body>
<form action=”" method=”post” enctype=”multipart/form-data” name=”form1″>
<p>
<input name=”fileImage” type=”file” id=”fileImage”>
</p>
<p>
<input name=”save-image” type=”submit” id=”save-image” value=”Upload Image”>
</p>
</form>
</body>
</html>

5 Responses to PHP Image File Upload Tutorial
golYterW
October 2nd, 2009 at 11:00 pm
Актуальная тема. Конечно, хочется еще дополнений. буду ждать…
Kolyewe
October 4th, 2009 at 4:16 pm
Спасиб! вообще отлично, 2 хороших новости - ваш сайт нашел и лето началось Можно сказать жизнь удалась, можно и на покой
Zamshed Farhan
January 13th, 2010 at 10:38 pm
Hello Dear.
It’s a must appreciable attempt to write such an
article. It helps us to build a nice one.
IRAKLIJ20KOMAROV
April 20th, 2010 at 7:47 am
Прокат и аренда автомобиля в том числе с водителем
php answers
May 21st, 2010 at 7:36 am
Very well put together, thank you for sharing. I’m sure this will help somebody no end