PeterErhard
asked on
Validate Upload Script
I'll be allowing users to upload certain files on my website and have put together the below script.
Can anyone see any problems with it? Any suggested improvements?
<?php
$FileUploadSuccess = 0;
$uploaddir = 'files/';
$uploadfile = $uploaddir . $_FILES['img1']['name'];
$filetypes = array("image/jpg","image/g if","image /jpeg","im age/pjpeg" );
$imageinfo = getimagesize($_FILES['img1 ']['tmp_na me']);
//check file size is less than 2MB (below is bytes)
if ($_FILES['img1']['size'] <= 2097152)
{
// upload the file only if the file type is of one listed above.
if(in_array(strtolower($_F ILES['img1 ']['type'] ),$filetyp es))
{
// rename the file.
$path_parts = pathinfo($uploadfile);
$uploadfile = $uploaddir.(substr($_FILES ['img1'][' name'],0,( strlen($_F ILES['img1 ']['name'] )-(strlen( $path_part s['extensi on'])+1))) .date("Ymd His").".". $path_part s['extensi on']);
if (move_uploaded_file($_FILE S['img1'][ 'tmp_name' ], $uploadfile))
{
$FileUploadSuccess = 1;
}
}
}
if ($FileUploadSuccess == 0)
{
echo "File Upload Failed for some reason - display error message with likely reasons";
}
else
{
echo "File Upload Successful - display happy message";
}
?>
Can anyone see any problems with it? Any suggested improvements?
<?php
$FileUploadSuccess = 0;
$uploaddir = 'files/';
$uploadfile = $uploaddir . $_FILES['img1']['name'];
$filetypes = array("image/jpg","image/g
$imageinfo = getimagesize($_FILES['img1
//check file size is less than 2MB (below is bytes)
if ($_FILES['img1']['size'] <= 2097152)
{
// upload the file only if the file type is of one listed above.
if(in_array(strtolower($_F
{
// rename the file.
$path_parts = pathinfo($uploadfile);
$uploadfile = $uploaddir.(substr($_FILES
if (move_uploaded_file($_FILE
{
$FileUploadSuccess = 1;
}
}
}
if ($FileUploadSuccess == 0)
{
echo "File Upload Failed for some reason - display error message with likely reasons";
}
else
{
echo "File Upload Successful - display happy message";
}
?>
code looks fine
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys.
A question regarding $filetypes = array('image/jpg','image/g if','image /jpeg','im age/pjpeg' );
If I wanted to allow doc, pdf as well how would it look within the array there?
A question regarding $filetypes = array('image/jpg','image/g
If I wanted to allow doc, pdf as well how would it look within the array there?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh, and ms word docs are 'application/msword'
ASKER
Thanks :)