【问题标题】:Upload a file to a website using php使用 php 将文件上传到网站
【发布时间】:2015-01-09 00:07:21
【问题描述】:

我是 php 新手,我正在尝试从这里修改代码:http://www.w3schools.com/php/php_file_upload.asp 文件上传后保持在同一页面上。

页面显示这些错误(文件上传前但文件上传后没有):

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\test-site\index.php on line 12
Sorry, file already exists.

Notice: Undefined index: fileToUpload in C:\xampp\htdocs\test-site\index.php on line 38
Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.

文件上传正常。

这是代码:

<!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>

【问题讨论】:

  • 在代码开始执行之前,你必须检查$_FILES数组是否设置了isset()函数。
  • 你不能从扩展名中分辨出文件类型,顺便说一句,你在这里犯了一个大错误。

标签: php


【解决方案1】:

你需要移动这个块

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

条件之后的所有内容都进入if(isset($_POST["submit"])) 条件。

第一次下载页面时,$_FILES 为空,但你想使用它。

【讨论】:

    【解决方案2】:

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

    <!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>
    

    【讨论】:

    • 太棒了,谢谢。我了解它是如何发送这些错误的。我被困在这个问题上太久了
    【解决方案3】:

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

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

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

    【讨论】:

    • OP 使用method="post"。问题是,他想在条件外使用$_FILES
    • @lolka_bolka 是的,我刚刚注意到。不过,这仍然是一种更清洁的方法,所以我就让它保持不变。
    【解决方案4】:

    看看你的代码行:

    $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 = '';
    }
    

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

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多