【问题标题】:PHP, using $_FILES and $_POST at the same time but its not workingPHP,同时使用 $_FILES 和 $_POST 但它不起作用
【发布时间】:2018-04-23 12:35:54
【问题描述】:

我正在制作一个管理页面以在 MySQL 中插入所有属性并将图像上传到一个文件夹中,但出现一些错误,这是表单代码

<form role="form" id="form1" action="" enctype="application/x-www-form-urlencoded"  method="post">
    <div class="col-lg-6">
        <div class="form-group">
            <label>Title:</label>
            <input type="text" name="title" placeholder="enter title" class="form-control"required>
        </div>
        <div class="form-group">
            <div class="col-lg-6 col-md-6 col-sm-6">  
                <label>Image:</label>
                <input type="file" name="upload" id="postimage" class="form-control"> 
            </div>
        </div>
        <input type="reset" name="reset" value="reset">
    </div>
    <div class="col-lg-6">
        <div class="form-group">
            <label>Content:</label>
            <input type="text" class="form-control" name="content" placeholder="explain in brief">
        </div>
        <div class="form-group">
            <label>Age:</label>
            <input type="text" class="form-control" placeholder="enter require age" name="age">
        </div>
        <div class="form-group">
            <input type="submit" name="submitpost" value="Submit-Post" class="form-control">
        </div>
    </div>
</form>

当我使用 PHP 代码将我的图像插入一个文件夹名称 img 和 MySQL 表中的其他属性时。

<?php
    $target_dir = "../../img/";
    if (isset($_FILES['upload'])and isset($_POST['submitpost'])) {   
        $target_file = $target_dir . basename($_FILES["upload"]["name"]);
            if(move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)){
                $title=$_POST['title'];
                $age=$_POST['age'];
                $content=$_POST['content'];
                $query="INSERT INTO `posts`
                            (`id`,`title`, `image`,`age`, `content`) 
                    VALUES (null,'$title','$target_file','$age','$content')"; 
                if(mysqli_query($con,$query))
                {
                    echo "<script>alert('passed')</script>";
                }else{
                    echo "<script>alert('not passed')</script>";
                }
            }else{
                echo "<script>alert('problem with image upload')</script>";}
        }
    ?>

但是提交后我什么也没得到,既没有错误也没有任何通知,我不知道有什么问题可以帮助我,我确实尝试了一些其他代码,然后将其插入表中,但图像没有上传到目标文件夹中。帮助我通过它我是 PHP 新手,所以

【问题讨论】:

  • DEBUGGINGini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 添加到脚本顶部。这将强制任何mysqli_ 错误生成一个您可以在浏览器上看到的异常,并且其他错误也将在您的浏览器上可见。
  • 一些合理的代码缩进是个好主意。它可以帮助我们阅读代码,更重要的是,它将帮助 您调试代码 Take a quick look at a coding standard 为您自己谋福利。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
  • 使用$target_dir = "../img";而不是$target_dir = "../../img/";这是上传文件到img文件夹的正确路径
  • 你的表单必须像&lt;form method="post" enctype="multipart/form-data"&gt;
  • 尝试 print_r($_POST) 检查表单是否发布任何值

标签: php html file-upload


【解决方案1】:

您在表格中添加了enctype="application/x-www-form-urlencoded" 用于文件输入,错误。像下面这样添加

<form role="form" id="form1" action="" enctype="multipart/form-data" method="post">

添加 PHP 之类的代码

$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/img/';
if(!empty($_FILES['upload']['name'] && isset($_POST['submitpost']))){
    $target_file = $target_dir . basename($_FILES["upload"]["name"]);
    if(move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)){
        extract($_POST);
        $title = mysqli_real_escape_string($con,$title);
        $age = mysqli_real_escape_string($con,$age);
        $content = mysqli_real_escape_string($con,$content);
        $query = "INSERT INTO `posts` (`id`,`title`,`image`,`age`,`content`) 
            VALUES (null,'{$title}','{$target_file}','{$age}','{$content}');";
        if(mysqli_query($con,$query)){
            echo "<script>alert('passed')</script>";
        }else{
            echo "<script>alert('not passed')</script>";
        }   
    }else{
        echo "<script>alert('problem with image upload')</script>";
    }
}

请确保$target_dir 是您图片上传目录的正确路径。

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2011-12-12
    相关资源
    最近更新 更多