【问题标题】:How to store images into files using SQL?如何使用 SQL 将图像存储到文件中?
【发布时间】:2017-05-08 11:37:15
【问题描述】:
<?php

    if(isset($_POST['hour'])&&isset($_POST['day'])){

        //the path to store the uploaded image.
        $target = "images/".basename($_FILES['image']['name']);

        $db = mysqli_connect("localhost","root","","a_database");

        $image = $_FILES['image']['name'];
        $carmodel = $_POST['carmodel'];
        $cartype = $_POST['cartype'];
        $hour = $_POST['hour'];
        $day = $_POST['day'];

        $query = "INSERT INTO `cardetails` (`image`, `carmodel`, `cartype`, `hour`, `day`) VALUES ('".$image."', '".$carmodel."', '".$cartype."', '".$hour."', '".$day."')";
        mysqli_query($db,$query);//stores the data

        //move uploaded image into the folder.
        if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){
             header('Location:upload_success.php');
        }else{

             header('Location:upload_fail.php');
        }

    }else{
            echo ' ';
    }

?>


<div class="col-lg-offset-4">
    <form method="POST" action="upload.php" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
    <div class="form-group row">
        <input type="file" name="image">
    </div>

    <div class="form-group col-lg-4 row">
        <label> Car Model </label>
        <select name="carmodel" class="form-control" >
        <option value="Myvi">Myvi</option>
        <option value="Vios">Vios</option>
        <option value="Lambo">Lambo</option>
        </select>
    </div>

    <div class="form-group row col-lg-offset-4">
    <label> Car Type </label>
        <input type="radio" name="cartype" value="Auto">Auto 
        <input type="radio" name="cartype" value="Manual"> Manual 
    </div> 

    <div class="form-group">
        <input type="text" name="hour" placeholder="$/hour">
    </div>

    <div class="form-group">
        <input type="text" name="day" placeholder="$/day">
    </div>

    <div class="form-group">
        <button type="submit">Upload</button>
    </div>

</form>

</div>

我已经成功地将数据插入到带有图像名称的数据库中,但是图像似乎没有上传到我的图像文件中,可能是什么问题?根据上面的代码,调用了upload_fail.php。如果使用 $_POST['image'] 设置,则不会发生任何事情。

【问题讨论】:

  • 你的完整图片路径是什么?你给 755 权限了吗?
  • move_uploaded_file() 如果发生错误则返回 false,并发出警告。也许您应该查看您的 Apache 错误日志以查看警告是什么?或者打开错误输出(在你的 php.ini 中 display_errors = On),这样你就可以直接看到输出了。
  • 我很久以前也遇到过类似的问题,是因为我没有临时文件夹的读/写权限。

标签: php mysql image-uploading


【解决方案1】:

我认为,您没有足够的权限写入images 目录。 images 目录本身是否存在?

如果你想知道哪里出了问题,你可以在你的 php 文件的开头启用错误报告,例如:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);   
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-23
    • 2017-06-19
    • 2023-03-06
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多