PHP限制文件类型上传

时间:2014-08-24 20:11:26

标签: php file web upload

我试图创建一个表单,允许我通过php将html文件上传到web目录,然后在我的服务器上24小时后删除它们。每当我上传文件时,我都会得到一个空白的白页。有人可以帮我解决这个问题吗?非常感谢!

<?php 
if (($_FILES["uploaded"]["type"] == "html")
{
    $target = "users/"; 
    $target = $target . basename( $_FILES['uploaded']['name']) ; 
    $ok=1; 

    echo "File: " . $_FILES["uploaded"]["name"] . "<br />";
    echo "Type: " . $_FILES["uploaded"]["type"] . "<br />";
    echo "Size: " . ($_FILES["uploaded"]["size"] / 1024) . " Kb<br />";
    echo "Location: users/" . $_FILES["uploaded"]["name"];

    $files=shell_exec('find /users/ -mmin +1440');
    $file = explode("\n",$files);
    if(isset($file) && is_array($file))
    {
        foreach($file as $val)
        {
            @unlink($val);
        }
    }
}

else 
{
    echo "Sorry, " . $_FILES["uploaded"]["name"] . " is not a valid html document. Please try again.";
    unlink . $_FILES["uploaded"]["name"];
}
?> 

1 个答案:

答案 0 :(得分:0)

有一个缺少的括号关闭第一个IF。然后在最后一行unlink . $_FILES["uploaded"]["name"];你可能意味着unlink() 空白页让我觉得你有错误报告。编码时将其打开:)

<?php 
if(($_FILES["uploaded"]["type"] == "html")){
    $target = "users/"; 
    $target = $target . basename( $_FILES['uploaded']['name']) ; 
    $ok=1; 

    echo "File: " . $_FILES["uploaded"]["name"] . "<br />";
    echo "Type: " . $_FILES["uploaded"]["type"] . "<br />";
    echo "Size: " . ($_FILES["uploaded"]["size"] / 1024) . " Kb<br />";
    echo "Location: users/" . $_FILES["uploaded"]["name"];

    $files=shell_exec('find /users/ -mmin +1440');
    $file = explode("\n",$files);
    if(isset($file) && is_array($file))
    {
        foreach($file as $val)
        {
            @unlink($val);
        }
    }
}

else {
    echo "Sorry, " . $_FILES["uploaded"]["name"] . " is not a valid html document. Please try again.";
    unlink($_FILES["uploaded"]["name"]);
}
?>