douluan5444 2012-04-09 02:12
浏览 60
已采纳

用PHP上传多个图像 - 超过5个

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条回答 默认 最新

  • dongzi9196 2012-04-09 03:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名