I have a little upload script which works fine in IE..
but in Firefox I get this error:
The image "http://localhost/ruils/upload.php" cannot be displayed because it contains errors.
But the image gets uploaded fine
Plus if I echo this at the top of the upload.php script:
echo '<pre>';
print_r($_FILES);
echo '</pre>';
...then Firefox displays the expected info, throws no errors and uploads the image correctly.
But of course you're not meant to echo anything when setting Headers - which I do as I'm rescaling the jpeg on upload
(twice - once to a reasonable size, second to a thumb size)
the error is nothing to do with IE's filetype of image/pjpeg as that's taken into account
any ideas anyone?
here's the entire upload script:
<?php
$userfile = $FILES['userfile']['tmp_name'];
$userfile_name = $FILES['userfile']['name'];
$userfile_size = $FILES['userfile']['size'];
$userfile_type = $FILES['userfile']['type'];
$userfile_error = $FILES['userfile']['error'];
if ($userfile_error > 0)
{echo "Error: ";
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;
}
echo "<br><a href='javascript:history.back()'><img src='images/back.gif' border='0'> Back </a>";
exit;}
// does file have the right MIME type?
if ($FILES['userfile']['type']=='image/jpeg' || $FILES['userfile']['type']=='image/pjpeg')
{ $userfile_name=(str_replace('.jpg','',$userfile_name));
$userfile_name=(str_replace('.JPG','',$userfile_name));
$userfile_name=$userfile_name.'.jpg';
} else {
?>
<table border="0" cellspacing="1" cellpadding="2" width="800" align="center">
<tr>
<td class="head2"><br>Error: file is not jpeg <p></td>
</tr>
<tr>
<td class="head2"><br>File <?php echo $userfile_name; ?> is: <?php echo $FILES['userfile']['type']; ?> <p></td>
</tr>
</table>
<?php exit;
}
//scale image
$max_wd = 456;
$max_ht = 390;
$thmb_wd = 70;
$thmb_ht = 70;
$size = GetImageSize($userfile);
$wd = $size[0];
$ht = $size[1];
$x_ratio = $max_wd / $wd;
$y_ratio = $max_ht / $ht;
$thmbx_ratio = $thmb_wd / $wd;
$thmby_ratio = $thmb_ht / $ht;
// large img
if ( ($wd <= $max_wd) && ($ht <= $max_ht) ) {
$tn_wd = $wd;
$tn_ht = $ht;}else if (($x_ratio $ht) < $max_ht) {
$tn_ht = ceil($x_ratio $ht);
$tn_wd = $max_wd;}else {
$tn_wd = ceil($y_ratio $wd);
$tn_ht = $max_ht;}
//thumb img
if ( ($wd <= $thmb_wd) && ($ht <= $thmb_ht) ) {
$thmbn_wd = $wd;
$thmbn_ht = $ht;}else if (($thmbx_ratio $ht) < $thmb_ht) {
$thmbn_ht = ceil($thmbx_ratio $ht);
$thmbn_wd = $thmb_wd;}else {
$thmbn_wd = ceil($thmby_ratio $wd);
$thmbn_ht = $thmb_ht;}
$src = ImageCreateFromJpeg($userfile);
// large img
$dst = ImageCreateTrueColor($tn_wd,$tn_ht);
ImageCopyResized($dst, $src, 0, 0, 0, 0,$tn_wd,$tn_ht,$wd,$ht);
header('Content-type: image/jpeg');
ImageJpeg($dst, 'upload_imgs/'.$userfile_name, 80);
ImageDestroy($dst);
//small img
$dst = ImageCreateTrueColor($thmbn_wd,$thmbn_ht);
ImageCopyResized($dst, $src, 0, 0, 0, 0,$thmbn_wd,$thmbn_ht,$wd,$ht);
header('Content-type: image/jpeg');
ImageJpeg($dst, 'upload_thmbs/'.$userfile_name, 80);
ImageDestroy($dst);
ImageDestroy($src);
//end scale
if (!is_uploaded_file($userfile))
{ ?>
<table border="0" cellspacing="1" cellpadding="2" width="800" align="center">
<tr><td class="head2"><br>File upload error: Filename: <?php echo $userfile_name; ?><p></td></tr>
</table>
<?php exit;
} else { ?>
<table border="0" cellspacing="1" cellpadding="2" width="800" align="center">
<tr><td class="head2"><br>File <?php echo $userfile_name; ?> uploaded successfully<p></td></tr>
</table>
<?php
}
?>