Problema erori in script adaugare imagini

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
duvibv
Mesaje:14

Problema erori in script adaugare imagini

Salutare useri Marplo,
Am un formular cu posibilitatea de adaugarea a 3 imagini in baza de date!
Functioneaza bine daca adaug acele imagini,insa daca nu aleg imi afiseaza eroare:

Cod: Selectaţi tot

getimagesize() [function.getimagesize]: Filename cannot be empty in C:\wamp\www\layer 23.07.2011\action\insert.php on line 187
Iar linia aferenta:

Cod: Selectaţi tot

list( , , $temp_image_type ) = getimagesize( $temp_image_path );
 
Si:

Cod: Selectaţi tot

Notice: Undefined variable: fisier_final_jpg in C:\wamp\www\layer 23.07.2011\action\insert.php on line 231
Eroarea imi spune ca nu este adaugata imaginea(ceea ce este si corect) dar nu as vrea sa mai apara.O metoda este sa nu-mi afiseze erorile (dar eu caut sa rezolv eroarea nu sa nu mai afisez erori la tot scriptul).Iar pentru a2-a eroare nu este declarata variabila $fisier_final_jpg.
Daca trebuie sa adaug formularul il voi posta(nu l-am pus pt ca este destul de lung)
Multumesc anticipat!

MarPlo Mesaje:4343
Salut
Indicat e ca inainte de a avea functiile pt adaugare imagini sa verifici daca este imagine transmisa.
De exemplu:

Cod: Selectaţi tot

// Verifica daca sunt date trimise de la "nume_camp"
if (isset($_FILES['nume_camp']) && !empty($_FILES['nume_camp'])) {
  // codul pt lucru cu datele trimmise
}
 
Incearca asa in scriptul tau. La linia 187:

Cod: Selectaţi tot

if (isset($temp_image_path) && !empty($temp_image_path)) {
  list( , , $temp_image_type ) = getimagesize( $temp_image_path );
}
 
Iar la linia 231, cam asa:

Cod: Selectaţi tot

if(isset($fisier_final_jpg)) {
  // codul de la acea linie, cu variabila $fisier_final_jpg
}
 

duvibv Mesaje:14
Din pacate nu am reusit!
Daca inlocuiesc la linia 187 imi apare

Cod: Selectaţi tot

Notice: Undefined variable: temp_image_type in C:\wamp\www\layer 23.07.2011\action\insert.php on line 200
Am adaugat scriptul...

Cod: Selectaţi tot

define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 140 ); // latime maxima a imaginii thumbnail generate
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 100 ); // inaltime maxima thumbnail

$fisier= $_FILES['Image1']['name'];
$marime = $_FILES['Image1']['size'];

$fisier2= $_FILES['Image2']['name'];
$marime = $_FILES['Image2']['size'];

$fisier3= $_FILES['Image3']['name'];
$marime = $_FILES['Image3']['size'];

// putem pune o limita la marimea pozei, in kB
if($marime >= 1500000)  
    {
    echo "Marime fisier poza prea mare. Maxim 150 KB";
    echo '<br><a href="javascript: history.go(-1)">Inapoi</a>';
    exit;
    }


// denumire fisier poza si eliminare caractere interzise din numele fisierului:

function RewriteFisier ($string){
    return strtolower(trim(preg_replace("/[^0-9a-zA-Z.]+/", "-", $string),"-"));
}

$fisier = RewriteFisier($fisier);
$now = time();
$fisier = $now."-".$fisier;
$fisier2 = $now."-".$fisier2;
$fisier3 = $now."-".$fisier3;
$fisier_final = $now."-".$fisier;
$fisier_final2 = $now."-".$fisier2;
$fisier_final3 = $now."-".$fisier3; // adaugare ID unic la umele fisierului, util in cazul cand uploadam doua poze cu acelasi nume de fisier

// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final, -3) =='png')
    {
    $fisier_final_jpg = str_replace("png", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='gif')
    {
    $fisier_final_jpg = str_replace("gif", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='bmp')
    {
    $fisier_final_jpg = str_replace("bmp", "jpg", $fisier_final);
    }
if (substr($fisier_final, -4) =='jpeg')
    {
    $fisier_final_jpg = str_replace("jpeg", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='jpg')
    {
    $fisier_final_jpg = $fisier_final;
    }

	// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final2, -3) =='png')
    {
    $fisier_final2_jpg = str_replace("png", "jpg", $fisier_final2);
    }
if (substr($fisier_final, -3) =='gif')
    {
    $fisier_final2_jpg = str_replace("gif", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -3) =='bmp')
    {
    $fisier_final2_jpg = str_replace("bmp", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -4) =='jpeg')
    {
    $fisier_final2_jpg = str_replace("jpeg", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -3) =='jpg')
    {
    $fisier_final2_jpg = $fisier_final2;
    }
	
	// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final3, -3) =='png')
    {
    $fisier_final3_jpg = str_replace("png", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='gif')
    {
    $fisier_final3_jpg = str_replace("gif", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='bmp')
    {
    $fisier_final3_jpg = str_replace("bmp", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -4) =='jpeg')
    {
    $fisier_final3_jpg = str_replace("jpeg", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='jpg')
    {
    $fisier_final3_jpg = $fisier_final3;
    }

// functie creare thumbnail
function generate_image_thumbnail( $source_image_path, $thumbnail_image_path )
 {
  list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );

  switch ( $source_image_type )
  {
   case IMAGETYPE_GIF:
    $source_gd_image = imagecreatefromgif( $source_image_path );
    break;

   case IMAGETYPE_JPEG:
    $source_gd_image = imagecreatefromjpeg( $source_image_path );
    break;

   case IMAGETYPE_PNG:
    $source_gd_image = imagecreatefrompng( $source_image_path );
    break;
  }

  if ( $source_gd_image === false )
  {
   return false;
  }

  $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
  $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;

  $source_aspect_ratio = $source_image_width / $source_image_height;
  $thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;

  if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
  {
   $thumbnail_image_width = $source_image_width;
   $thumbnail_image_height = $source_image_height;
  }
  elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
  {
   $thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
  }
  else
  {
   $thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
  }

  $thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );

  imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );

  imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 80 );

  imagedestroy( $source_gd_image );

  imagedestroy( $thumbnail_gd_image );

  return true;
}

 
define( 'UPLOADED_IMAGE_DESTINATION', 'img_original/' ); // calea catre directorul unde se uploadeaza poza originala
define( 'THUMBNAIL_IMAGE_DESTINATION', 'img_thumbnail/' ); // calea catre directorul unde se uploadeaza poza mica (thumbnail)

function process_image_upload( $field )
{
  $temp_image_path = $_FILES[ $field ][ 'tmp_name' ];
  
  $now = time();
  $temp_image_name = $now."-".$_FILES[ $field ][ 'name' ];
  $temp_image_name = RewriteFisier($temp_image_name);
  
  list( , , $temp_image_type ) = getimagesize( $temp_image_path );

  if ( $temp_image_type === NULL )
  {
   return false;
  }

  switch ( $temp_image_type )
  {
   case IMAGETYPE_GIF:
    break;

   case IMAGETYPE_JPEG:
    break;

   case IMAGETYPE_PNG:
    break;

   default:
    return false;
  }

  $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
      

  move_uploaded_file( $temp_image_path, $uploaded_image_path );

  $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace( '{\\.[^\\.]+$}', '.jpg', $temp_image_name );

  $result = generate_image_thumbnail( $uploaded_image_path, $thumbnail_image_path );

  return $result
   ? array( $uploaded_image_path, $thumbnail_image_path )
   : false;
}


$result = process_image_upload( 'Image1' );
$result = process_image_upload( 'Image2' );
$result = process_image_upload( 'Image3' );


  $query="INSERT INTO anunturi (categorie, subCategorie, judet, pret, tipPret, titlu, continut, email, valid, telefon, nume, data, poze, poze2, poze3 )
VALUES ('$categorie','$subcategorie','$judet','$pret','$tipPret','$titlu','$continut','$email','0','$telefon','$nume','$servdate','$fisier_final_jpg','$fisier_final2_jpg','$fisier_final3_jpg')";
if (!mysql_query($query)) {
die(mysql_error());
} else {
echo " Mesajul a fost trimis la data de $servdate";
}

mysql_close($conn);
Am atatea variabile,daca modific ceva imi apare alta eroare si tot asa :)

Iti multumesc Marplo!Numai bine!

MarPlo Mesaje:4343
Vezi daca merge varianta asta, sunt definite la inceput variabilele pt imagini pe care le foloseste comanda de adaugare in baza de date, astfel, daca nu sunt imagini adaugate, ele vor fi goale. Iar functia de la $result e accesata doar daca sunt imagini.
Mai departe cred ca te descurci daca apare vreo eroare, sau, refacut tot scriptul asa cum trebuie.

Cod: Selectaţi tot

$fisier_final_jpg = $fisier_final2_jpg = $fisier_final3_jpg = '';
define( 'THUMBNAIL_IMAGE_MAX_WIDTH', 140 ); // latime maxima a imaginii thumbnail generate
define( 'THUMBNAIL_IMAGE_MAX_HEIGHT', 100 ); // inaltime maxima thumbnail

$fisier= $_FILES['Image1']['name'];
$marime = $_FILES['Image1']['size'];

$fisier2= $_FILES['Image2']['name'];
$marime = $_FILES['Image2']['size'];

$fisier3= $_FILES['Image3']['name'];
$marime = $_FILES['Image3']['size'];

// putem pune o limita la marimea pozei, in kB
if($marime >= 1500000) 
    {
    echo "Marime fisier poza prea mare. Maxim 150 KB";
    echo '<br><a href="javascript: history.go(-1)">Inapoi</a>';
    exit;
    }


// denumire fisier poza si eliminare caractere interzise din numele fisierului:

function RewriteFisier ($string){
    return strtolower(trim(preg_replace("/[^0-9a-zA-Z.]+/", "-", $string),"-"));
}

$fisier = RewriteFisier($fisier);
$now = time();
$fisier = $now."-".$fisier;
$fisier2 = $now."-".$fisier2;
$fisier3 = $now."-".$fisier3;
$fisier_final = $now."-".$fisier;
$fisier_final2 = $now."-".$fisier2;
$fisier_final3 = $now."-".$fisier3; // adaugare ID unic la umele fisierului, util in cazul cand uploadam doua poze cu acelasi nume de fisier

// thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final, -3) =='png')
    {
    $fisier_final_jpg = str_replace("png", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='gif')
    {
    $fisier_final_jpg = str_replace("gif", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='bmp')
    {
    $fisier_final_jpg = str_replace("bmp", "jpg", $fisier_final);
    }
if (substr($fisier_final, -4) =='jpeg')
    {
    $fisier_final_jpg = str_replace("jpeg", "jpg", $fisier_final);
    }
if (substr($fisier_final, -3) =='jpg')
    {
    $fisier_final_jpg = $fisier_final;
    }

   // thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final2, -3) =='png')
    {
    $fisier_final2_jpg = str_replace("png", "jpg", $fisier_final2);
    }
if (substr($fisier_final, -3) =='gif')
    {
    $fisier_final2_jpg = str_replace("gif", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -3) =='bmp')
    {
    $fisier_final2_jpg = str_replace("bmp", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -4) =='jpeg')
    {
    $fisier_final2_jpg = str_replace("jpeg", "jpg", $fisier_final2);
    }
if (substr($fisier_final2, -3) =='jpg')
    {
    $fisier_final2_jpg = $fisier_final2;
    }
   
   // thumbnailul va fi transformat in imagine de tip "jpg"
if (substr($fisier_final3, -3) =='png')
    {
    $fisier_final3_jpg = str_replace("png", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='gif')
    {
    $fisier_final3_jpg = str_replace("gif", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='bmp')
    {
    $fisier_final3_jpg = str_replace("bmp", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -4) =='jpeg')
    {
    $fisier_final3_jpg = str_replace("jpeg", "jpg", $fisier_final3);
    }
if (substr($fisier_final3, -3) =='jpg')
    {
    $fisier_final3_jpg = $fisier_final3;
    }

// functie creare thumbnail
function generate_image_thumbnail( $source_image_path, $thumbnail_image_path )
{
  list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );

  switch ( $source_image_type )
  {
   case IMAGETYPE_GIF:
    $source_gd_image = imagecreatefromgif( $source_image_path );
    break;

   case IMAGETYPE_JPEG:
    $source_gd_image = imagecreatefromjpeg( $source_image_path );
    break;

   case IMAGETYPE_PNG:
    $source_gd_image = imagecreatefrompng( $source_image_path );
    break;
  }

  if ( $source_gd_image === false )
  {
   return false;
  }

  $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
  $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;

  $source_aspect_ratio = $source_image_width / $source_image_height;
  $thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;

  if ( $source_image_width <= $thumbnail_image_width && $source_image_height <= $thumbnail_image_height )
  {
   $thumbnail_image_width = $source_image_width;
   $thumbnail_image_height = $source_image_height;
  }
  elseif ( $thumbnail_aspect_ratio > $source_aspect_ratio )
  {
   $thumbnail_image_width = ( int ) ( $thumbnail_image_height * $source_aspect_ratio );
  }
  else
  {
   $thumbnail_image_height = ( int ) ( $thumbnail_image_width / $source_aspect_ratio );
  }

  $thumbnail_gd_image = imagecreatetruecolor( $thumbnail_image_width, $thumbnail_image_height );

  imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height );

  imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 80 );

  imagedestroy( $source_gd_image );

  imagedestroy( $thumbnail_gd_image );

  return true;
}


define( 'UPLOADED_IMAGE_DESTINATION', 'img_original/' ); // calea catre directorul unde se uploadeaza poza originala
define( 'THUMBNAIL_IMAGE_DESTINATION', 'img_thumbnail/' ); // calea catre directorul unde se uploadeaza poza mica (thumbnail)

function process_image_upload( $field )
{
  $temp_image_path = $_FILES[ $field ][ 'tmp_name' ];
 
  $now = time();
  $temp_image_name = $now."-".$_FILES[ $field ][ 'name' ];
  $temp_image_name = RewriteFisier($temp_image_name);
 
  list( , , $temp_image_type ) = getimagesize( $temp_image_path );

  if ( $temp_image_type === NULL )
  {
   return false;
  }

  switch ( $temp_image_type )
  {
   case IMAGETYPE_GIF:
    break;

   case IMAGETYPE_JPEG:
    break;

   case IMAGETYPE_PNG:
    break;

   default:
    return false;
  }

  $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
     

  move_uploaded_file( $temp_image_path, $uploaded_image_path );

  $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace( '{\\.[^\\.]+$}', '.jpg', $temp_image_name );

  $result = generate_image_thumbnail( $uploaded_image_path, $thumbnail_image_path );

  return $result
   ? array( $uploaded_image_path, $thumbnail_image_path )
   : false;
}


$result = (isset($_FILES['Image1']) && !empty($_FILES['Image1']) ? process_image_upload( 'Image1' ) : array();
$result = (isset($_FILES['Image2']) && !empty($_FILES['Image2']) ? process_image_upload( 'Image2' ) : array();
$result = (isset($_FILES['Image3']) && !empty($_FILES['Image3']) ? process_image_upload( 'Image3' ) : array();


$query="INSERT INTO anunturi (categorie, subCategorie, judet, pret, tipPret, titlu, continut, email, valid, telefon, nume, data, poze, poze2, poze3 )
VALUES ('$categorie','$subcategorie','$judet','$pret','$tipPret','$titlu','$continut','$email','0','$telefon','$nume','$servdate','$fisier_final_jpg','$fisier_final2_jpg','$fisier_final3_jpg')";
if (!mysql_query($query)) die(mysql_error());
else echo " Mesajul a fost trimis la data de $servdate";

mysql_close($conn);
 

duvibv Mesaje:14

Cod: Selectaţi tot

 Parse error: syntax error, unexpected ';' in C:\wamp\www\layer 23.07.2011\action\insert.php on line 224

evident Mesaje:168
Din cate vad folosesti wampserver, nu ti-l recomand, iti da niste erori aiurea.
Eu aveam de ex $casa=1; si tot imi dadea eroare undefined variabile $casa in...
Eu folosesc wampserver dar tot timpul ma verific si cu serverele de pe net. Incearca sa pui totul undeva pe net si vezi daca iti mai da ceva erori. Incearca aici, URL: 000webhost.com
bafta

duvibv Mesaje:14
Da am observat ca pe host nu mai da nici o eroare! Dar afecteaza intr-un fel?Multumesc

Subiecte similare