2

提前致谢。

我有一个我构建的 php 图片上传器,它工作得很好。但是,我尝试上传 .JPG 扩展名图片,但没有成功。请注意,我的意思是大写 JPG,而不是小写 jpg。.png、.jpg、.gif 都可以正常工作并被上传,缩略图被创建等等。由于某种原因,它是大写的 .JPG 不起作用。请参阅下面的代码。它是不同的 mime 类型吗?

查询(出于安全目的我删除了)工作得很好。信息被存储。由于大写的 .JPG 扩展名,文件和缩略图不会移动到正确的文件夹中。任何帮助都会很棒,我做了一些研究并尝试了 Apache 添加类型 JPG,但没有奏效。

if(isset($_POST['submit']))
{

    $getcatid = mysql_query('"query here"');
    $rescatid = mysql_fetch_assoc($getcatid);
    $catid = $rescatid['catid'];

    if ((($_FILES['file']['type'] == "image/png") || 
        ($_FILES['file']['type'] == "image/jpeg") ||
        ($_FILES['file']['type'] == "image/pjpeg")) && ($_FILES['file']['size'] < 2097152))
    {

        if (file_exists("location here".time().'_'.$_FILES["file"]["name"]))
            {
                echo '<div class="error">'.$_FILES["file"]["name"].' already exists</div>'; 

            }
        else
            {

                    $filename = time().'_'.$_FILES['file']['name'];  
                    $source = $_FILES['file']['tmp_name'];  
                    $target = 'location here'.$filename;  

                    move_uploaded_file($source, $target); 

                    createThumbnail($filename);  

                $date = time();
                $store = '"query here"';';

                mysql_query ($store);

                echo '<div class="success">Image added</div>';

            }
    }

    else
    {
        echo '<div class="error">Invalid file, only jpg, jpeg, or png file types allowed</div>';
    }

}

if(isset($_POST['multisubmit']))
{

    $getcatid = mysql_query('"query here"');
    $rescatid = mysql_fetch_assoc($getcatid);
    $catid = $rescatid['catid'];


    for($i=0;$i< count($_FILES['multifile']['type']);$i++)
    {

        if ((($_FILES['multifile']['type'][$i] == "image/png") || 
        ($_FILES['multifile']['type'][$i] == "image/jpeg") || 
        ($_FILES['multifile']['type'][$i] == "image/pjpeg")) && 
        ($_FILES['multifile']['size'][$i] < 2097152))
        {

            $filename = time().'_'.$_FILES['multifile']['name'][$i]; 
            $source = $_FILES['multifile']['tmp_name'];   
            $target = 'location here'.$filename;  

            if(move_uploaded_file ($_FILES['multifile']['tmp_name'][$i],$target))
            {
                createThumbnail($filename);  

                $date = time();
                $store = '"query here"';';

                mysql_query ($store);

                echo '<div class="success">Image: '.$filename.' added, view individual album to edit image descriptions</div>';
            } 
            else
            {
            echo '<div class="error">Invalid file'.$filename.', only jpg, jpeg, or png file types allowed</div>';
            }        
        }
    }
}

缩略图功能如果有帮助:

function createThumbnail($filename) {  
$final_width_of_image = 160;  
$path_to_image_directory = 'path here';  
$path_to_thumbs_directory = 'path here';

if(preg_match('/[.](jpg)$/', $filename)) {  
    $im = imagecreatefromjpeg($path_to_image_directory . $filename);  
} else if (preg_match('/[.](gif)$/', $filename)) {  
    $im = imagecreatefromgif($path_to_image_directory . $filename);  
} else if (preg_match('/[.](png)$/', $filename)) {  
    $im = imagecreatefrompng($path_to_image_directory . $filename);  
}  

$ox = imagesx($im);  
$oy = imagesy($im);  

$nx = $final_width_of_image;  
$ny = floor($oy * ($final_width_of_image / $ox));  

$nm = imagecreatetruecolor($nx, $ny);  

imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);  

if(!file_exists($path_to_thumbs_directory)) {  
  if(!mkdir($path_to_thumbs_directory)) {  
       die("There was a problem. Please try again!");  
  }  
   }  

imagejpeg($nm, $path_to_thumbs_directory . $filename, 100);  

}
4

1 回答 1

1

从您的缩略图代码:

if(preg_match('/[.](jpg)$/', $filename)) 

那只会匹配小写的'jpg'。如果要匹配大小写,可以添加/i不区分大小写的标志:

if(preg_match('/[.](jpg)$/i', $filename)) 
于 2012-04-23T01:55:31.607 回答