【问题标题】:online server can not discover jpeg image type while localhost does本地服务器发现时,在线服务器无法发现 jpeg 图像类型
【发布时间】:2013-05-29 20:56:32
【问题描述】:

我正在使用一个简单的脚本来上传和调整上传图像的大小,同时保持透明度问题是服务器无法识别 jpeg 图像,而 localhost 可以识别这里的代码

function image_resize($src, $dst, $width, $height, $crop=0){

if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type 1!";

$type = strtolower(substr(strrchr($src,"."),1));
if($type == 'jpeg') { $type = 'jpg'; }
switch($type){
case 'bmp': $img = imagecreatefromwbmp($src); break;
case 'gif': $img = imagecreatefromgif($src); break;
case 'jpg': $img = imagecreatefromjpeg($src); break;
case 'png': $img = imagecreatefrompng($src); break;
default : return "Unsupported picture type 2!";
}

$new = imagecreatetruecolor($width, $height);

// preserve transparency
if($type == "gif" or $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}

imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

switch($type){
case 'bmp': imagewbmp($new, $dst); break;
case 'gif': imagegif($new, $dst); break;
case 'jpg': imagejpeg($new, $dst); break;
case 'png': imagepng($new, $dst); break;
}
return true;
}    

这是上传代码

require('_req/func.php');       
$img_name = mysql_real_escape_string($_FILES['main_img']['name']);
if(strstr($img_name," ")){
$img_name = str_replace(" ","_",$img_name); 
}
$num = substr(md5(mt_rand(1,999999)),0,4);
$new_name = $num.$img_name;

move_uploaded_file($_FILES['main_img']['tmp_name'], "../products_large/".$new_name);
$pic_type = strtolower(strrchr($_FILES['main_img']['name'],"."));
$pic_name = "../products_large/".$new_name;

if (true !== ($pic_error = @image_resize($pic_name, "../products_thumb/".$new_name, 180, 180, 1))) {
echo $pic_error;
unlink($pic_name);
 }
    else {
require_once('_req/base.php');
$addNameQ = "update products set Product_Img = '$new_name' where Product_ID = '$id'";
$addNameR = mysql_query($addNameQ);
mysql_close($connect);
}

我得到的错误是

Unsupported picture type 2!

在函数代码的第 10 行,但是本地主机可以毫无问题地上传相同的图像,并且在返回 $_FILES["main_img"]["type"] 时,我得到了图像/jpeg。 请问有什么想法吗?

【问题讨论】:

  • 本地服务器基于Windows,远程基于Unix/Linux?如果是这样,请考虑文件属性扩展名是否区分大小写
  • 扩展名是小写的jpg
  • 您能否对类型变量进行回显以验证已解析到服务器的内容?
  • 奇怪的是它不返回图像类型
  • 也许您最好在代码中添加某种错误处理。看看这个link。也许您超出了主机的文件大小限制?最好知道错误代码

标签: php jpeg image-uploading file-type


【解决方案1】:

试试这个:

     $type = strtolower(pathinfo($src, PATHINFO_EXTENSION));

你也可以这样存储图片类型(但你反正不用)

$pic_type = ["main_img"]["type"]

【讨论】:

  • 它没有返回错误但图像没有上传并且页面刷新后没有得到任何数据唯一的变化是错误没有出现
  • 现在 $type 的输出是什么?
  • 它没有回显我试图在函数文件中的不同位置回显它的任何内容,但什么也没有
  • $_FILES["main_img"]["type") 返回图片/jpeg
  • 这就是文件类型被返回的方式,但你不使用它吗?使用您的代码,您还可以上传移动/可执行文件并将其命名为 .jpg,因为您不进行任何文件类型检查
【解决方案2】:

问题是在打开与数据库的连接之前使用mysql_real_escape_string,应该在打开连接后使用它

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2013-05-11
    • 1970-01-01
    相关资源
    最近更新 更多