Php 当localhost发现jpeg图像类型时,联机服务器无法发现jpeg图像类型

Php 当localhost发现jpeg图像类型时,联机服务器无法发现jpeg图像类型,php,jpeg,image-uploading,file-type,Php,Jpeg,Image Uploading,File Type,我正在使用一个简单的脚本来上传和调整上传的图像,同时保持透明度问题是服务器无法识别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,".

我正在使用一个简单的脚本来上传和调整上传的图像,同时保持透明度问题是服务器无法识别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行,但是本地主机可以毫无问题地上传相同的图像,并且当返回$_文件[“main\u img”][“type”]时,我得到图像/jpeg。 有什么想法吗?

试试这个:

     $type = strtolower(pathinfo($src, PATHINFO_EXTENSION));
您也可以像这样存储pic类型(但无论如何都不使用它)


问题是在打开到数据库的连接之前使用mysql\u real\u escape\u字符串,并且应该在打开连接之后使用它

本地服务器基于Windows,远程服务器基于Unix/Linux吗?如果是这样,请考虑文件属性扩展名中的大小写敏感度扩展名是小写的jpg。您可以对类型变量进行回显以验证解析到服务器的内容吗?这很奇怪,它不会返回图像类型。也许您最好在代码中添加某种错误处理。看看这个。可能您超出了主机的文件大小限制?如果知道错误代码就好了。它不会返回错误,但是图像没有上传,刷新后页面也没有得到任何数据。唯一的变化是错误没有显示。现在$type的输出是什么?它不会回显我要求在函数文件中的不同位置回显的任何内容,但根本没有任何$u文件[“main\u img”][“type”)返回图像/JPEG这是返回文件类型的方式,但您无论如何都不会使用它,是吗?使用您的代码,您还可以上载一个移动/可执行文件,并简单地将其命名为.jpg,因为您不进行任何文件类型检查
$pic_type = ["main_img"]["type"]