使用php将文件上载到网站

使用php将文件上载到网站,php,Php,我是php新手,正在尝试从这里修改代码: 上传文件后保持在同一页面 页面显示这些错误(在上载文件之前,但不是在上载文件之后): 不管怎样,文件都可以正常上传 代码如下: <!DOCTYPE html> <html> <body> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> Select ima

我是php新手,正在尝试从这里修改代码: 上传文件后保持在同一页面

页面显示这些错误(在上载文件之前,但不是在上载文件之后):

不管怎样,文件都可以正常上传

代码如下:

<!DOCTYPE html>
<html>
<body>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload" class="upload_file">
    <input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) 
{
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) 
    {
        echo "File is an image - " . $check["mime"] . ".";
        echo '<a href="'.$target_file.'">Download you file here</a>';
        $uploadOk = 1;
    } 
    else 
    {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) 
{
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) 
{
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&&         $imageFileType != "gif" ) 
{
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) 
{
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} 
else 
{
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
    {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } 
    else 
    {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
</body>
</html>

检查此代码-它现在不应该抛出错误

<!DOCTYPE html>
<html>
<body>

    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload" class="upload_file">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    <?php

    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) 
    {

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

        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) 
        {
            echo "File is an image - " . $check["mime"] . ".";
            echo '<a href="'.$target_file.'">Download you file here</a>';
            $uploadOk = 1;
        } 
        else 
        {
            echo "File is not an image.";
            $uploadOk = 0;
        }

        // Check if file already exists
        if (file_exists($target_file)) 
        {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 500000) 
        {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&&         $imageFileType != "gif" ) 
        {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
            echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } 
        else 
        {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
            {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            } 
            else 
            {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }
    ?>
</body>
</html>

您需要移动此块

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
并将条件之后的所有内容放入
if(isset($\u POST[“submit”])
条件


首次下载页面时,
$\u文件是空的,但您希望使用它。

您应该在代码中区分何时通过GET加载页面(加载表单)以及何时发出POST请求以及何时需要处理上载。这适用于与文件上载相关的所有事项

我通常也会将所有处理放在顶部,如果您想重定向,例如在成功发布后,这可能会很有用:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
  // your upload processing code
}
else
{
  // show your form
}

查看您的代码行:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
替换为:

if (isset($_FILES["fileToUpload"]["name"])) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
} else {
var_dump($_FILES);
$target_file = '';
}

这不是一个完整的解决方案,代码中可能会有许多其他错误,但只是从某个地方开始

在代码开始执行之前,您必须检查
$\u FILES
数组是否已设置或未使用
isset()
函数。顺便说一句,您无法区分文件类型及其扩展名,你犯了一个大错误。请使用
method=“post”
。问题是,他想在条件之外使用
$\u文件。@lolka\u bolka是的,我刚刚注意到。这仍然是一个更干净的方法,所以我会让它继续下去。太棒了,谢谢。我理解它是如何发送这些错误的。我在这个问题上陷得太久了
if (isset($_FILES["fileToUpload"]["name"])) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
} else {
var_dump($_FILES);
$target_file = '';
}