2012-04-09 80 views
2

我有一個圖像上傳功能,支持多個文件上傳,但不知何故,它最多隻能一次上傳5張圖像。PHP多圖像上傳(更新)

我真的需要它像一百張圖片一起上傳。

這裏的PHP文件來處理上傳

session_start(); 
$path = $_SERVER['DOCUMENT_ROOT']; 
//Include Image class file and db for 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 new image object after file moves to a location 
     $curImage = new Image(); 
     $curImage -> scaleFunction($source); 
     $curImage -> save($source); 
    } 
} 

而這裏的圖片類

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

class Image { 
    private $image; 
    //Private Variable stores image type information 
    private $image_type; 
    //creates a php picture "resource" from imagecreate calls, 
    //this allows for special function calls like imagesy/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 built in php function to get width in pixels 
    return imagesx($this -> image); 
} 

function getHeight() { 
    //use 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) 100 being the highest quality 
//only used for jpg 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 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 

但它似乎仍不能用於超過5張圖像。我發現的文章說重啓服務器。但我的網站託管在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> 
+0

當您上傳超過5張圖片,到底會發生什麼? – deceze 2012-04-09 02:20:25

+0

您的POST最大尺寸是否足以上傳100張圖像? – 2012-04-09 02:23:37

+0

此外,您的腳本最大執行時間是否足夠高? – teh1 2012-04-09 02:25:19

回答

5

如果你有機會爲php.ini,請嘗試將max_file_uploads設置。 GoDaddy的可能已經降低了它,你可以在php.ini

max-file-uploads

沒關係調整它,我想我得到了它

count($_FILES['upload']) 

是錯誤的。做

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

$_FILES['upload']總有5個元素:nametmp_namesizetypeerror

+0

我在php.ini中放了'max_file_uploads = 30',但它仍然不起作用。當我只上傳3張圖片時,它回顯了5行,最後兩行有目錄+空白文件名。我認爲上傳大小固定爲5個文件。 – lusketeer 2012-04-09 04:03:00

+0

啊哈,這是線索,這是一個不正確的計數。編輯答案。 – 2012-04-09 04:09:54

+0

不錯,謝謝,我只是想問哪個是正確的方法來做正確的計數,因爲我也發現了這個錯誤。 – lusketeer 2012-04-09 04:15:26