ciao,
ho questo script che gestisce l'inserimento di prodotti nel db mysql.
Riceve quindi i dati dal form, inserisce i dati nel db, prende l'immagine, fa un resize per l'anteprima e la copia nella cartella specificata insieme a quella originale.
Adesso voglio utilizzare html5 che mi permette di selezionare più immagini contemporaneamente :
codice:
<input name="fleImage[]" type="file" id="fleImage" class="box" multiple/>
Quindi ho modificato lo script in questo modo:
Codice PHP:
function addProduct() {
$catId = $_POST['cboCategory'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$description = nl2br(htmlspecialchars($description, ENT_QUOTES));
foreach($_FILES['fleImage']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['fleImage']['name'][$key];
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];
$query="INSERT into tbl_images (cat_id, file_name, pd_image, pd_thumbnail) VALUES('$catId','$file_name','$mainImage','$thumbnail') ";
$result = dbQuery($query);
}
$sql = "INSERT INTO tbl_product (cat_id, pd_name, pd_description, pd_date) VALUES ('$catId', '$name', '$description', NOW())";
$result = dbQuery($sql);
header("Location: index.php?catId=$catId"); }
mentre la funzione che esegue il resize e l'upload delle immagini è questo:
Codice 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); }
Rispetto allo script originale funzionante, ho aggiunto solo il foreach nella funzione addProduct() e una query, mentre la seconda funzione uploadProductImage non l'ho toccata.
Il problema: mi carica i dati nel DB ma non fa ne il resize ne l'upload delle immagini.
C'è qualcosa che mi sfugge??
Grazie!