文件上传脚本返回false

时间:2014-04-01 02:16:20

标签: php mysql

我正在尝试创建一个脚本,允许用户更改我正在制作的网站的个人资料图片。以下是表单的HTML代码:

        <form action="upload_prof_pic.php" method="POST" enctype="multipart/form-data">
            <input type="file" class="btn btn-default" name="file" id="file" /><br /><br />
            <input type="submit" class="btn btn-default" value="Upload Profile Picture" />
        </form>

以下是upload_prof_pic.php的PHP代码:

<?php
session_start();
require("includes/connect.php");

$results = $db->query("SELECT * FROM users WHERE username='".$_SESSION["logged_in"]."'");
$rows = $results->fetch();

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (
    (
       ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png")
    )
    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts)
) {
    if ($_FILES["file"]["error"] > 0) {
        //error: uploading
        header("Location: account.php?err=upload");
    } else {
        //success
        move_uploaded_file($_FILES["file"]["tmp_name"],
            "prof_pic/users/".$rows["username"]."/" . $_FILES["file"]["name"]);
        $db->query("UPDATE users SET prof_pic='".$_FILES['file']['name']."' WHERE username='".$_SESSION["logged_in"]."'");
        header("Location: account.php");
     }
} else {
    //error: invalid file
    header("Location: account.php?err=invalid");
}

?>

它始终运行&#39; //错误:无效文件&#39;脚本的一部分。有人可以帮忙吗?它运作了一次,然后我改变了成功&#39; //成功&#39;部分。这不应该有任何影响,但显然确实如此。

1 个答案:

答案 0 :(得分:0)

我得到了同样的错误,因此我使用了以下javascript编码。

    <script type="text/javascript" src="js/jquery-func.js"></script>
<SCRIPT type="text/javascript">
    function ValidateFileUpload() {
        var fuData = document.getElementById('filename');
        var FileUploadPath = fuData.value;

//To check if user upload any file
        if (FileUploadPath == '') {
            alert("Please upload an image");

        } else {
            var Extension = FileUploadPath.substring(
                    FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

//The file uploaded is an image

if (Extension == "gif" || Extension == "png" || Extension == "bmp"
                    || Extension == "jpeg" || Extension == "jpg") 

            {


             <?php
 move_uploaded_file($_FILES["file"]["tmp_name"],
            "prof_pic/users/".$rows["username"]."/" . $_FILES["file"]["name"]);
        $db->query("UPDATE users SET prof_pic='".$_FILES['file']['name']."' WHERE username='".$_SESSION["logged_in"]."'");
        header("Location: account.php");
?>
            } 

//The file upload is NOT an image
else {
                alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");

            }
        }
    }

html部分

<input onchange="ValidateFileUpload(this);" type="file"  name="file"  id="filename"/>