Jump to content

upload image into database table


Recommended Posts

The following code is not working.
even php error is not commenting at all. Just reading " something wrong " which is my statement in the code.
i also tried ' sprintf ' function also. I failed.
- HTML FORM ;
<html>
<body>
<form enctype="multipart/form-data" method="post" action="image_insert0.php" >
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<p id="phup">Click browse button to choose photo:<br /><input type="file" name="image" /></p>
<br />
<textarea id="photoup" type="text" name="message" maxlenght="200" rows="10" col="85"></textarea>
<br />
<br />
<input type="submit" name="formsubmit" value="upload here" size="35" />
</form>
</body>
</html>
- Upload into phpmyadmin database code ;
<html>
<body>
<?php
mysqli_select_db($con, "theboatcare") or die("Could not find DB");
if (isset($_POST['formsubmit']))
{
$image = $_FILES['image'] ;
print_r($image);
$name = $_FILES['image']['name'] ;
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])) ;
$query = "INSERT INTO images (name, image) VALUES ('', '{$name}', '{$image}')";
$result = mysqli_query($con, $query) or die('something wrong' . mysql_error());
$id = (int) mysqli_insert_id($result);
header('Location: image_view.php?id=' . $id);
exit;
}
?>
</body>
</html>
Any direction appreciated
erok
Link to post
Share on other sites

i dont think mysql_error() will work properly with mysqli extension. First step would be to use mysqli_error() to pin point the problem.

Link to post
Share on other sites

Hi everyone, Thanks birbal.

mysqli_error() made difference.

error was unneccesary first column ''

correct code is /* $query = "INSERT INTO images (name, image) VALUES ('{$name}', '{$image}')"; */

still i have some more work to do.I need to retrieve image from database and display it

Thanks again

erok

Link to post
Share on other sites

All you need to do is insert the filename into the database and send your file to a folder.

 

Then retrieve the files by matching the filenames.

 

So in your database you will have something like:

 

ID IMG_NAME

1 img1.jpg

2 img2.jpg

3 img3.jpg

 

Inside the folder where you sent the file, it will contain the actual file with the same names.

 

You then just need to save the path to the folder,in a variable and when you do your looping of the images after your query, just concatenate the filenames onto the end of the folder path.

 

Kind regards,

 

Lab.

Edited by Labtec
Link to post
Share on other sites

Get the data from the table, and use PHP to output headers that tell the browser it is an image, then output the data. You need to send the correct headers according to the type of the original image.

Link to post
Share on other sites
This is the section of the code i run ;
A) $sql = stripslashes("SELECT type, image FROM images WHERE id= {$imageid}") ;
// the result of the query
$lresult = mysqli_query($con, $sql) or die("Problem : " . mysqli_error($con));
$id = (int) mysqli_insert_id($con);
$imagedata = mysqli_fetch_array($lresult, MYSQLI_ASSOC) ;
header("Content-type: . {$imagedata['type']}") ;
echo $imagedata ;
This is the error message i got
<b>Notice</b>: Array to string conversion in <b>C:xampphtdocsimage_insert0.php</b> on line <b>39</b><br />Array</body>
If i type ;
B) echo $imagedata['image']; instead of echo $imagedata ;
I view computer machine language script

 

I believe i am missing some functions ;

I would appreciate any direction

Link to post
Share on other sites

This is the flow i tried so far ;

$query = "INSERT INTO images (name, type, image) VALUES ('{$name}', '{$type}', '{$image}')";

.

.

.

$sql = stripslashes("SELECT type, image FROM images WHERE id= {$imageid}") ;

.

$lresult = mysqli_query($con, $sql)

$imagedata = mysqli_fetch_array($lresult, MYSQLI_ASSOC) ;

 

header("Content-type: . {$imagedata['type']}") ;

It did not work out. After justsomeguy' suggestion

I switch to

header("Content-type: image/jpeg");

 

Hoping might make difference.

When i run, unfortunately same problem. I view computer machine language script instead of picture.

Something i am missing for sure.

Link to post
Share on other sites

If you're seeing the binary data then it sounds like there's a problem with the headers. Do you have this online anywhere? Have you used your browser's developer tools to look at the headers that are actually being sent?

Link to post
Share on other sites

it is not binary data like ( 00110110). it is more like " %ny#ejyy<":/$@*^ ..." two pages long.

Actually i tried copy paste to show you what message is like. I could not copy/paste the message.

I have not had this before. This is the first time this happen when i tried to display the picture.

I still feel like i am missing some functions.

Link to post
Share on other sites

 

 

it is not binary data like ( 00110110). it is more like " %ny#ejyy<":/$@*^ ..."

Yeah, that's binary data being interpreted as text. That's what the image is, binary data, that's what you have in the database. That's the B in blob. If the browser is displaying the image data instead of the actual image, then the browser doesn't know it's an image, the server is probably telling the browser it is HTML or text so the browser displays the binary data as if it was text. Headers tell the browser that it is an image, so it sounds like you are not sending the correct headers. If you put it online then I can check, or you can also use your browser's developer tools to look at the actual headers that the server sends. That's how to troubleshoot this, you need to verify the headers that are actually being sent.

Link to post
Share on other sites

.
.
$sql = stripslashes("SELECT type, image FROM images WHERE id= {$imageid}") ;
// the result of the query
$lresult = mysqli_query($con, $sql) or die("Problem : " . mysqli_error($con));
$id = (int) mysqli_insert_id($con);
$imagedata = mysqli_fetch_array($lresult) ;
$imagedata = imagecreatefromstring($imagedata) ;
header("Content-type: image/jpeg") ;
echo $imagedata['image'] ;
}
mysqli_close($con) ;
?>
</body>
</html>
the picture i uploaded is a jpg
when i ran the code, I got this error message ;

<b>Notice</b>: Array to string conversion in <b>C:xampphtdocsimage_insert0.php</b> on line <b>38</b><br /> <br /> <b>Warning</b>: imagecreatefromstring(): Empty string or invalid image in <b>C:xampphtdocsimage_insert0.php</b> on line <b>38</b><br />

Link to post
Share on other sites
$imagedata = imagecreatefromstring($imagedata) ;

 

here, $imagedata is an array. you would like to pass the binary data (specify the cloumn of the image data) as param which probably is $imagedata['image']

also that particular function returns a image resource. echoing it will not show up the picture. you need to use http://in3.php.net/manual/en/function.imagejpeg.php or any variant of image*() which is dependent on image type.

 

If your scripts generates any error before the image get displayed it would send the default header already. as header can be sent only once so next time if you try to send header again it wont send it. You may like to read this for header related problem http://w3schools.invisionzone.com/index.php?showtopic=44106#entry245750

 

Even if you pass the correct header when you display a image there should not be any data other than the binary data, otherwise image will be corrupted.

Edited by birbal
Link to post
Share on other sites
Hi all,
thank you for your help.
I am able to display picture
i used the following code and the program ran fine.
<img src="image.php?id=<?=$imageId ?>
.
.
header("Content-type: image/$image_type") ;
imagejpeg($im) ;
imagedestroy($im) ;
I decided to add some more features and include a text under the picture as a caption.
I am able to insert text into table as a new column.
using $caption= $_POST['message'] ;
I am unable to get text under the picture.
I used following two separate codes, neither of these brought the text under the picture.
only able to display picture.
header("Content-type: image/$image_type") ;
imagejpeg($im) ;
imagedestroy($im) ;
echo $caption
or
header("Content-type: image/$image_type") ;
imagejpeg($im) ;
echo $caption
imagedestroy($im) ;
What am i missing?
direction appreciated
erok
Link to post
Share on other sites

$caption is string , image is binary data. you need imagettftext() to write text in image.

Link to post
Share on other sites
i tried something like this. it did not work. I am able to insert the text into table. i could not take it from table and put on a page together with image.
header("Content-type: image/$image_type") ;
$black = imagecolorallocate($im, 0, 0, 0) ;
$caption ;
$font = 'arial.ttf' ;
imagettftext($im, 20, 0, 10, 20, $black, $font, $caption) ;
imagejpeg($im) ;
imagedestroy($im) .
I tried inefficient way, selecting text(caption) from same table separate and put the text on a page under the image. in this way it works.
Program have to connect to server one more time to get the text .It does not sound good idea if website has heavy image traffic. But at least it works
Link to post
Share on other sites

Hi all,

I am able to insert .doc file into database using the same code i used for images.

I am trying to get .doc file from database and display it on browser.

 

The code i used for displaying images does not work for .doc files.

- Here is the code i tried to display .doc file;

$bunu = base64_decode($dene) ;
$content = imagecreatefromstring($bunu) ;
if ($content !== false)
{
header("Content-type: {$type}") ;
imagejpeg($content) ;
imagedestroy($content) ;
}
else {
echo 'an error occured. ' ;
}
Is there any specific function i can use for displaying .doc files or pdf ?
any direction appreciated.
erok
Link to post
Share on other sites

Image functions are not going to work with any data except image data. If you want to download an arbitrary binary file all you need to do is send a content-type header and then the binary data. If can also send a content-disposition header if you want to show a download box with a certain filename.

Link to post
Share on other sites

I sent content-type header and then binary data

 

$type = $imageData['type'] ;

 

header("Content-type: {$type}") ;

imagejpeg($content) ;
imagedestroy($content) ;

 

i see " application/msword " inserted in at the 'type' column in the database.

 

so, i am assuming i have no problem with header "Content-type" .

am i missing a function?

Link to post
Share on other sites

Content type looks fine. You're still trying to output a jpeg image though, because you're still using that image function. Do not use image functions for anything other than images. They are not general-purpose binary functions, they are specifically for handling image data, nothing else. You're outputting a corrupted image with a Word content type.

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...