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未定義エラーは次のようにエラーを出力します

 error_reporting(E_ALL);
 ini_set('display_errors','On');
于 2012-04-27T16:42:13.167 に答える