scusate potreste aiutarmi a capire dov'è l'errore?
Ho creato questo script che legge una dir e ne inserisce il contenuto (img) in mysql.
Il problema consiste nel fatto che se la dir contiene più di un tot img, non le inserisce tutte.
Forse ho sbagliato a racchiudere qualche blocco di troppo all'interno di qualche condizione, ma ho fatto milioni di tentativi senza risolvere nulla...
Ecco il codice:
Codice PHP:
$handledir = opendir($dir);
while (false !== ($file=readdir($handledir))){
if ($file != "." && $file != ".."){
$file = strtolower($file);
$filename = $dir."/".$file;
$imginfo = getimagesize($filename);
$type = $imginfo['mime'];
$width = $imginfo[0];
$height = $imginfo[1];
$nomeimg = addslashes(stripslashes($file));
if(in_array($type,$visualizzabili)){
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$immagine = addslashes($contents);
echo $filename."
";
//RESIZE IMG
switch ($type) {
case 'image/gif':
if (imagetypes() & IMG_GIF) {
$tfilename = imagecreatefromgif($filename) ;
} else {
$msg = 'GIF images are not supported
';
}
break;
case 'image/jpeg':
if (imagetypes() & IMG_JPG) {
$tfilename = imagecreatefromjpeg($filename) ;
} else {
$msg = 'JPEG images are not supported
';
}
break;
case 'image/png':
if (imagetypes() & IMG_PNG) {
$tfilename = imagecreatefrompng($filename) ;
} else {
$msg = 'PNG images are not supported
';
}
break;
case 'image/wbmp':
if (imagetypes() & IMG_WBMP) {
$tfilename = imagecreatefromwbmp($filename) ;
} else {
$msg = 'WBMP images are not supported
';
}
break;
default:
$msg = $type.' images are not supported
';
break;
}
if ($tfilename){
$width = imagesx($tfilename);
$height = imagesy($tfilename);
$theight = 180;
if($height > $theight){
$twidth = round($width * $theight / $height);
$oDestinationImage = imagecreatetruecolor($twidth, $theight);
echo $twidth;
//resize image
imagecopyresampled($oDestinationImage, $tfilename, 0, 0, 0, 0, $twidth, $theight, $width, $height);
ob_start();
imagejpeg($oDestinationImage);
$sBinaryThumbnail = ob_get_contents();
ob_end_clean();
$imgresize = addslashes($sBinaryThumbnail);
}else{
$imgresize = $immagine;
}
}
//controllo i dati nel db
$query = "SELECT id, img
FROM img
WHERE id = '".$id."'
AND img = '".$imgresize."'";
$result = mysql_query($query);
if(mysql_num_rows($result) != 0){
$msg = "Attenzione: le immagini sono già presenti!";
}else{
$query = "INSERT INTO img (id, name, type, width, height, img, data)
VALUES ('$id', '$nome', '$type', '$twidth', '$theight', '$imgresize', now())";
$result = mysql_query($query);
if($result){
$msg = "Le immagini sono state correttamente inserite!";
}else{
$msg = "L'inserimento non è andato a buon fine!";
}
}
}
}
}
closedir($handledir);