script del form :
Codice PHP:
<form action="modifiche_utente/modifica_foto.php" method="post" enctype="multipart/form-data" name="modifica_foto" id="modifica_foto">
<label><input type="hidden" name="MAX_FILE_SIZE" value="8000000" />
<input name="data" type="file" class="Stile1" size="30" />
</label>
<label>
<input type="submit" class="Stile1" value="Invia" />
</label>
</form>
script modifica_foto.php :
Codice PHP:
session_start();
$query = "UPDATE tabella SET foto='" . $_FILES['data']['modifica_foto'] . "' WHERE utente ='" . $_SESSION['utente'] . "'";
mysql_query($query);
//percorsi cartelle per il caricamento dei files
$cartella_thumbs = "../public/foto_utente_thumbs/";
$cartella_upload = "../public/foto_utente/";
//la larghezza delle miniature, in pixel
$img_thumb_width = 120;
{
//verifica le dimensioni del file caricato
if($_FILES['data']['size'] ==0)
{
die('Errore : campo upload nullo');
}
// Ottengo le informazioni sull'immagine
list($width, $height, $type, $attr) = getimagesize($_FILES['data']['tmp_name']);
// Controllo che le dimensioni (in pixel) non superino 600x800
if (($width < 600) || ($height < 800))
{
die("Dimensioni non corrette");
}
$file_permessi = array("image/pjpeg","image/jpeg");
if(!in_array($_FILES['data']['type'], $file_permessi))
{
die('Estensione non consentita');
}
// Verifico che sul sul server non esista già un file con lo stesso nome
if (file_exists('../../public/foto_utente/'.$_FILES['data']['name']))
{
die('File già esistente sul server. controlla che non hai gia inserito questa foto o pure prava a rinominarla.');
}
///////////////////////
//crea la thumbsnail///
//////////////////////
$ThumbWidth = $img_thumb_width;
//crea la nuova immagine
if($_FILES['data']['size']){
if($_FILES['data']['type'] == "image/pjpeg" || $_FILES['data']['type'] == "image/jpeg"){
$new_img = imagecreatefromjpeg($_FILES['data']['tmp_name']);
}elseif($_FILES['data']['type'] == "image/x-png" || $_FILES['data']['type'] == "image/png"){
$new_img = imagecreatefrompng($_FILES['data']['tmp_name']);
}elseif($_FILES['data']['type'] == "image/gif"){
$new_img = imagecreatefromgif($_FILES['data']['tmp_name']);
}
//ottiene larghezza e altezza dell'immagine originale.
list($width, $height) = getimagesize($_FILES['data']['tmp_name']);
//calcola le proporzioni e ottiene dimensioni thumbsnail
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//funzione per ridimensionare l'immagine.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Errore: Assicurati che sul tuo server siano installate le GD library");
}
//ridimensionamento
if($_FILES['data']['type'] == "image/x-png" || $_FILES['data']['type'] == "image/png"){
imagealphablending($resized_img, false);
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//salva l'immagine
$nome_file = strtolower($_FILES['data']['name']);
if($_FILES['data']['type'] == "image/pjpeg" || $_FILES['data']['type'] == "image/jpeg"){
imagejpeg ($resized_img,"$cartella_thumbs/$nome_file");
}
elseif($_FILES['data']['type'] == "image/x-png" || $_FILES['data']['type'] == "image/png"){
imagesavealpha($resized_img, true);
imagepng ($resized_img,"$cartella_thumbs/$nome_file");
}
elseif($_FILES['data']['type'] == "image/gif"){
imagegif($resized_img,"$cartella_thumbs/$nome_file");
}
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
if(!is_uploaded_file($_FILES['data']['tmp_name']))
{die('Erroe nel caricamento');
}
move_uploaded_file($_FILES['data']['tmp_name'],$cartella_upload .$nome_file)
or die('Non posso caricare il file');
echo "L'immagine è stata ridimensionata ed inserita con successo:
<img src=\"$cartella_thumbs/$nome_file\" />
";
}
?>
che devo fare per far si che la seconda foto inviata sostituisca la prima?