PHP image uploader not working with Capital JPG extension

心已入冬 提交于 2019-12-24 00:34:05

问题


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:


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


来源:https://stackoverflow.com/questions/10273804/php-image-uploader-not-working-with-capital-jpg-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!