dourao1968 2012-04-23 01:33
浏览 26
已采纳

PHP图像上传器不使用Capital JPG扩展

Thanks in advance.

I have a php image uploader that I built, which is working just fine. However I tried uploading a .JPG extension image and it did not work. Please note I mean capital JPG and not lowercase jpg. .png, .jpg, .gif all work fine and get uploaded, the thumbnails are created and what not. It is the capital .JPG that isn't working for some reason. See my code below. Is it a different mime type?

The queries (which I removed for security purposes) work just fine. The information is stored. It is the moving of the file and thumbnail to the correct folders that isn't happening due to the capital .JPG extension. Any help would be great, I did some research and tried the Apache add type JPG and that did not work.

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>';
            }        
        }
    }
}

Thumbnail function if it helps:

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);  

}
  • 写回答

1条回答 默认 最新

  • dousou1878 2012-04-23 01:55
    关注

    From your thumbnail code:

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

    That will only match lowercase 'jpg'. If you want to match upper or lowercase, you can add the /i flag for case-insensitivity:

    if(preg_match('/[.](jpg)$/i', $filename)) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏