Error when showing images blob of mysql in the PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rafaelassmann
Forum Newbie
Posts: 1
Joined: Thu Jun 20, 2013 6:35 am

Error when showing images blob of mysql in the PHP

Post by rafaelassmann »

Good day, I am new in the forum and I am with a problem when showing images blob of mine mysql in my page php, am recording correctly, searching of the flock of database, but the images do not appear, follow the screen of as they are being shown:

problem:

Image

mysql:

Image

upload:

Code: Select all

<center>
    <div id="login">
<h1>Upload da Imagem ...</h1><p><br>
<?php
if ( !isset($_FILES['userFile']['type']) ) {
   die('<p>Nenhuma imagem selecionada !</p></body></html>');
}
?>
Visualize a baixo as informações da foto:<br /><br />
Nome: <?php echo $_FILES['userFile']['name'] ?><br />
Tamanho: <?php echo $_FILES['userFile']['size'] ?> bytes<br />
Tipo: <?php echo $_FILES['userFile']['type'] ?></p><br>
<strong>

<?php
require("../Connections/config.php");
// Validate uploaded image file
if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) {
   die('<p>Somente as fotos com o formato: .gif | .png | .x-png  e .jpeg são compatíveis com nosso sistema de fotos.</p></body></html>');
} else if ( strlen($_POST['altText']) < 5 ) {
   die('<p>Por favor digite uma descrição de no mínimo 5 (cinco) caracteres !</p></body></html>');
} else if ( $_FILES['userFile']['size'] > 1048576 ) {
   die('<p>Desculpe, este arquivo é muito grande, insira fotos com menos de 1 MB !</p></body></html>');
// Connect to database
} else if ( !($link=mysql_connect($hostname_config, $username_config, $password_config)) ) {
   die('<p>Erro de conexão com o banco de dados, por favor entre em conta +55 (51) 9955-5141.</p></body></html>');
} else if ( !(mysql_select_db($database_config)) ) {
   die('<p>Erro ao selecionar o banco de dados, por favor entre em conta +55 (51) 9955-5141.</p></body></html>');
// Copy image file into a variable
} else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) {
   die('<p>Erro ao abrir o arquivo temporário, tente novamente !</p></body></html>');
} else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) {
   die('<p>Erro ao ler o arquivo temporário, tente novamente !</p></body></html>');
} else {
   fclose ($handle);
   // Commit image to the database
   $image = mysql_real_escape_string($image);
   $categoria = htmlentities($_POST['categoria']);
   $alt = htmlentities($_POST['altText']);
   $query = 'INSERT INTO image (type,name,categoria,alt,img) VALUES ("' . $_FILES['userFile']['type'] . '","' . $_FILES['userFile']['name']  . '","' . $categoria . '", "' . $alt  . '","' . $image . '")';
   if ( !(mysql_query($query,$link)) ) {
      die('<p>Erro ao salvar a imagem no banco de dados, tente novamente !</p></body></html>');
   } else {
      die('<p>Imagem inserida com sucesso !</p></body></html>');
   }
}
?>
show:

Code: Select all

<?php
$link = mysql_connect($hostname_config, $username_config, $password_config);
mysql_select_db($database_config);
$query = 'SELECT type,img FROM image WHERE id = "' . $_GET['id'] . '"';
$result = mysql_query($query,$link);
$row = mysql_fetch_assoc($result);
header('Content-Type: ' . $row['type']);
echo $row['img'];
?>
at last shows here as wants my images:

Code: Select all

<?php
if ( !($link=mysql_connect($hostname_config, $username_config, $password_config)) ) {
   die('<p>Error connecting to database</p></body></html>');
} else if ( !(mysql_select_db($database_config)) ) {
   die('<p>Error selecting database</p></body></html>');
} else {
   $query = "SELECT id,name,alt FROM image";
   if ( !($result = mysql_query($query,$link)) ) {
      die('<p>Error reading database</p></body></html>');
   } else {
      for ( $i = 0 ; $i < mysql_num_rows($result) ; $i++ ) {
        $row = mysql_fetch_assoc($result);
        echo '<img src="show.php?id=' . $row['id'] . '" alt="' . $row['alt'] . '" title="' . $row['name']  .'"/>  ' . "\n";
      }
   }
}
?>
some suggestion? tks!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Error when showing images blob of mysql in the PHP

Post by AbraCadaver »

Is magic_quotes_gpc enabled on your server?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Error when showing images blob of mysql in the PHP

Post by requinix »

Code: Select all

<?php
$link = mysql_connect($hostname_config, $username_config, $password_config);
mysql_select_db($database_config);
$query = 'SELECT type,img FROM image WHERE id = "' . $_GET['id'] . '"';
$result = mysql_query($query,$link);
$row = mysql_fetch_assoc($result);
header('Content-Type: ' . $row['type']);
echo $row['img'];
?>
Comment out the header() and go to the image URL directly. Do you see any error messages? Do you see binary output?

You're also open to SQL injection and the upload script isn't fully secured.
Post Reply