doujieju0397 2016-07-27 09:15 采纳率: 0%
浏览 26
已采纳

PHP - 上传文件时出错

I recently wrote code in php to upload image/file via move_upload_file() like this:

if (isset($_POST["title"]) && isset($_POST["content"]) && isset($_POST["category"])) {

        //if (!isset($_POST[]))
        $title = $_POST['title'];
        $desc = $_POST['content'];
        $lat = $_POST['latitude'];
        $long = $_POST['longitude'];
        $category = $_POST['category'];
        $image = $_POST['userfile'];

        if(isset($_FILES['userfile'])) {

            $fileName = $_FILES['Filename']['name'];
            $target = "uploads/"; 
            $fileTarget = $target.$fileName; 
            $tempFileName = $_FILES["Filename"]["tmp_name"];

            $result = move_uploaded_file($_FILES["Filename"]["tmp_name"],$fileTarget);

            /*
            *   If file was successfully uploaded in the destination folder
            */
            if($result) { 
                header('Location: post.php?success'); 
                $query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
                $link->query($query) or die("Error : ".mysqli_error($link)); 
                }
            else { 
                header('Location: post.php?errimg'); 
                }
            mysqli_close($link);
        }
        else {
            $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')"; 

            $result = mysqli_query($link, $query);

            if ($result) {
                header('Location: post.php?success');
            }

            else {
                header('Location: post.php?error');
            }
        }
        // $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";   
    }

and the form like this in html

<form enctype="multipart/form-data" action="getPublicInfo.php" method="post" class="form">
                <?php if($result) {echo $hasil;} ?>            
                <div class="form-group">
                    <label> Judul </label>
                        <input type="text" name="title" class="form-control" placeholder="title" required/>
                </div>
                <div class="form-group">
                    <label> Description </label>
                        <textarea type="text" name="content" class="form-control" required rows="3"> </textarea>
                </div>
                <div class="form-group">
                    <label> Pick a category </label><br>
                        <label class="radio-inline">
                            <input type="radio" name="category" value="1"/> Headlines
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="category" value="2"/> Event
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="category" value="3" checked/> Info lain
                        </label>
                </div>
                <!--store image-->
                <input name="MAX_FILE_SIZE" value="10000000" type="hidden">
                <div class="form-group">
                    <label> Upload an image </label>
                        <input name="userfile" type="file">
                </div>
                <input value="Submit" type="submit" class="btn btn-default">
            </form>

but this code shows the Location: post.php?errimg that actually is going to pop up an error. Anyone can help? Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • douyi0219 2016-07-27 09:19
    关注

    you have used the wrong variable HERE:

    $fileName = $_FILES['Filename']['name'];
    $target = "uploads/"; 
    $fileTarget = $target.$fileName; 
    $tempFileName = $_FILES["Filename"]["tmp_name"];
    $result = move_uploaded_file($_FILES["Filename"]["tmp_name"],$fileTarget);
    

    USE:

    $fileName = $_FILES['userfile']['name'];
    $target = "uploads/"; 
    $fileTarget = $target.$fileName; 
    $tempFileName = $_FILES["userfile"]["tmp_name"];
    
    $result = move_uploaded_file($_FILES["userfile"]["tmp_name"],$fileTarget);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮