0

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

提前致谢!科林

4

1 回答 1

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');
于 2012-04-27T16:42:13.167 回答