2013-02-21 26 views
0

MrCode的領導之後,我能夠發現爲什麼我的上傳沒有通過其他圖片格式。下面是createThumbs函數,所以我如何讓它爲其他擴展工作。只上傳jpg格式的PHP

<?php 
function createThumbs($pathToImages, $pathToThumbs, $thumbWidth) 
{ 
// open the directory 
$dir = opendir($pathToImages); 

// loop through it, looking for any/all JPG files: 
while (false !== ($fname = readdir($dir))) { 
// parse path for the extension 
$info = pathinfo($pathToImages . $fname); 
// continue only if this is a JPEG image 
if (strtolower($info['extension']) == 'jpg') 
{ 
    // load image and get image size 
    $img = imagecreatefromjpeg("{$pathToImages}{$fname}"); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    $new_height = floor($height * ($thumbWidth/$width)); 

    // create a new tempopary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 
    imagejpeg($tmp_img, "{$pathToThumbs}{$fname}"); 
} 
} 
// close the directory 
closedir($dir); 
} 

?> 
+0

你會得到什麼錯誤,你使用了哪些腳本(自制或庫)。 – Manuel 2013-02-21 11:12:37

+0

嘗試上傳單曲「png」或「gif」。 – 2013-02-21 11:14:59

+0

發佈的代碼對文件類型不敏感,我猜你的問題在'createThumbs()'中,可能是因爲你只使用'imagejpg *()'函數。您必須顯示該功能的代碼。 – MrCode 2013-02-21 11:17:46

回答

0

我認爲這個問題是在循環

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ 

應該這樣寫

foreach($_FILES['files'] as $key => $file){ 

作爲第一個將僅運行於第一時間和第一file有可能一個jpeg圖片 因此它第一次運行。

+0

情況並非如此。 '$ _FILES ['files'] ['tmp_name'] [$ key]'是多個文件上傳的正確結構。見http://php.net/manual/en/features.file-upload.multiple.php – MrCode 2013-02-21 11:23:34