2

我有一个支持多个文件上传的图片上传功能,但不知何故它一次最多只能上传五张图片。

我真的需要它一次上传大约一百张图片。

这是处理上传的 PHP 文件:

session_start();
$path = $_SERVER['DOCUMENT_ROOT'];

// Include the Image class file and database for the database connection
include_once $path . "/includes/db.php";
include_once $path . "/classes/Image.php";
$folderName = $_SESSION['id'];

// Loop through the image array
for ($i = 0; $i < count($_FILES['upload']); $i++) {
    // Creating image location information
    $fileLink = "upload/$folderName/" . $_FILES['upload']['name'][$i];
    $fileType = $_FILES['upload']['type'][$i];
    $fileSize = ($_FILES['upload']['type'][$i]) / 1024;

    // See if a photo uploads to just upload not to a specific user
    $source = "$path/$fileLink";
    $insertImageQuery = "INSERT INTO Image (id, image_link, image_type, image_size) VALUES($folderName, '$fileLink', '$fileType', '$fileSize')";
    if ((move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $source)) && mysql_query($insertImageQuery)) {

        // Create a new image object after a file moves to a location
        $curImage = new Image();
        $curImage -> scaleFunction($source);
        $curImage -> save($source);
    }
}

这是 Image 类:

<?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    require_once ($path . "/includes/constants.php");

    class Image {
        private $image;

        //Private Variable stores image type information
        private $image_type;
        // It creates a PHP picture "resource" from imagecreate
        // calls, and this allows for special function calls
        // like imagesy and imagesx.

    function load($filename) {
        $image_info = getimagesize($filename);
        $this -> image_type = $image_info[2];
        if ($this -> image_type == IMAGETYPE_JPEG) {
            //Create JPEG file from source
            $this -> image = imagecreatefromjpeg($filename);
        } elseif ($this -> image_type == IMAGETYPE_GIF) {
            //Create GIF file from source
            $this -> image = imagecreatefromgif($filename);
        } elseif ($this -> image_type == IMAGETYPE_PNG) {
            //Create PNG file from source
            $this -> image = imagecreatefrompng($filename);
        }
    }

    function getWidth() {
        // Use a built-in PHP function to get width in pixels
        return imagesx($this -> image);
    }

    function getHeight() {
        // Use a built-in PHP function to get height in pixels
        return imagesy($this -> image);
    }

    function resizeToHeight($height) {

        $ratio = $height / $this -> getHeight();
        $width = $this -> getWidth() * $ratio;
        $this -> resize($width, $height);
    }

    function resizeToWidth($width) {
        $ratio = $width / $this -> getWidth();
        $height = $this -> getHeight() * $ratio;
        $this -> resize($width, $height);
    }

    // Scale to a particular percentage, for future use
    function scale($scale) {
        $width = $this -> getWidth() * $scale / 100;
        $height = $this -> getheight() * $scale / 100;
        $this -> resize($width, $height);
    }

    function resize($width, $height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this -> image, 0, 0, 0, 0,
                           $width, $height, $this -> getWidth(),
                           $this -> getHeight());
        $this -> image = $new_image;
    }

    // Save a picture with a given quality (compression), with
    // 100 being the highest quality.
    // It is only used for JPEG files because the
    function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 100) {

        if ($image_type == IMAGETYPE_JPEG) {
            imagejpeg($this -> image, $filename, $compression);
        } elseif ($image_type == IMAGETYPE_GIF) {

            imagegif($this -> image, $filename);
        } elseif ($image_type == IMAGETYPE_PNG) {

            imagepng($this -> image, $filename);
        }
    }

    function output($image_type = IMAGETYPE_JPEG) {

        if ($image_type == IMAGETYPE_JPEG) {

            imagejpeg($this -> image);
        } elseif ($image_type == IMAGETYPE_GIF) {

            imagegif($this -> image);
        } elseif ($image_type == IMAGETYPE_PNG) {

            imagepng($this -> image);
        }
    }

    function scaleFunction($filename) {
        $this -> load($filename);
        if ($this -> getHeight() > IMAGE_THRESHOLD) {
            $this -> resizeToHeight(IMAGE_THRESHOLD);
        }
        if ($this -> getWidth() > IMAGE_THRESHOLD) {
            $this -> resizeToWidth(IMAGE_THRESHOLD);
        }
    }

    } // End of the image class
?>

好的,这就是我的 php.ini

max_execution_time = 120
max_input_time = 120
; Whether to allow HTTP file uploads.
file_uploads = On
; Maximum amount of memory a script may consume (50MB)
memory_limit = 50M
; Maximum size of POST data that PHP will accept.
post_max_size = 30M
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

但它似乎仍然不适用于超过五个图像。我找到的文章说要重新启动服务器。但我的网站托管在GoDaddy上。任何想法?

另外,这是我处理文件提交的表单

    <form action="/functions/processor.php" method="post" enctype="multipart/form-data">
        <input class="upload choose_file" type="file" name="upload[]" multiple/>
        <button class="upload" type="submit">
            Upload
        </button>
    </form>
4

1 回答 1

5

如果您有权访问 php.ini,请尝试设置 max_file_uploads 设置。GoDaddy 可能已经降低了它,您可以在 php.ini 中调整它

最大文件上传


没关系,我想我明白了。

count($_FILES['upload'])

是错的。做

count($_FILES['upload']['name'])

$_FILES['upload']总是有五个元素:nametmp_namesizetypeerror

于 2012-04-09T03:54:15.020 回答