我似乎无法弄清楚为什么我的图片上传脚本无法正常工作

时间:2014-04-23 05:19:13

标签: php mysql

所以我花了大部分时间来处理将图像上传到数据库,我决定找到一个预制的图像上传脚本。根据我的需要调整后,我似乎无法弄清楚它为什么不起作用。

表:

    CREATE TABLE `proj_images` (
      `id` int(100) NOT NULL AUTO_INCREMENT,
      `name` mediumtext,
      `type` varchar(45) DEFAULT NULL,
      `project_id` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

形式:

    <form name="form" method="POST" action="inc/editproc.php">
        <h4>Project Name:</h4>
        <input name="projectname" type="text" class="form-control" value="<?php echo $projectname ?>">
        <br><br><br><br><br>
        <textarea id="description" class="form-control" name="article-body" style="height: 200px"><?php echo $description ?></textarea>
        <br>

    <input type="file" name="photo" accept="image/*">

    <div align="center">
            <input name="hiddenField" type="hidden" value="add_n">
            <input name="pid" type="hidden" value="<?php echo $pid; ?>">
            <input class="btn btn-success" name="add" type="submit" id="add" value="Submit">
    </div>

</form>

脚本(editproc.php中有什么):

    if (isset($_FILES["photo"]["name"]) && isset($_POST["pid"])){
        $projectid = $_POST['pid'];
        $q = ("SELECT COUNT(id) FROM  WHERE user=$$projectid");
        $query = $link->prepare($q);
        $query->execute();
        $count = $query->fetchColumn();
        if($$count > 8){
            exit();
        }
        $gallery = preg_replace('#[^a-z 0-9,]#i', '', $_POST["pid"]);
        $fileName = $_FILES["photo"]["name"];
        $fileTmpLoc = $_FILES["photo"]["tmp_name"];
        $fileType = $_FILES["photo"]["type"];
        $fileSize = $_FILES["photo"]["size"];
        $fileErrorMsg = $_FILES["photo"]["error"];
        $kaboom = explode(".", $fileName);
        $fileExt = end($kaboom);
        $db_file_name = date("r",hexdec(substr(uniqid(),0,8)))."".rand(1000,9999).".".$fileExt;
        list($width, $height) = getimagesize($fileTmpLoc);
        if($width < 10 || $height < 10){
            header("location: ../message.php?msg=ERROR: That image has no dimensions");
            exit();
        }
        if($fileSize > 1048576) {
            header("location: ../message.php?msg=ERROR: Your image file was larger than 1mb");
            exit();
        } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
            header("location: ../message.php?msg=ERROR: Your image file was not jpg, gif or png type");
            exit();
        } else if ($fileErrorMsg == 1) {
            header("location: ../message.php?msg=ERROR: An unknown error occurred");
            exit();
        }
        $moveResult = move_uploaded_file($fileTmpLoc, "../pblc/pimg/$projectid/$db_file_name");
        if ($moveResult != true) {
            header("location: ../message.php?msg=ERROR: File upload failed");
            exit();
        }

        $q = ("INSERT INTO proj_images(filename, type, project_id) VALUES ('$db_file_name','$fileType','$projectid')");
        $query = $link->prepare($q);
        $query->execute();
        header("location: ../project.php?id=$projectid");
        exit();
    }

1 个答案:

答案 0 :(得分:0)

更改

<form name="form" method="POST" action="inc/editproc.php">

<form name="form" method="POST" action="inc/editproc.php" enctype="multipart/form-data">

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2州:

  

内容类型“application / x-www-form-urlencoded”对于发送大量二进制数据或包含非ASCII字符的文本效率不高。 内容类型“multipart / form-data”应用于提交包含文件,非ASCII数据和二进制数据的表单