文件已上传,但未在修改的Uploadify脚本中创建缩略图

时间:2012-04-27 16:29:05

标签: php file-upload uploadify image-uploading

我有一个相当简单的脚本来处理从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.';
    }
}
?>

提前致谢! 科林

1 个答案:

答案 0 :(得分: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下面输出错误

 error_reporting(E_ALL);
 ini_set('display_errors','On');