0

I've built a rather straightforward file uploader in PHP. So far I've had no troubles uploading images and zip files. However, I can't seem to upload .mpg's. Whenever I try then it after hanging for a moment the page seems like it didn't try to upload anything at all. For example: this

// 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

returns nothing but empty arrays. For completeness, here's the front-end

<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>

The file I am testing with is only 33MB and I tested a .wmv of similar size and it uploaded just fine.

Edit: Entire PHP file listed below

<?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>";
}
?>
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91

2 Answers2

0

Ok, the first thing i thougt would be a problem with the filesize but other files with this size are working as you wrote.

But just do get shure it isn't a file size problem:

When you rise the max filesize you hmust also rise the max post size: post_max_size

Johni
  • 2,933
  • 4
  • 28
  • 47
0

You cannot adjust the file upload limits using ini_set() from within the script that the uploads go to - the script does not get executed until after the upload is completed, so the ini_set() overrides cannot take place. The default parameters in PHP will be in place with the lower limit, and will kill the upload if it exceeds the system upload_max_filesize.

You need to override at the .ini level in PHP, or via a php_value directive in a .htaccess file. Those will change PHP's settings as the upload starts.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • "// This is also manually set in php.ini" so I adjusted the php.ini to match that before. However, I do appreciate the information. Now I won't have to use a useless line. – Mike Cluck Aug 17 '11 at 15:12