上传的图像不会保存到文件夹

时间:2013-11-24 07:02:05

标签: php file-upload

我有一个html上传表单,由image-crop.php处理。我想在上传完成后将图像保存到img文件夹。但它不是将图像保存到img文件夹。任何建议为什么图像不保存到该文件夹​​?

Html格式:

<form action="image-crop-demo.php" method="post" enctype="multipart/form-data">
    Upload an image for processing<br>
    <input type="file" name="Image1"><br>
    <input type="submit" value="Upload">
</form>

Php代码:

define('DESIRED_IMAGE_WIDTH', 200);
define('DESIRED_IMAGE_HEIGHT', 200);
$source_path = $_FILES['Image1']['tmp_name'];
list($source_width, $source_height, $source_type) = getimagesize($source_path);

switch ($source_type) {
    case IMAGETYPE_GIF:
        $source_gdim = imagecreatefromgif($source_path);
        break;
    case IMAGETYPE_JPEG:
        $source_gdim = imagecreatefromjpeg($source_path);
        break;
    case IMAGETYPE_PNG:
        $source_gdim = imagecreatefrompng($source_path);
        break;
}

$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

if ($source_aspect_ratio > $desired_aspect_ratio) {
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
} else {
    $temp_width = DESIRED_IMAGE_WIDTH;
    $temp_height = ( int ) (DESIRED_IMAGE_WIDTH / $source_aspect_ratio);
}

$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
    $temp_gdim,
    $source_gdim,
    0, 0,
    0, 0,
    $temp_width, $temp_height,
    $source_width, $source_height
);

$x0 = ($temp_width - DESIRED_IMAGE_WIDTH) / 2;
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT) / 2;
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
imagecopy(
    $desired_gdim,
    $temp_gdim,
    0, 0,
    $x0, $y0,
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
);

header('Content-type: image/jpeg');
imagejpeg($desired_gdim, "img/", $_FILES["file"]["name"]);

感谢。

1 个答案:

答案 0 :(得分:0)

您错误地传递了第二个参数:

imagejpeg($desired_gdim, "img/", $_FILES["file"]["name"]);
                               ^--- THE COMMA SHOULD NOT BE THERE

可能你需要传递绝对路径,而不是相对路径,所以试试这个:

imagejpeg($desired_gdim, $_SERVER["DOCUMENT_ROOT"]."img/".$_FILES["file"]["name"]);