Ciao ragazzi,
perdonatemi se rompo di sabato ma spero che tanti di voi come me siano online a lavorare...
ho un problema che non riesco a risolvere per la gestione multipla delle immagini.
vi spiego meglio....
ho uno script che inserisce nel db il percorso di un file e contemporaneamente lo inserisce anche in una cartella creando sia la thumbiline che l'immagine grande ora ho la necessità di fare in modo che lo script carichi contemporaneamente 4 immagini e nel caso qualcuna manchi la sostituisca con una di default. Posto lo scrit funzionante per un solo file.
tabella database
codice:
--
-- Struttura della tabella `immagini`
--
CREATE TABLE `immagini` (
`idimmagini` int(11) NOT NULL auto_increment,
`big` varchar(45) collate latin1_general_ci default NULL,
`thumbs` varchar(45) collate latin1_general_ci default NULL,
`rand_name` varchar(45) collate latin1_general_ci default NULL,
PRIMARY KEY (`idimmagini`)
);
upload.php
Codice PHP:
<?php
include("config.php");
include('connessione.php');
//if the for has submittedd
if (isset($_POST['upForm'])){
$file_type = $_FILES['imgfile']['type'];
$file_name = $_FILES['imgfile']['name'];
$file_size = $_FILES['imgfile']['size'];
$file_tmp = $_FILES['imgfile']['tmp_name'];
//$descrizione = $_POST['descrizione'];
//check if you have selected a file.
if(!is_uploaded_file($file_tmp)){
header("Location: upload.php"); /* Redirect browser */
exit(); //exit the script and don't do anything else.
}
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension.
--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
//print message
//echo "
Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
}
//upload the big image
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
//echo "
Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>";
//echo "
--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
//echo "$rand_name.$file_ext";
$query = "INSERT INTO `immagini` (`big`,`thumbs`, `rand_name`) VALUES ('$path_big/$rand_name.$file_ext', '$path_thumbs/$rand_name.$file_ext', '$rand_name')";
/* //test for insert db ok
echo "$path_big/$rand_name.$file_ext";
echo "
";
echo "$path_thumbs/$rand_name.$file_ext";
echo "
";
echo "$descrizione";
echo "
";
*/
$result = mysql_query($query) or die("Query failed: ".mysql_error());
if($result) {
echo "<div class='err'>
L'immagine è stata inserita correttamente
</div>";
echo "<meta http-equiv=refresh content=2;url=upload.php>";
} else {
echo "<div class='err'>
Errore inserimento immagine.</div>";
}
}else{ //if the form hasn't been submitted.
//print the form
echo "
<form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">
Pictures:
<input type='file' name='imgfile[0]' />
<input type='file' name='imgfile[1]' />
<input type=\"Submit\" name=\"upForm\" value=\"Upload\" class='submit'>
</p>
</form>";
}
?>