【问题标题】:PHP upload script will not upload flash files. Why?PHP 上传脚本不会上传 Flash 文件。为什么?
【发布时间】:2016-01-13 11:50:58
【问题描述】:

我在尝试使此上传脚本正常工作时遇到问题。它不上传 .swf,即使脚本本身应该可以工作,我使用了图像上传器的脚本并对其进行了编辑,因此它应该可以与 .swf 文件一起使用,但 PHP 中似乎存在不允许的问题这些类型的文件要上传。

谁能帮我解决这个问题?

这是html

    <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select game (*.swf) to upload and add to the list of games:
    <input type="file" name="gameToUpload" id="gameToUpload">
    <input type="text" name="nameOfGame" id="nameOfGame">
    <input type="submit" value="Upload Game" name="submit">
</form>

</body>
</html> 

这就是 PHP

<?php

    include('init.php');

    //Reminder to self: Turn off when finished coding!!
    error_reporting(-1);     

    //Some querys and variables used later on
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["gameToUpload"]["name"]);
    $uploadOk = 1;
    $gameFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $gameName = $_POST["nameOfGame"];
    $query = "INSERT INTO `beoordelingen`.`beoordeling` (`ID`, `file`, `Spel`) VALUES (NULL, '{$target_file}', '{$gameName}')";

    //If submit is pressed
    if (isset($_POST['submit'])) {

        $check = $_FILES["gameToUpload"]["tmp_name"];

        //Check if file already exists in folder
        if (file_exists($target_file)) {
            echo "This game is already in the folder.";
            $uploadOk = 0;
        }
        //Check if the file type is .SWF
    if ($gameFileType != "swf") {
            echo "Sorry, only .swf is allowed for this page.";
            $uploadOk = 0;
        }
        //When any errors occured do not go passed this statement
        if ($uploadOk === 0) {
            echo "Sorry, your file wasn't uploaded.";
        }
    //If no errors have occured then continue with this
    else {
        //Move the file from the temporary folder to the final destination
        if (move_uploaded_file($_FILES["gameToUpload"]["tmp_name"], $target_file)) {
            echo "The file " . basename($_FILES["gameToUpload"]["name"]). " has been uploaded.";
            //Update the mysql database
            $conn->query($query);
        }
        //If something bad happened while trying to move the file. Show this message to the user.
        else {
            echo "Sorry, there was an error uploading the file.";
        }
      }
    }

?>

【问题讨论】:

  • 你得到什么错误?
  • 可能是因为这种情况:if ($gameFileType != "swf") { ... }
  • @Dennis1679 假设您正在尝试上传文件 MyGame.SWF 。文件扩展名将是“SWF”而不是“swf”(记住字符串在 php 中区分大小写)。比较两个字符串的正确方法是:if( strcasecmp($gameFileType, "swf") === 0 ) { /* Is a swf file */ }
  • @CarlosAlbertoB.Carucce 好的,这可能是未来的问题,所以谢谢。但在这种情况下,我上传的文件具有小写扩展名。所以这不可能是我的问题的原因。我会改变它,但只是为了确保。
  • @Dennis1679 $target_dir 也可能不是有效的相对文件夹...您可以使用 ´$target_dir = _ DIR _."/uploads/"; 将其设置为绝对文件夹'.并且不要忘记检查它是否具有写入权限 P.S:(删除 DIR 常量中的空格。由于 S.Overflow 格式,我无法删除它)

标签: php flash


【解决方案1】:

您好,您的脚本正在运行,我已尝试使用此 file 并且运行良好。但是当我将文件扩展名更改为 SWF 时,我收到了以下错误:“抱歉,此页面只允许使用 .swf。抱歉,您的文件未上传”。您还必须检查您的文件夹权限,当然,uploads 文件夹必须与您的 php 文件处于同一级别。

【讨论】:

  • 哇,看来您的文件实际上是我尝试过的唯一一个真正有效的文件!我想知道为什么会这样?
  • 你能帮我弄清楚为什么有些 swf 文件会上传而有些不会吗?我已经将 php.ini 编辑为 post_max_size = 8M upload_max_filesize = 20M
  • 您能否发布一个链接,其中包含您尝试上传的 swf 文件并且您遇到了问题?
猜你喜欢
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 2011-07-09
  • 1970-01-01
相关资源
最近更新 更多