2014-11-04 44 views
1

我正在使用此腳本上傳圖片.jpg &.png類型到服務器和數據庫,但是當談到.jpeg或.JPG它不工作,而不是將文件放在正確的目錄中,並且使用正確的擴展名,它只是在/galleri/uploads/a4b7c7fb0de9c561110c2279f24ec820jpeg.php中自動添加.php文件。上傳圖片到服務器不起作用.jpeg是類型

我一直試圖做的是增加這些線路

if ($type == 'image/jpeg') { $filetype = '.jpeg'; } else { $filetype = str_replace('image/', '', $type); } 

if ($type == 'image/jpeg') { $filetype = '.JPG'; } else { $filetype = str_replace('image/', '', $type); } 

,但沒有用..

除了有沒有更好的裁切工具,我可以在這種情況下使用是不是很先進?

這是完整的腳本:

if(isset($_POST['addmedia'])) { 
    $mediatype = escape(striptags($_POST['mediatype'])); 
    $title =  escape(striptags($_POST['title'])); 
    $video =  escape(striptags($_POST['medialink'])); 
    $date =   date('Y-m-d'); 

    if ($mediatype === 'img') { 
     if(!isset($_POST['p'])) { $_POST['p']= 0; } 

     if($_POST['p'] == 1) { 
      $name =  $_FILES['image']['name']; 
      $temp =  $_FILES['image']['tmp_name']; 
      $type =  $_FILES['image']['type']; 
      $size =  $_FILES['image']['size']; 

      if ($type == 'image/jpeg') { $filetype = '.jpg'; } else { $filetype = str_replace('image/', '', $type); } 
      if ($type == 'image/png') { $filetype = '.png'; } else { $filetype = str_replace('image/', '', $type); } 

      $path =    md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000)) . $filetype; 
      $thumb_path =  md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000)) . $filetype; 

      $size2 = getimagesize ($temp); 
      $width = $size2[0]; 
      $height = $size2[1]; 

      $maxwidth = 1281; 
      $maxheight = 751; 
      $allowed = array('image/jpeg', 'image/png'); 

      if(in_array($type, $allowed)) { 
       if($width < $maxwidth && $height < $maxheight) { 
        if($size < 10485760) { 
         if($width == $height) { $case = 1;} // Square form 
         if($width > $height) { $case = 2;} // Lying form 
         if($width < $height) { $case = 3;} // Standing form 

         switch($case) { 
          case 1: 
          $newwidth = 280; 
          $newheight = 150; 
          break; 

          case 2: 
          $newheight = 150; 
          $ratio  = $newheight/$height; 
          $newwidth = round($width * $ratio); 
          break; 

          case 3: 
          $newwidth = 280; 
          $ratio  = $newwidth/$width; 
          $newheight = round($height * $ratio); 
          break; 
         } 

         switch($type) { 
          case 'image/jpeg': 
          $img = imagecreatefromjpeg($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
          imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
          imagejpeg($thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path); 
          break; 

          case 'image/png': 
          $img = imagecreatefrompng($temp); 
          $thumb = imagecreatetruecolor($newwidth, $newheight); 
          imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
          imagepng($thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path); 
          break; 
         } 

         move_uploaded_file($temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/" . $path); 
         $addimg = "INSERT INTO uploads (`type`, `title`, `src`, `thumb`, `date`) VALUES ('$mediatype', '$title', '$path', '$thumb_path', '$date')"; 
         if ($add_img = $db_connect->query($addimg)) {} 
         echo 'Din bild har laddats upp!'; 
         header("Location: " . $_SERVER['HTTP_REFERER']); 

        } else { 
         echo '10MB'; 
        } 
       } else { 
        echo 'To big in size'; 
       } 
      } else { 
       echo '.jpg, .jpeg, .png!'; 
      } 
     } 
    } else if ($mediatype === 'vid') { 
     $name  =  $_FILES['image']['name']; 
     $temp  =  $_FILES['image']['tmp_name']; 
     $size  =  $_FILES['image']['size']; 
     $thumb_path =  md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000)) . '.jpg'; 

     move_uploaded_file($temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path); 
     $addvid = "INSERT INTO uploads (`type`, `title`, `thumb`, `videolink`, `date`) VALUES ('$mediatype', '$title', '$thumb_path', '$video', '$date')"; 

     if ($add_vid = $db_connect->query($addvid)) {} 
     echo 'Video uploaded'; 
     header("Location: " . $_SERVER['HTTP_REFERER']); 
    } 
} 
+0

你能不能做一個'回聲$類型;'爲你的形象,其未被接受,並告訴我們結果如何呢? – akmozo 2014-11-04 13:02:32

+0

@akmozo我注意到一件事,當我刪除.png格式的.jpg和.JPG工作正常..這很奇怪 – Montague 2014-11-04 13:16:10

+0

這是因爲你的錯誤條件。我給出了答案,看看。 – akmozo 2014-11-04 13:17:32

回答

2

試試這個:

<?php 

    ... 

    $name = $_FILES['image']['name']; 
    $temp = $_FILES['image']['tmp_name']; 
    $size = $_FILES['image']['size']; 

    $type = image_type_to_mime_type(exif_imagetype($temp)); // get the real image mime type 

    if ($type == 'image/jpeg') { // jpeg 

     $filetype = '.jpg'; 

    } else if ($type == 'image/png'){ // png 

     $filetype = '.png'; 

    } else { // other image type 

     $filetype = '.' . str_replace('image/', '', $type); // to get .gif for a gif image, for example 

    } 

    ... 

?> 
+0

感謝它真棒,但是當我上傳前。 .gif將它上傳爲.jpg,而應該有一條消息說明此類型不可上傳 – Montague 2014-11-04 13:43:44

+0

是否要禁用.gif上傳? – akmozo 2014-11-04 13:47:46

+0

不,但我想ot禁用所有其他像.tga或.pdf等 – Montague 2014-11-04 13:48:17