Multiple images upload in PHP - more than 5

2020-02-15 02:16发布

I have an image upload function that supports multiple files upload, but somehow it only uploads five images at once at most.

I really need it working with like maybe a hundred pictures to upload at once.

Here's the PHP file that handles upload:

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

And here's the Image class:

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

Okay, so this is what I have for 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

But it still doesn't seem to work with more than five images. The article I found said to restart the server. But my website is hosted on GoDaddy. Any idea?

Also, here's my form that handle file submission

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

1条回答
家丑人穷心不美
2楼-- · 2020-02-15 02:28

If you have access to php.ini, try setting the max_file_uploads setting. GoDaddy may have lowered it and you can adjust it in php.ini

max-file-uploads


Never mind, I think I got it.

count($_FILES['upload'])

is wrong. Do

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

$_FILES['upload'] always has five elements: name, tmp_name, size, type, and error.

查看更多
登录 后发表回答