How to Upload a image from HTML form using PHP?

Tuesday, April 15th, 2014 Programming

[php]
<?php

if(isset($_REQUEST[‘sub’]))
{
$fileType = $_FILES[‘img’][‘type’];
$fileSize = $_FILES[‘img’][‘size’];
if($fileSize/1024 > ‘2048’)
{
echo ‘Filesize is not correct it should be equal to 2 MB or less than 2 MB.’;
exit();
} //FileSize Checking
if($fileType != ‘image/png’ && $fileType != ‘image/gif’ && $fileType != ‘image/jpg’ && $fileType != ‘image/jpeg’ )
{
echo ‘Sorry this file type is not supported we accept only. Jpeg, Gif, PNG’;
exit();
} //file type checking ends here.
$upFile = ‘uploads/’.date(‘Y_m_d_H_i_s’).$_FILES[‘img’][‘name’];
if(is_uploaded_file($_FILES[‘img’][‘tmp_name’])) {
if(!move_uploaded_file($_FILES[‘img’][‘tmp_name’], $upFile)) {
echo ‘Problem could not move file to destination.’;
exit;
}
} else {
echo ‘Problem: Possible file upload attack. Filename: ‘;
echo $_FILES[‘img’][‘name’];
exit;
}
$img= $upFile;
} //File upload ends here.

}
?>
<html>
<body>
<h3>File Upload:</h3>
Select a file to upload:
<form action="" enctype="multipart/form-data" method="post">
<input type="file" name="img" size="50" />
<input type="submit" name="sub" value="Upload File" />
</form>
</body>
</html>
[/php]

File Upload:

Select a file to upload:

No comments yet.

Leave a comment

Request a Free SEO Quote