|
Posted by Ashok on 06/30/05 03:04
Hi,
1. how do I enable multi format image upload, like user may upload jpeg,
jpg, gif files.
2. I get error 'Problem: file is not jpg image' when checking file format in
this code. Any help on why the error shows?
Code:
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
if ($userfile_error > 0)
{
echo 'Problem: ';
switch ($userfile_error)
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
// end of code for 4.2.0
// one more check: does the file have the right MIME type?
if ($userfile_type != 'image/pjpeg')
{
echo 'Problem: file is not jpg image';
exit;
}
// put the file where we'd like it
$upfile = '/uploads/'.$userfile_name;
// is_uploaded_file and move_uploaded_file added at version 4.0.3
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
exit;
}
echo 'File uploaded successfully<br /><br />';
// show what was uploaded
echo "$userfile_name<br />";
echo "<img src='$upfile'>";
echo '<br /><hr />';
Thanks,
Ashok
Navigation:
[Reply to this message]
|