2012-04-27 2 views
0

У меня довольно простой скрипт, который обрабатывает информацию о файлах, отправленную на сервер из Uploadify, и все работает, кроме создания миниатюры. Может ли кто-нибудь увидеть, где моя ошибка?Файл загружен, но thumbnail не создан в модифицированном скрипте Uploadify

<?php 
session_start(); 
include "../_db.inc"; 

/* 
Uploadify v3.1.0 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/ 

// Define a destination 
$targetFolder = '/images/artist_pictures/'; // Relative to the root 
$thumbsFolder = '/images/artist_pictures/thumbs/'; // Relative to the root 

if (!empty($_FILES)) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
     $yourid = (int)$_POST['yourid']; 
     $extension = end(explode(".", $_FILES['Filedata']['name'])); 
     $filename = "artist_".$yourid.".".$extension; 
    $targetFile = rtrim($targetPath,'/') . '/' . $filename; 
     $targetThumb = rtrim($thumbsFolder,'/') . '/' . $filename; 

    // Validate the file type 
    $fileTypes = array('jpg','jpeg','gif','png','JPG','bmp'); // File extensions 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 

    if (in_array($fileParts['extension'],$fileTypes)) { 
       // CREATE THUMBNAIL 
       if($extension=="jpg" || $extension=="jpeg") { 
        $src = imagecreatefromjpeg($tempFile); 
       } 
       else if($extension=="png") { 
        $src = imagecreatefrompng($tempFile); 
       } 
       else { 
        $src = imagecreatefromgif($tempFile); 
       } 

       list($width,$height)=getimagesize($tempFile); 

       $newwidth=50; 
       $newheight=($height/$width)*$newwidth; 
       $tmp=imagecreatetruecolor($newwidth,$newheight); 

       imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

       $thumbname = $targetThumb; 

       if (file_exists($thumbname)) { 
        unlink($thumbname); 
     } 

       imagejpeg($tmp,$thumbname,100); 

       imagedestroy($src); 
       imagedestroy($tmp); 

     move_uploaded_file($tempFile,$targetFile); 
     echo '1'; 
    } else { 
     echo 'Invalid file type.'; 
    } 
} 
?> 

Заранее благодарен! Colin

ответ

1

Я проверил ваш код и он работает с несколькими изменениями причины

Используется

$targetFolder = realpath('/temp'); // Relative to the root 
$thumbsFolder = realpath('/temp/thumbs/'); // Relative to the root 

Существует также ошибка при $extension = end(explode(".", $_FILES['Filedata']['name']));

Заменить его

$extension = $fileParts ['extension']; 

Полный код

<?php 
session_start(); 
//include "../_db.inc"; 

/* 
* Uploadify v3.1.0 Copyright (c) 2012 Reactive Apps, Ronnie Garcia Released 
* under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/ 

// Define a destination 
    $targetFolder = realpath('/temp'); // Relative to the root 
    $thumbsFolder = realpath('/temp/thumbs/'); // Relative to the root 

if (! empty ($_FILES)) { 
    $tempFile = $_FILES ['Filedata'] ['tmp_name']; 
    $targetPath = $_SERVER ['DOCUMENT_ROOT'] . $targetFolder; 
    $yourid = 2 ;//(int) $_POST ['yourid']; 

    $fileParts = pathinfo ($_FILES ['Filedata'] ['name']); 
    $extension = $fileParts ['extension']; 
    $filename = "artist_" . $yourid . "." . $fileParts ['extension']; 
    $targetFile = rtrim ($targetPath, '/') . '/' . $filename; 
    $targetThumb = rtrim ($thumbsFolder, '/') . '/' . $filename; 

    // Validate the file type 
    $fileTypes = array (
      'jpg', 
      'jpeg', 
      'gif', 
      'png', 
      'JPG', 
      'bmp' 
    ); // File extensions 

    if (in_array ($fileParts ['extension'], $fileTypes)) { 
     // CREATE THUMBNAIL 
     if ($extension == "jpg" || $extension == "jpeg") { 
      $src = imagecreatefromjpeg ($tempFile); 
     } else if ($extension == "png") { 
      $src = imagecreatefrompng ($tempFile); 
     } else { 
      $src = imagecreatefromgif ($tempFile); 
     } 

     list ($width, $height) = getimagesize ($tempFile); 

     $newwidth = 50; 
     $newheight = ($height/$width) * $newwidth; 
     $tmp = imagecreatetruecolor ($newwidth, $newheight); 

     imagecopyresampled ($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

     $thumbname = $targetThumb; 

     if (file_exists ($thumbname)) { 
      unlink ($thumbname); 
     } 

     imagejpeg ($tmp, $thumbname, 100); 

     imagedestroy ($src); 
     imagedestroy ($tmp); 

     move_uploaded_file ($tempFile, $targetFile); 
     echo '1'; 
    } else { 
     echo 'Invalid file type.'; 
    } 
} 
?> 


<form method="post" target="_self" enctype="multipart/form-data"> 
    <input type="file" name="Filedata" id="file" /> <input name="submit" 
     type="submit" value="submit" /> 
</form> 

Заключение .. этот вопрос, кажется, ваш путь к файлу и некоторые PHP неопределенные ошибки Yse в folloing для вывода ошибок

error_reporting(E_ALL); 
ini_set('display_errors','On'); 
Смежные вопросы