从上传器获取文件(调整大小)

时间:2013-08-18 04:07:56

标签: php

我使用下面的代码上传多个文件。 现在我需要调整图像大小并在块结束时使用代码(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"}');

1 个答案:

答案 0 :(得分:0)

您无法直接处理在tmp目录中上传的文件。 尝试以这种方式移动文件,

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

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

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

“file”可以是任何其他值。