PHP - 图片上传,move_uploaded_file无效?

时间:2015-06-12 22:34:53

标签: php redirect image-uploading

我编写了一个图片上传脚本,但是当我点击上传时,我会被重定向到上传页面?

以下是代码:

    $image1name = $_FILES['image1']['name'];
    $image1crntloc = $_FILES['image1']['tmp_name'];
    $image1ext = pathinfo($image1name, PATHINFO_EXTENSION);
    $image1size = $_FILES['image1']['size'];

    $allowedext = array("jpg","gif","png");
    //check image 1 extension.
    if (!in_array($image1ext,$allowedext))
    {
        echo "<script>alert(\"Image 1 has an invalid file.\");</script>";
    }

    else{
        $image1final = md5(time($image1name));
        $saveimage1 = "../images/".$image1final.".".$image1ext;
        $image1uploadresult = move_uploaded_file($image1crntloc,       $saveimage1); 

        if ($image1uploadresult == TRUE)
        {
            echo "uploaded.";
        }
        else{
            echo "image not uploaded.";
        }

一旦我点击上传,我就会被重定向到用户选择图片的页面,我还检查了目录并且没有上传任何内容? 也没有显示PHP错误。

任何帮助?

感谢。

表格代码:

<form id="new-ad" name="new-ad" method="post" action="includes/create.php" enctype="multipart/form-data">
<div class="form-group animated fadeIn">
<label class="labelcustom" for "image1">Image #1:</label>
<br />
Select image to upload:
<input type="file" class="form-control" name="image1" id="image1" />
<input type="submit" name="submit" />
</form>

2 个答案:

答案 0 :(得分:1)

在您的代码中,您错过了else声明中的结束括号:

else {
    echo "image not uploaded.";
}

应该是:

    else {
        echo "image not uploaded.";
    }
}

答案 1 :(得分:0)

在您的表单中,您忘记了最大文件大小。添加<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />,其值是表单中要接受的最大字节数。