用PHP代码上传php文件类型

时间:2014-11-27 08:26:55

标签: php

我有以下代码,我将不同的文件类型上传到我的服务器上。这些主要是图片文件和PDF格式。我现在正在尝试上传.php文件但是我收到了错误。

$folder      = $_POST['folder_name'];
$allowedExts = array("gif", "jpeg", "jpg", "png", "pdf", "php");
$temp        = explode(".", $_FILES["file"]["name"]);
$extension   = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "application/pdf")
        || ($_FILES["file"]["type"] == "application/php")
        || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts)
) {
    if ($_FILES["file"]["error"] > 0) {
        echo "
<div style='position:absolute;left:50%;top:50%;margin-top:-80px;margin-left:-80px;width:300px;height:200px;'>";
        echo "<br><br><span style='font-weight:bold;text-decoration:underline;'>Return Code: " . $_FILES["file"]["error"] . "</span><br>";
        echo "<br><br><input type=\"submit\" value=\"Back to CMS\" onclick=\"location.href='index_upload.php'\">";
    } else {
        echo "
    <div style='position:absolute;left:50%;top:50%;margin-top:-80px;margin-    left:-80px;width:300px;height:200px;'>";
        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($folder . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " <br><br><span style='font-weight:bold;text-    decoration:underline;'>already exists</span>.
        ";
            echo "<br><br><input type=\"submit\" value=\"Back to CMS\" onclick=\"location.href='index_upload.php'\">";
        } else {
            move_uploaded_file(
                $_FILES["file"]["tmp_name"],
                $folder . $_FILES["file"]["name"]
            );
            echo "<br><span style='font-weight:bold;text-decoration:underline;'>Stored in:</span> " . $folder .
                $_FILES["file"]["name"];
            echo "<br><br><input type=\"submit\" value=\"Back to CMS\" onclick=\"location.href='index_upload.php'\">";
            echo "
    </div>
    ";
        }
    }
} else {
    echo "
    <div style='position:absolute;left:50%;top:50%;margin-top:-50px;margin-left:-50px;width:200px;height:200px;'>";
    echo "<span style='font-weight:bold;text-decoration:underline;'>Invalid file</span>";
    echo "<br><br><input type=\"submit\" value=\"Back to CMS\" onclick=\"location.href='index_upload.php'\">";
    echo "
    </div>
    ";
}

我收到最后一个错误show INVALID FILE。

1 个答案:

答案 0 :(得分:0)

在我尝试完相同的任务后,我完成了本教程,请看一下。

Upload File, image or Videos

  1. 请缩进代码,易于阅读并发现错误。
  2. 我们可以检查发生了什么错误以及原因。

    $_FILES["file"]["error"]给出了可以从文档中解码的错误代码。

  3. 我使用了文件扩展名finfo(必须通过在php.ini文件中进行更改才能激活),我个人认为这是检查多部分表单数据的MIME类型的更好方法。

  4. 我还提到错误,因为最大文件上传大小,执行时间等参数在php.ini中设置为某个默认值,必须进行更改才能使此操作成功。

  5. 在阅读本文之后,您始终可以要求任何让您感到困惑的事情或任何错误。

相关问题