problema con upload immagini multiple

  • Creatore Discussione Creatore Discussione aganju
  • Data di inizio Data di inizio

aganju

Nuovo Utente
5 Feb 2013
10
0
0
Ciao,

ho un problema con l' upload di immagini multiple. In pratica, quello che vorrei fare è modificare uno script che ho ed è funzionante e modificarlo per poter eseguire upload multipli.

Dal form ricevo l'array contenente i dati delle immagini
HTML:
<input name="fleImage[]" type="file" id="fleImage" class="box"  multiple="multiple">
Di seguito ho 2 funzioni che processano il tutto.
La prima, funzione addProduct() da notare che ho aggiunto il foreach che prima non c'era, ma che non funziona

PHP:
function addProduct()
{
	

foreach($_FILES['fleImage']['tmp_name'] as $key => $images ){ 
     
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');			
            
$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];


$query="INSERT into tbl_images... bla bla bla... ";		 
     
    $result = dbQuery($query);	
       
}

 	header("Location: index.php?catId=$catId");	
}

e la funzione uploadProductImage() che processa l'immagine facendo un resize e spostandola con quella originale, in una cartella predefinita. Questa funzione non l'ho toccata.
PHP:
function uploadProductImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

		// generate a random new file name to avoid name conflict
		$imagePath = md5(rand() * time()) . ".$ext";
		
		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

		// make sure the image width does not exceed the
		// maximum allowed width
		if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
			$imagePath = $result;
		} else {
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}	
		
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the product cannot be upload / resized
			$imagePath = $thumbnailPath = '';
		}
		
	}

	
	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

Quello che non riesco a capire è come far funzionare il foreach in modo da sfruttare la 2a funzione. Nel modo in cui ho fatto, mi funziona l'inserimento dei dati nel DB mysql con la query, ma non l'upload delle immagini.

Spero di essere stato chiaro e di ricevere un aiuto :crying:
grazie
 

Discussioni simili