上载SWF文件时限制检查条件

时间:2012-04-14 07:44:17

标签: php flash

我正在创建一个使用PHP在文件夹中上传swf文件的应用程序。 我的脚本全部工作,除了我检查扩展名是否为swf的第一个if条件,但我似乎有一些错误。

我不确定video/swf是否是SWF文件的有效检查参数。我的完整脚本如下。我正在使用getimagesize()检查SWF的大小。有些人可能想知道getimagesize适用于图像,但我看到一些例子,其中getimagesize()已用于获取SWF文件的大小。

它给我的消息是“无效的swf文件”,这意味着它根本不满足第一个检查条件。

<?php
        foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
        {

        list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);

        if (( ($_FILES["item_swf"]["type"][$key] == "video/swf") || ($_FILES["item_swf"]["type"][$key] == "video/SWF") )
        && ($_FILES["item_swf"]["size"][$key] < 800000))
           {
                if ($_FILES["item_swf"]["error"][$key] > 0)
                  {
                     echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
                  }
                else if($width==1000 && $height==328)
                  {
                   if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
                            {
                               echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
                             }
                   else
                            { 

                               move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
                               echo "done";
                        }
                  }
             else 
                 {
                    echo "size doest permit";
                 }  
            }
        else
            {
               echo "Not a valid swf file::";
            }                       

        }
        ?>

下面给出的行

move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);

工作正常,因为它将文件上传到专用文件夹,似乎某些SWF文件的检查参数设置不正确。

修改

我得到了答案。我没有使用video/swf,而是使用application/x-shockwave-flash

所以最终的代码是:

<?php
        foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
        {

        list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);

        if (($_FILES["item_swf"]["type"][$key] == "application/x-shockwave-flash") 
        && ($_FILES["item_swf"]["size"][$key] < 800000))
           {
                if ($_FILES["item_swf"]["error"][$key] > 0)
                  {
                     echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
                  }
                else if($width==1000 && $height==328)
                  {
                   if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
                            {
                               echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
                             }
                   else
                            { 

                               move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
                               echo "done";
                        }
                  }
             else 
                 {
                    echo "size doest permit";
                 }  
            }
        else
            {
               echo "Not a valid swf file::";
            }                       

        }
        ?>

2 个答案:

答案 0 :(得分:1)

你可以尝试

$savePath = "PATH_TO_SAVE";
$errors = array ();
$output = array ();
//

if (isset ( $_FILES ['item_swf'])) {

    foreach ( $_FILES ['item_swf'] ['tmp_name'] as $key => $val ) {

        $fileName = $_FILES ['item_swf'] ['name'] [$key];
        $fileSize = $_FILES ['item_swf'] ['size'] [$key];
        $fileTemp = $_FILES ['item_swf'] ['tmp_name'] [$key];

        $fileExtention = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExtention = strtolower ( $fileExtention );

        if ($fileExtention != ".swf") {
            $errors [$fileName] [] = "Invalid File Extention";
            continue;
        }

        if ($fileSize > 800000) {
            $errors [$fileName] [] = "File Too large";
            continue;
        }

        list ( $width, $height ) = getimagesize ( $fileTemp );

        if ($width != 1000 && $height != 328) {
            $errors [$fileName] [] = "Wrong File dimention ";
            continue;
        }

        if (file_exists ( $savePath . DIRECTORY_SEPARATOR . $fileName )) {
            $errors [$fileName] [] = "File Exist";
            continue;
        }

        if(!is_writable($savePath ))
        {
            $errors [$fileName] [] = "File Destination not writeable";
        }


        if(count($errors [$fileName]) == 0)
        {
            if(@move_uploaded_file ( $fileTemp, $savePath . DIRECTORY_SEPARATOR . $fileName))
            {
                $output[$fileName] == "OK" ;
            }
            else
            {
                $errors [$fileName] [] = "Error Saving File";
            }

        }


    }
        var_dump($errors, $output);
}

如果您有任何更多挑战,请告诉我

答案 1 :(得分:0)

好的,我得到了答案....

而不是使用video/swf我需要使用application/x-shockwave-flash

所以终极代码将是

<?php
        foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
        {

        list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);

        if (( ($_FILES["item_swf"]["type"][$key] == "application/x-shockwave-flash") || ($_FILES["item_swf"]["type"][$key] == "video/SWF") )
        && ($_FILES["item_swf"]["size"][$key] < 800000))
           {
                if ($_FILES["item_swf"]["error"][$key] > 0)
                  {
                     echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
                  }
                else if($width==1000 && $height==328)
                  {
                   if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
                            {
                               echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
                             }
                   else
                            { 

                               move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
                               echo "done";
                        }
                  }
             else 
                 {
                    echo "size doest permit";
                 }  
            }
        else
            {
               echo "Not a valid swf file::";
            }                       

        }
        ?>