1

我有以下用于上传图片的脚本;此文件适用于除 PNG 文件之外的所有其他文件扩展名。有什么理由吗?

这是我的脚本;

// initialization
$result_final = "";
$counter = 0;

// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
    
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');

// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
while( $counter <= count($_FILES['photo_filename']['tmp_name']) ) 

    {
            if($photos_uploaded['size'][$counter] > 0)
            {
                    if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                    {
                            $result_final .= "File ".($counter+1)." is not a photo!<br />";
                    }else{
                    
                       mysql_query( "INSERT INTO database(`filename`, `caption`, `category`, `id`) VALUES('0', '".addslashes($caption[$counter])."', '".addslashes($_POST['category'])."', '".addslashes($_POST['id'])."')" );
                            $new_id = mysql_insert_id();
                            $filetype = $photos_uploaded['type'][$counter];
                            $extention = $known_photo_types[$filetype];
                            $filename = $new_id.".".$extention;

                            @mysql_query( "UPDATE database SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );

// Store the orignal file
copy($photos_uploaded['tmp_name'][$counter], $images_dir2."/".$filename);

// Let's get the original image size                                
$size = GetImageSize( $images_dir2."/".$filename );

// .................................................................................
// Lets resize the image if its width is greater than 500 pixels
// Resized image settings
if ($size[0] > '1'){
        $Config_width_wide = 800; // width of wide image
        $Config_height_wide = 800; // height of wide image

        $Config_width_tall = 800; // width of tall image
        $Config_height_tall = 800; // height of tall image

// The Code
        if($size[0] > $size[1]){
            $image_width = $Config_width_wide;
            $image_height = (int)($Config_width_wide * $size[1] / $size[0]);

            if($image_height > $Config_height_wide){
                $image_height = $Config_height_wide;
                $image_width = (int)($Config_height_wide * $size[0] / $size[1]);
            }
        }else{
            $image_width = (int)($Config_height_tall * $size[0] / $size[1]);
            $image_height = $Config_height_tall;

            if($image_width > $Config_width_tall){
                $image_width = $Config_width_tall;
                $image_height = (int)($Config_width_tall * $size[1] / $size[0]);
            }
        }

// Build image with GD 2.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;

// Read the source file
$source_handle = $function_to_read ( $images_dir2."/".$filename );

if($source_handle){
// Let's create a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

// Now we resize it
ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}

// Store the orignal file
copy($photos_uploaded['tmp_name'][$counter], $images_dir2."/".$filename);
 
// Let's save the image
if ($extension == 'png') {
$function_to_write( $destination_handle, $images_dir2."/".$filename, 9 );
} else {
$function_to_write( $destination_handle, $images_dir2."/".$filename, 90 ); 
}
ImageDestroy($destination_handle );
        
                    }

                }
            }
        $counter++;
    }   
} else exit('<p>Error: ' .
    mysql_error() . '</p>');

那是我的代码,当我上传 jpeg 或 gif 文件时一切正常,但是当我上传 png 文件时,它不起作用。我也没有得到任何错误。请问,可能是什么错误?

4

2 回答 2

2

只需将其更改为:

image/png
于 2013-04-25T14:34:11.820 回答
1

png 是官方认可的 mime 类型,它应该只是image/png. x-前缀用于实验/非官方类型。你可以简单地验证你得到了什么var_dump($_FILES)

您的上传处理代码也需要更新。您不检查是否成功上传,而只是假设它们成功。永远不要假设成功。

if($_FILES['photo_filename']['error'] !== UPLOAD_ERR_OK) {
  die("Upload failed with error code " . $_FILES['photo_filename']['error']);
}

addslashes()是完全没用的垃圾。它无法保护您免受 sql 注入攻击如果您坚持继续使用已弃用的 mysql_*() 函数,那么至少使用正确的mysql_real_escape_string().

于 2013-04-25T14:34:55.627 回答