【问题标题】:Cannot upload .mpg files无法上传 .mpg 文件
【发布时间】:2011-10-29 00:25:49
【问题描述】:

我用 PHP 构建了一个相当简单的文件上传器。到目前为止,我在上传图像和 zip 文件时没有遇到任何问题。但是,我似乎无法上传 .mpg。每当我在挂起一段时间后尝试它时,页面似乎根本没有尝试上传任何内容。例如:这个

// This is also manually set in php.ini
ini_set("upload_max_filesize", "524288000");
...
print_r($_FILES);
print_r($_POST); // I'm sending along one variable in addition to the file

只返回空数组。为了完整起见,这里是前端

<form action="uploadVideo.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="524288000"/>
        <input type="hidden" name="extravar" value="value" />
        <p>
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file" /><br />
            <i>Accepted formats: .mp4, .3gp, .wmv, .mpeg and .mpg. Cannot exceed 500MB.</i>
        </p>
        <p>Description:</p>
            <textarea name="description" rows="4" cols="40"></textarea>
        <p><input type="submit" name="submit" value="Submit" /></p>
    </form>

我正在测试的文件只有 33MB,我测试了一个类似大小的 .wmv,它上传得很好。

编辑:下面列出的整个 PHP 文件

<?php
// Ensure that the user can upload up to the maximum size
ini_set("upload_max_filesize", "524288000");
ini_set("post_max_size", "524288000");
print_r($_POST);
print_r($_FILES);

if(!$link = mysql_connect($SERVER_LOCATION, $DB_USER, $DB_PASS)) die("Error connecting to server.");
mysql_select_db($DB_NAME);
$eventID = $_POST['event'];

// Select the event this is associated with
$query = "SELECT eventName FROM event WHERE eventID = $eventID";
if(!$res = mysql_query($query, $link)) die("Error communicating with database.");
$path = mysql_fetch_row($res);
$path = "media/$path[0]";

// If this event doesn't have a media folder, make one
if(!file_exists($path)) {
    mkdir($path);
}

// If this event doesn't have a GIS subfolder, make one
$path = "$path/videos";
if(!file_exists($path)) {
    mkdir($path);
}

// Generate todays date and a random number for the new filename
$today = getdate();

$seed  = $today['seconds'] * $today['minutes'];
srand($seed);
$random = rand(0, 999);

$today = $today['mon']."-".$today['mday']."-".$today['year'];

$fileType = $_FILES["file"]["type"];
$fileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

$isMP4 = ($fileType == "video/mp4" && $fileExtension == "mp4");
$isWMV = ($fileType == "video/x-ms-wmv" && $fileExtension == "wmv");
$isMPG = ($fileType == "video/mpeg" && ($fileExtension == "mpeg" || $fileExtension == "mpg"));
$is3GP = ($fileType == "video/3gp" && $fileExtension == "3gp");
$sizeIsOK = ($_FILES["file"]["size"] < 524288000);

if( ($isMP4 || $isWMV || $isMPG || $is3GP) && $sizeIsOK ) {
    if($_FILES["file"]["error"] > 0) {
        echo "<p>There was a problem with your file. Please check that you submitted a valid .zip or .mxd file.</p>";
        echo "<p>If this error continues, contact a system administrator.</p>";
        die();
    } else {
        // Ensure that the file get's a unique name
        $filename = $today . "-" . $random . "." . $fileExtension;
        while(file_exists("$path/$filename")) {
            $random = rand(0, 999);
            $filename = $today . "-" . $random . "." . $fileExtension;
        }
        move_uploaded_file($_FILES["file"]["tmp_name"], "$path/$filename");

        $description = $_POST['description'];
        $query = "INSERT INTO media (eventID,FileName,File,filetype,Description) VALUES ($eventID,'$filename','$path','video','$description')";
        if(!$res = mysql_query($query, $link))
            echo "<p>Error storing file description. Please contact a system administrator.</p>";
        else {
            echo "<h3>File: <i>".$_FILES["file"]["name"]."</i></h3>";
            if(strlen($description) > 0) {
                echo "<h3>Description: <i>".$description."</i></h3>";
            }
            echo "<p><strong>Upload Complete</strong></p>";
        }
        echo "<button onclick=\"setTimeout(history.go(-1), '1000000')\">Go Back</button>";
    }
} else {
    echo "<p>There was a problem with your file. Please check that you submitted a valid .zip or .mxd file.</p>";
    echo "<p>If this error continues, contact a system administrator.</p>";
}
?>

【问题讨论】:

  • 你的$_FILES[&lt;name&gt;]['errors']中有什么
  • 您的 php 中是否包含所有这些文件类型?:stackoverflow.com/questions/790873/mp3-filetype-upload
  • 将文件重命名为 e.g. 时会发生什么.txt?
  • @cwallenpoole 什么都没有。 $_FILES[] 未定义。 @ Robot Woods 我没有任何 mp3 内容,也没有音频/mpeg(可能有用),但如前所述,$_FILES 没有正确定义,所以我什至无法检查类型。 @Kris 同样的结果。
  • 你能把剩下的 php 贴出来吗?

标签: php html file-upload


【解决方案1】:

您无法在上传到的脚本中使用 ini_set() 调整文件上传限制 - 直到上传完成后才会执行该脚本,因此无法进行 ini_set() 覆盖。 PHP中默认参数会在下限到位,超过系统upload_max_filesize会终止上传。

您需要在 PHP 中的 .ini 级别或通过 .htaccess 文件中的 php_value 指令覆盖。这些将在上传开始时更改 PHP 的设置。

【讨论】:

  • "// 这也是在 php.ini 中手动设置的" 所以我调整了 php.ini 以匹配之前的那个。但是,我很欣赏这些信息。现在我不必使用无用的线路了。
【解决方案2】:

好的,我首先想到的是文件大小有问题,但是其他具有这种大小的文件正在按照您的编写方式工作。

但请确保这不是文件大小问题:

当您增加最大文件大小时,您还必须增加最大帖子大小:post_max_size

【讨论】:

  • 我已经调整了upload_max_filesize,但我只是尝试调整post_max_size,但无济于事。
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 2016-07-25
  • 2013-08-24
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多