duan2891 2017-08-30 06:41
浏览 57
已采纳

PHP文件上传图片不起作用

I am trying to upload a image in my db but it won't work.

here is the code to upload the image.

<?php
    $con = mysqli_connect("localhost", "root", "", "test");
    mysqli_select_db("test", $con);

    $username = $_POST['username'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $no = $_POST['no'];

    if(isset($_POST['submit'])) {

        if(getimagesize($_FILES['image']['tmp_name']) == FALSE) {
            echo "Please select an image.";
        }

        else {
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["image"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

            // Check file size
            if ($_FILES["image"]["size"] > 500000) {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }

            // Check if file already exists
            if (file_exists($target_file)) {
                echo "<p>Sorry, file already exists.</p>";
                $uploadOk = 0;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "<p>Sorry, your file was not uploaded.</p>";
            }

            // if everything is ok, try to upload file
            else {

                if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
                    $name = basename( $_FILES["image"]["name"]);
                    $image = "uploads/".basename( $_FILES["image"]["name"]);
                    echo "<p>".$name. " has been uploaded.</p>";
                    header("location: image.php");

                    $qry = "INSERT INTO images (id, name, image, username, password, dob, no) VALUES (NULL, '".$name."', '".$image."', '".$username."', '".$password."', '".$dob."', '".$no."')";
                    $result = mysqli_query($con, $qry);

                    //$sql = "INSERT INTO images (username) VALUES ('$username')";
                    //$result = mysqli_query($con, $sql);

                } 

                else {
                    echo "<p>Sorry, there was an error uploading your file.</p>";
                }
            }
        }
     }
 ?>

it always said that sorry there was an error uploading the file.

and this is the error in the server log.

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpRGn6S7' to 'uploads/Screenshot from 2017-08-09 16-57-58.png' in /var/www/html/practice1/images2.php on line 45, referer: http://localhost/practice1/image.php

I am new to file uploading in PHP. would really appreciate if someone can help. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongyang1518 2017-08-30 07:20
    关注

    On Windows and Linux, the move_uploaded_file() function doesn't work the same. On Linux, you need to put the full path like this :

    $target_dir = "/var/www/html/practice1/uploads/";
    

    Or seperate project_dir and upload dir like this :

    $project_dir = '/var/www/html/practice1/';
    $target_dir  = $project_dir . 'uploads/';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用