ho questo database che si chiama tabella:
id int autoincrement
nome varchar (20)
utente varchar (8)
password varchar (8)
foto varchar(255)
per caricare le foto ho questo form nella pagina login_success.php:
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="foto" type="file" class="Stile1" size="30" />
</label>
<label>
<input type="submit" class="Stile1" value="Invia" />
</label>
</form>
la pagina modifica_foto.php è questa:
Codice PHP:
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database
mysql_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
session_start();
$query = "UPDATE tabella SET foto='" . $_FILES['foto']['name'] . "' 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/";
$img_thumb_width = 120;
{
if($_FILES['foto']['size'] ==0)
{
die('Errore : campo upload nullo');
}
list($width, $height, $type, $attr) = getimagesize($_FILES['foto']['tmp_name']);
if (($width < 600) || ($height < 800))
{
die("Dimensioni non corrette");
}
$file_permessi = array("image/pjpeg","image/jpeg");
if(!in_array($_FILES['foto']['type'], $file_permessi))
{
die('Estensione non consentita');
}
if (file_exists('../public/foto_utente/'.$_FILES['foto']['name']))
{
die('File già esistente sul server. controlla che non hai gia inserito questa foto o pure prova a rinominarla.');
}
$ThumbWidth = $img_thumb_width;
//crea la nuova immagine
if($_FILES['foto']['size']){
if($_FILES['foto']['type'] == "image/pjpeg" || $_FILES['foto']['type'] == "image/jpeg"){
$new_img = imagecreatefromjpeg($_FILES['foto']['tmp_name']);
}elseif($_FILES['foto']['type'] == "image/x-png" || $_FILES['foto']['type'] == "image/png"){
$new_img = imagecreatefrompng($_FILES['foto']['tmp_name']);
}elseif($_FILES['foto']['type'] == "image/gif"){
$new_img = imagecreatefromgif($_FILES['foto']['tmp_name']);
}
//ottiene larghezza e altezza dell'immagine originale.
list($width, $height) = getimagesize($_FILES['foto']['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;
}
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Errore: Assicurati che sul tuo server siano installate le GD library");
}
if($_FILES['foto']['type'] == "image/x-png" || $_FILES['foto']['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['foto']['name']);
if($_FILES['foto']['type'] == "image/pjpeg" || $_FILES['foto']['type'] == "image/jpeg"){
imagejpeg ($resized_img,"$cartella_thumbs/$nome_file");
}
elseif($_FILES['foto']['type'] == "image/x-png" || $_FILES['foto']['type'] == "image/png"){
imagesavealpha($resized_img, true);
imagepng ($resized_img,"$cartella_thumbs/$nome_file");
}
elseif($_FILES['foto']['type'] == "image/gif"){
imagegif($resized_img,"$cartella_thumbs/$nome_file");
}
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
if(!is_uploaded_file($_FILES['foto']['tmp_name']))
{die('Erroe nel caricamento');
}
move_uploaded_file($_FILES['foto']['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\" />
";
}
?>
sulla pagina success_login.php ho anche inserito un rettangolo in cui l'utente può caricare la foto e quindi dentro success_login.php ho inserito un recordset:
Codice PHP:
<?php require_once('Connections/connessione_registrazione_utente.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_Recordset_foto = "-1";
if (isset($_GET['id'])) {
$colname_Recordset_foto = $_GET['id'];
}
mysql_select_db($database_connessione_registrazione_utente, $connessione_registrazione_utente);
$query_Recordset_foto = sprintf("SELECT id, foto FROM tabella WHERE id = %s", GetSQLValueString($colname_Recordset_foto, "text"));
$Recordset_foto = mysql_query($query_Recordset_foto, $connessione_registrazione_hostess) or die(mysql_error());
$row_Recordset_foto = mysql_fetch_assoc($Recordset_foto);
$totalRows_Recordset_foto = mysql_num_rows($Recordset_foto);
?>
e sempre su success_login.php nel rettangolo per far vedere la foto ho inserito questo script:
Codice PHP:
<img src= "../public/foto_utente_thumbs/<?php echo $row_Recordset_foto['foto']; ?>" >
il nome della foto mi compare sul database in questo modo esempio 3.jpg
le foto vengono perfettamente salvate nelle cartelle
ma la foto non si vede perchè???????? dov'è che sbaglio???