简体   繁体   English

将文件上传到 PHP 时为空 $_FILES 数组

[英]Empty $_FILES array when uploading file to PHP

As mentioned in the title uploading a file to the server fails with different warnings.如标题中所述,将文件上传到服务器失败并显示不同的警告。

PHP code: PHP 代码:

<?php
function getBack()
{
    echo "<script>window.location = 'newimage.php';</script>";
    die;
}
if(isset($_POST["submit"])) {

    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "projectphp";
    $link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    if(mysqli_connect_error()){
        setcookie("error", "Problems with SQL connection", time() + 3600, "/");
        getBack();
    }
    if (getimagesize($_FILES['imagefile']['tmp_name']) == false) {
        setcookie("error", "Please Select An Image", time() + 3600, "/");
        //getBack();
    }

    $image = $_FILES["imagefile"]["tmp_name"];
    $name = $_FILES["imagefile"]["name"];
    $image = base64_encode(file_get_contents(addslashes($image)));

    $sql = "INSERT INTO user (photo) VALUES ('$name', '$image')";

    if (mysqli_query($link, $sql)) {
        echo "Image uploaded successfully.";
    } else {
        echo "Image Failed to upload";
    }
}
?>

HTML part: HTML 部分:

<html>
<head>
    <title></title>
    <link type="text/css" href="style.css" rel="stylesheet">
</head>
<body>
<!--- Header -->
<?php //include "headconfig.php"?>
<!--- Main --->
<div class="main">
    <div class="inner_main">
        <div class="form">
            <form action="" enctype=”multipart/form-data” method="post">
                <?php
                if(isset($_COOKIE["error"])){
                    echo "<span style='color: red'>".$_COOKIE["error"]."</span><br>";
                    setcookie("error", "", time()-3600, "/");
                }
                ?>
                <input type="file" name="imagefile" class="input" style="border: none" id="imagefile">
                <input type="submit" class="button" value="Upload" name="submit">
            </form>
        </div>
    </div>
</div>
<!--- Footer --->
<div class="footer">

</div>
</body>
</html>

When uploading a file these warnings and errors can be observed:上传文件时,可以观察到这些警告和错误:

Notice: Undefined index: imagefile in C:\xampp\htdocs\ProjectPHP\newimage.php on line 18注意:未定义索引:第 18 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中的图像文件

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ProjectPHP\newimage.php on line 18注意:尝试访问第 18 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中 null 类型值的数组偏移量

Warning: getimagesize(): Filename cannot be empty in C:\xampp\htdocs\ProjectPHP\newimage.php on line 18警告:getimagesize():第 18 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中的文件名不能为空

Notice: Undefined index: imagefile in C:\xampp\htdocs\ProjectPHP\newimage.php on line 23注意:未定义索引:第 23 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中的图像文件

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ProjectPHP\newimage.php on line 23注意:尝试访问第 23 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中 null 类型值的数组偏移量

Notice: Undefined index: imagefile in C:\xampp\htdocs\ProjectPHP\newimage.php on line 24注意:未定义索引:第 24 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中的图像文件

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\ProjectPHP\newimage.php on line 24注意:尝试访问第 24 行 C:\xampp\htdocs\ProjectPHP\newimage.php 中 null 类型值的数组偏移量

Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\ProjectPHP\newimage.php on line 25 Image Failed to upload警告:file_get_contents(): 第 25 行的 C:\xampp\htdocs\ProjectPHP\newimage.php 中的文件名不能为空 图像上传失败

Passed parameter should be a non-empty array.传递的参数应该是一个非空数组。

Filename cannot be empty in C:\xampp\htdocs\ProjectPHP\newimage.php on line 18, which means that the array you're passing it's null and has nothing inside make sure the file you're uploading is getting in array. Filename cannot be empty in C:\xampp\htdocs\ProjectPHP\newimage.php on line 18, which means that the array you're passing it's null and has nothing inside make sure the file you're uploading is getting in array.

comment this:评论这个:

/* if (getimagesize($_FILES['imagefile']['tmp_name']) == false) {
    setcookie("error", "Please Select An Image", time() + 3600, 
    "/");
    //getBack();
} */

try var_dump($image);尝试 var_dump($image); to see the variable information.查看变量信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM