脚本在完成文件上载之前退出

时间:2015-06-30 15:34:36

标签: php file-upload upload

我有一个上传文件的PHP脚本。但是,当总上传量太大时,脚本会在大约97%的上传时退出,并让我回到上一页。我不能让它抛出错误信息,所以我不确定是什么导致它。实际上没有文件上传或添加到数据库中。

我认为这可能是一个内存问题,所以在我的上传脚本的顶部,我有:

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

奇怪的是,无论我尝试上传多少文件,如果要退出,它总会在上传的最后阶段退出(~97%)。我假设如果它是一个内存问题,它会在它用完后立即退出。

另一个想法是超出了允许的最大文件数,但检查配置,我看到了

ini_get("max_file_uploads") = 20

我的整个PHP脚本如下(忽略奇数变量大小写,我保持这个脚本的格式与其他人创建的旧脚本相同)。

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

当我上传总共14.2MB的13个文件时上传失败,但是当我上传8个文件时总共4.82MB就成功了。

导致此脚本的初始形式:

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

任何想法如何解决这个问题?或者至少查看一个关于脚本退出的原因的错误?

1 个答案:

答案 0 :(得分:1)

更改php.ini文件中的某些设置

max_execution_time = 60 
60 or higher sets the maximum time in seconds