2013-02-21 72 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