Jump to content

reading a binary image file from database


contra10

Recommended Posts

hey i want to read an image file i uploaded into database

 

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    $ii = "{$row['content']} <br>";
       echo($ii);
} 
    echo($ii);
?>

 

how di i see the image

Link to comment
Share on other sites

am i srtof on the right track

<?php
if(is_numeric($_GET['id'])){

$id = $_GET['id'];

  $insert1= "SELECT * FROM users WHERE id = '$id'";
$idnu = mysql_query($insert1) or die(mysql_error());

while ($idn = mysql_fetch_assoc($idnu))
{
$idnumb= "{$idn['id']}";
}
}

header("Location: http://localhost/photoupload/index.php?userphotoid=$idnumb");

$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    $ii = "{$row['content']} <br>";
       echo($ii);
} 
    echo($ii);
?>

Link to comment
Share on other sites

No, you do not want to the location.

 

The header tag has many more uses. Look at the header site for other examples of "Content-type:"

 

<?php
if(is_numeric($_GET['id'])){
   $id = $_GET['id'];

   $insert1= "SELECT * FROM users WHERE id = '$id'";
   $idnu = mysql_query($insert1) or die(mysql_error());

   while ($idn = mysql_fetch_assoc($idnu)) {
       $idnumb= "{$idn['id']}";
   }
}
   
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);


// We'll be outputting a jpeg
header('Content-type: application/jpeg'); // this needs to be changed to the actual type of the image, png, gif, jpeg etc

echo $row['content'];

?>

 

I do not know if that will work but that is the basic idea. Read through the WHOLE page of header and you will see plenty of examples, especially in the user remarks. Remember the first example on PHP.NET is not always the right one or the example you need, so read through the WHOLE page.

Link to comment
Share on other sites

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

// We'll be outputting a jpeg
header("Content-type: img/pjpeg"); // this needs to be changed to the actual type of the image, png, gif, jpeg etc

echo $row['content'];

?>

Link to comment
Share on other sites

To fix the error in the code I posted:

 

// We'll be outputting a jpeg
header('Content-type: image/jpeg'); // this needs to be changed to the actual type of the image, png, gif, jpeg etc

echo $row['content'];

 

I just had the content-type wrong, should be image/jpeg.

Link to comment
Share on other sites

just to note i did that prviously, didn't work...but im using the resource website that you found and on page 7 it basically says this

<?php
    // the upload function
    function upload(){

    if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

        // check the file is less than the maximum file size
        if($_FILES['userfile']['size'] < $maxsize)
            {
        // prepare the image for insertion
        $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
        // $imgData = addslashes($_FILES['userfile']);

        // get the image info..
          $size = getimagesize($_FILES['userfile']['tmp_name']);

        // put the image in the db...
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error()); 

        // our sql query
        $sql = "INSERT INTO testblob
                ( image_id , image_type ,image, image_size, image_name)
                VALUES
                ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";

        // insert the image
        if(!mysql_query($sql)) {
            echo 'Unable to upload file';
            }
        }
    }
    else {
         // if the file is not less than the maximum allowed, print an error
         echo
          '<div>File exceeds the Maximum File limit</div>
          <div>Maximum File limit is '.$maxsize.'</div>
          <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
          <hr />';
         }
    }
?>

with the exception that i put my databse connection in there... when i submit the data does not enter the table and i get this mesage

File exceeds the Maximum File limit
Maximum File limit is 
File Array is Array bytes

--------------------------------------------------------------------------------

Thank you for submitting

 

any ideas?

Link to comment
Share on other sites

I did not really mean for you to actually implement the whole script. More of use it as a guideline, particulary the display part.

 

<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());

        // select our database
        mysql_select_db("testblob") or die(mysql_error());

        // get the image from the db
        $sql = "SELECT image FROM testblob WHERE image_id=0";

        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
    }
    else {
        echo 'Please use a real id number';
    }
?>

 

As seen there, my implementation of the header content-type was correct.

 

So back to your old code:

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/jpeg");
echo($row['content']);
?>

 

And on a huge SIDE NOTE!!!::::::

The image must be a JPEG type that was inserted into the DB for this to work. If it was png it should be image/png or gif it should be image/gif.

 

If you do not know what type it was I would suggest looking into storing the type when it is uploaded in the DB so you can call the correct content-type when the time is necessary.

Link to comment
Share on other sites

yes u were definatly right with the content...

i have

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/jpeg");
echo($row['content']);
?>

 

i also tried

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/pjpeg");
echo($row['content']);
?>

 

since it was pjpeg in the database,

 

i still get

ÿØÿàJFIFÿÛC     "" $(4,$&1'-=-157:::#+?D?8C49:7ÿÛC 7%%77777777777777777777777777777777777777777777777777ÿÀ00"ÿÄÿÄ.!1AQq"#B¡±$2b‘ÿÄÿÄ!1QÿÚ ?ÎQ‘/Ú¼‰`(ál‡Gî#oZÓý-%¬KYdU'ÎŽ é ðEj¦W$û“Ú$[#¼‘ÝËwb×üVwdo’fظ0Á4Ÿ Ø¿ÕÅ-âùûzÜsC.âºáí¤µ ø§±ÜФ;SV;Ué]ˆˆæÛPTíEFRlÜwªJJ1m’¾Y¦tÿWá±ø8g›1Ž UBK^±ä7í\‡TæSf™¬’sd…CôžçÔÛíUM6Ål$†‘4U¨¾ö=»ÿ´¸Tðc䑵…ïYê#ÓwdFvØì­E‘ÜÐœÔÜB«Æîƒâ[ŸQTÒâ §¾6GPºqõx>Âœ%ÒÊOª?ˆ§Jß oü«*Ì­¯ÒiŽÈŸâ€wX½#ÊéñFnFà_jŽ¯hÁ;©µÍ1Ée ƒ¸¥AeŒ¿'b-\ô„«2·*H«¬,ÀéÔqÈÛš£ÅÔÌ/õ·æŠãK5±nÿÙ 

 

i really don't know y this can't work for me, everything your doing is right and i thought it was correct

 

here's my whole code at the moment

 

<?php 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}


$query = "INSERT INTO image (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br>";
} 


?>
</body> 
</html>

<table border="1" cellpadding="10">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</form>
</table>

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/pjpeg");
echo($row['content']);
?>
</tr>
</form>
</table>
<body>
</html>

 

files can go in but they can't come out....

 

i don't know if this helps but i uploaded 2 images and already my id count is up to 4 i think each image is taking up 2 id fields

Link to comment
Share on other sites

i did this

<?php 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = mysql_real_escape_string($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = mysql_real_escape_string($fileName);
}


$query = "INSERT INTO image (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br>";
} 


?>
</body> 
</html>

<table border="1" cellpadding="10">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</form>
</table>

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/pjpeg");
echo($row['content']);
?>

 

and got the same thing as before

 

and i also did this

<?php 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error());

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
fclose($fp);


$query = "INSERT INTO image (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br>";
} 


?>
</body> 
</html>

<table border="1" cellpadding="10">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</form>
</table>

<?php
$query  = "SELECT content FROM image WHERE id= '1'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

header("Content-type: image/pjpeg");
echo($row['content']);
?>

 

and got "query error".

Link to comment
Share on other sites

"an got query error"

 

How does that help?

 

Change this:

mysql_query($query) or die('Error, query failed');

 

to

 

mysql_query($query) or die('Error, query failed!<br />' . mysql_error());

 

And report back that error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.