使用php将文件上传到网站

时间:2015-01-08 15:56:55

标签: php

我是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>

4 个答案:

答案 0 :(得分: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为空,但您想要使用它。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

查看您的代码行:

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

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