PHP文件上传图片不起作用

亚兹

我正在尝试在我的数据库中上传图像,但它不起作用。

这是上传图片的代码。

<?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>";
                }
            }
        }
     }
 ?>

它总是说对不起,上传文件时出错。

这是服务器日志中的错误。

PHP 警告:move_uploaded_file():无法将“/tmp/phpRGn6S7”移动到“上传/截图自 2017-08-09 16-57-58.png”在 /var/www/html/practice1/images2.php 在线45、referer:http://localhost/practice1/image.php

我是用 PHP 上传文件的新手。如果有人可以提供帮助,将不胜感激。提前致谢。

忧郁症

在 Windows 和 Linux 上,move_uploaded_file() 函数的工作方式不同在 Linux 上,您需要像这样放置完整路径:

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

或者像这样分开 project_dir 和上传目录:

$project_dir = '/var/www/html/practice1/';
$target_dir  = $project_dir . 'uploads/';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章