【问题标题】:Error uploading files thorugh php script通过php脚本上传文件时出错
【发布时间】:2013-05-13 15:22:26
【问题描述】:

我在创建 php 文件上传脚本时遇到问题:

index.php:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>File Upload</title>
</head>

<body>
<form method="post" action="upload_file.php" enctype="multipart/form-data">
<label for="file"><h3>File: </h3> </label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="Upload" />
</form>

</body>

</html>

上传文件.php:

<?php
$restricted_extensions = array("htm", "html", "php", "asp", "aspx", "js");
$file_extension = end(explode(".", $_FILES["file"]["name"]));

if ($_FILES["file"]["error"] > 0)
{
    echo("<h2>An error occured!</h2>");
    echo("<h2>Return Code: " . $_FILES["file"]["error"] . "</h2><br>");
}
elseif ($_FILES["file"]["error"] == 0)
{
    $is_valid_extension = true;


    foreach ($restricted_extension as $extension => $value)
    {
        if ($file_extension == $extension)
        {
            $is_valid_extension == false;
        }

        if (!$is_valid_extension)
        {
            echo("<h2>The files extension " . $file_extension . " is not allowed!</h2>");
        }
    }
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
    {
        echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
        move_uploaded_file($_FILES["file"]["tmp_name"],
        "upload/" . $_FILES["file"]["name"]);
        echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
}
?>

错误消息:

注意:未定义的索引:第 3 行 C:\inetpub\wwwroot\upload_file.php 中的文件

严格标准:只有变量应该在第 3 行的 C:\inetpub\wwwroot\upload_file.php 中通过引用传递

注意:未定义的索引:C:\inetpub\wwwroot\upload_file.php 中第 5 行的文件

注意:未定义索引:第 10 行 C:\inetpub\wwwroot\upload_file.php 中的文件

注意:未定义变量:C:\inetpub\wwwroot\upload_file.php 第 15 行中的受限扩展

警告:在第 15 行的 C:\inetpub\wwwroot\upload_file.php 中为 foreach() 提供的参数无效

【问题讨论】:

  • upload_file.php 好像少了一部分。请发布整个文件以查看问题出在哪里Edit抱歉 - 没有滚动;)
  • 对于最后两个错误 - 你有一个错字(应该是restricted_extensions 而不是restricted_extension)。另外,请尝试将var_dump($_FILES); exit; 放在upload_file.php 的开头,并显示您尝试上传文件后得到的结果。

标签: php file-upload


【解决方案1】:

我无法重现这些错误,但是您在命名变量 $restricted_extensions 时有错字。请注意第一行末尾的“s”和代码后面缺少的“s”。

由于错误消息涉及未设置的数组键,因此您的上传似乎不起作用。由于您的 HTML 标记似乎没问题(并且它适用于我的设置),我怀疑它是一些服务器设置(Apache 或您使用什么?)或 php.ini 问题。请检查允许上传的相关设置。

【讨论】:

  • 我使用 Internet Information Services,我今天设置了所有内容,我认为没问题
【解决方案2】:

end(explode()) 的部分.. 你应该首先将explode() 结果分配给另一个变量,然后像这样将该变量传递给end() 函数,

$variable = explode(something..); $variable2 = end ($variable);

至于其他错误,也许这可能有助于解决其中的一些问题..

顺便说一句,它对我有用,我没有时间知道为什么它真的很快。当时:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多