1

I have a PHP script that uploads files. However, when the total upload size is too great, the script exits on about 97% uploaded, and drops me back to the previous page. I can't get it to throw an error message, so I'm not sure what's causing it. No files actually get uploaded or added to the database.

I thought it could be a memory issue, so at the top of my upload script, I have:

ini_set("memory_limit", "64M");

The odd thing is, no matter how many files I try and upload, if it wants to exit, it always exits right at the final stage of uploading (~97%). I assume if it was a memory issue, it would exit as soon as it ran out.

Another thought was exceeding the maximum allowed files, but checking the configuration, I see

ini_get("max_file_uploads") = 20

My entire PHP script is as follows (ignore the odd variable casing, I'm keeping this script in the same format as an older one created by someone else).

ini_set("memory_limit", "64M");

if (!isset($_POST['SiteID']) || !filter_var($_POST['SiteID'], FILTER_VALIDATE_INT)) {
    header("Location: ./");
    exit();
}
$SiteID = $_POST['SiteID'];

if (!isset($_POST['GalleryID']) || !filter_var($_POST['GalleryID'], FILTER_VALIDATE_INT)) {
    header("Location: ./?SiteID=" . $SiteID . "&error=upload");
    exit();
}
$GalleryID = $_POST['GalleryID'];

$ImageFile = $_FILES['ImageFile'];

$Extensions = array("jpg", "jpeg");
$Timestamp = date("U");

require_once("db.php");

for ($X = 0; $X < count($_FILES['ImageFile']['name']); $X++) {
    $Extension = strtolower(pathinfo($ImageFile['name'][$X], PATHINFO_EXTENSION));
    if (in_array($Extension, $Extensions)) {
        if ($ImageFile['error'][$X] === UPLOAD_ERR_OK) {
            if ($ImageFile['size'][$X] <= 5 * 1024 * 1024) {
                $Filename = $GalleryID . "-" . $Timestamp . "-" . $X . "." . $Extension;
                move_uploaded_file($ImageFile['tmp_name'][$X], "uploads/" . $Filename);
                mysql_query("INSERT INTO NewGalleryImages SET GalleryID = $GalleryID, ImageFile = '$Filename', ImageCaption = ''");
                $Dimensions = getimagesize("uploads/" . $Filename);
                $Width = ($Dimensions[0] > 768 ? 768 : $Dimensions[0]);
                $Height = ($Dimensions[1] / $Dimensions[0]) * $Width;
                $Expl = explode(".", $Filename);
                $LargeFilename = $Expl[0] . "_gallery." . $Expl[1];
                $Source = "uploads/" . $Filename;
                $Canvas = imagecreatetruecolor($Width, $Height);
                $LargeImage = imagecreatefromjpeg($Source);
                imagecopyresampled($Canvas, $LargeImage, 0, 0, 0, 0, $Width, $Height, $Dimensions[0], $Dimensions[1]);
                imagejpeg($Canvas, "uploads/" . $LargeFilename, 100);
                $Width = 200;
                $Height = ($Dimensions[1] / $Dimensions[0]) * $Width;
                $Expl = explode(".", $Filename);
                $ThumbFilename = $Expl[0] . "_thumb." . $Expl[1];
                $Source = "uploads/" . $Filename;
                $Canvas = imagecreatetruecolor($Width, $Height);
                $ThumbImage = imagecreatefromjpeg($Source);
                imagecopyresampled($Canvas, $ThumbImage, 0, 0, 0, 0, $Width, $Height, $Dimensions[0], $Dimensions[1]);
                imagejpeg($Canvas, "uploads/" . $ThumbFilename, 100);
            } else {
                mysql_close();
                header("Location: ./?SiteID=" . $SiteID . "&error=size");
                exit();
            }
        } else {
            mysql_close();
            header("Location: ./?SiteID=" . $SiteID . "&error=unknown");
            exit();
        }
    } else {
        mysql_close();
        header("Location: ./?SiteID=" . $SiteID . "&error=type");
        exit();
    }
}

mysql_close();
header("Location: ./?SiteID=" . $SiteID . "&uploaded");
exit();

// var_dump($_POST);
// var_dump($_FILES);
// var_dump($ImageFile);

The upload fails when I upload 13 files at a total of 14.2MB, but succeeds when I upload 8 files at a total of 4.82MB.

The initial form that leads to this script it:

<form action="gallery-upload.php" method="post" enctype="multipart/form-data">
    <input class="form-control" name="ImageFile[]" type="file" multiple required>
    <input type="hidden" name="SiteID" value="<?=$SiteID?>">
    <input type="hidden" name="GalleryID" value="<?=$GalleryID?>">
    <button class="btn btn-primary" type="submit">Upload Images</button>
</form>

Any ideas how to fix this? Or at least view an error as to why the script it exiting?

mpdc
  • 3,550
  • 5
  • 25
  • 48
  • this answer might be helpful http://stackoverflow.com/questions/9691057/php-apache-ajax-post-limit/9691395#9691395 – dav Jun 30 '15 at 15:38
  • Thanks! I see the `post_max_vars` is set to `8M`, which looks like the problem. I need to amend it in-page: do I amend it on the form page, or on the upload script? – mpdc Jun 30 '15 at 15:43
  • putting on the form page definitely will not make any effect, however im not sure if putting on upload script file will work (pls give it a try), but I think it should be set from apache's config file, rather than by `ini_set` – dav Jun 30 '15 at 15:46

1 Answers1

1

change some setting in your php.ini file

max_execution_time = 60 
60 or higher sets the maximum time in seconds
rocky
  • 631
  • 5
  • 14
  • Excellent. This, and increasing my `post_max_vars` worked a treat. Thanks to yourself and @dav. – mpdc Jun 30 '15 at 16:00