0

我使用下面的代码上传多个文件。现在我需要调整图像大小并在块的末尾使用代码(CREATE THUMB)。

我的麻烦是无法获取文件图像来创建拇指。我已经签出

$filename = $_FILES["file"]["tmp_name"]; 
$filename = $_FILES["file"]["name"]; 

但似乎不起作用...

有什么帮助吗?

if (!isset($_SESSION)) session_start();
ob_start();

include_once "../../config.php";



// HTTP headers for no cache etc
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");


//$cleanupTargetDir = false; // Remove old files
//$maxFileAge = 60 * 60; // Temp file age in seconds

// 5 minutes execution time
@set_time_limit(5 * 60);

// Uncomment this one to fake upload time
// usleep(5000);

// Get parameters
$chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
$chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
$fileName = isset($_FILES["file"]["name"]) ? $_FILES["file"]["name"] : '';
//$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';


// Settings
//$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";


$targetDir = 'uploads/';

// Clean the fileName for security reasons
$fileName = preg_replace('/[^\w\._]+/', '', $fileName);

// Make sure the fileName is unique but only if chunking is disabled
if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
   die('{"jsonrpc" : "2.0", "error" : {"code": 109, "message": "File Already Exists."}, "id" : "id"}');

}

// Create target dir
if (!file_exists($targetDir))
   @mkdir($targetDir);

// Look for the content type header
if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
   $contentType = $_SERVER["HTTP_CONTENT_TYPE"];

if (isset($_SERVER["CONTENT_TYPE"]))
   $contentType = $_SERVER["CONTENT_TYPE"];

// Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
if (strpos($contentType, "multipart") !== false) {
   if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
        // Open temp file
        $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
        if ($out) {
            // Read binary input stream and append it to temp file
            $in = fopen($_FILES['file']['tmp_name'], "rb");

        if ($in) {
            while ($buff = fread($in, 4096))
                fwrite($out, $buff);
        } else
            die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
        fclose($in);
        fclose($out);
        @unlink($_FILES['file']['tmp_name']);
    } else
        die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
} else
    die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
} else {

// Open temp file
$out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");

if ($out) {
    // Read binary input stream and append it to temp file
    $in = fopen("php://input", "rb");

    if ($in) {
        while ($buff = fread($in, 4096))
            fwrite($out, $buff);
    } else
        die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');

    fclose($in);
    fclose($out);

} else
    die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}



// CREATE THUMB


//Name you want to save your file as
//$save = 't_foo.jpg';
//$save = '';
$save = $targetDir.'/t_'.$fileName;


// The file
$filename = $_FILES["file"]["tmp_name"];

// Set a maximum height and width
$width = 200;
$height = 200;

// Content type
//header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, $save, 100);



////// HERE CODE TO STORE MYSQL /// 


// Return JSON-RPC response
die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
4

1 回答 1

0

不能直接处理上传到 tmp 目录的文件。尝试以这种方式移动文件,

move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name)

请注意, “文件”是<input>表单中元素的名称

<input type="file" name="file">

“文件”可以是任何其他值。

于 2013-08-18T05:06:43.863 回答