erok Posted July 13, 2013 Share Posted July 13, 2013 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 comment Share on other sites More sharing options...
birbal Posted July 13, 2013 Share Posted July 13, 2013 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 comment Share on other sites More sharing options...
erok Posted July 14, 2013 Author Share Posted July 14, 2013 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 comment Share on other sites More sharing options...
Labtec Posted July 14, 2013 Share Posted July 14, 2013 (edited) 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 July 14, 2013 by Labtec Link to comment Share on other sites More sharing options...
erok Posted July 16, 2013 Author Share Posted July 16, 2013 Hi people, Actually, code is about uploading image to database table by using blob. So image is in the database table. I need to retrieve image from database table and display it. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 16, 2013 Share Posted July 16, 2013 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 comment Share on other sites More sharing options...
erok Posted July 16, 2013 Author Share Posted July 16, 2013 Hi justsomeguy, I view computer machine language script, instead of picture. What possible am i missing? erok Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 You're not sending the correct headers. Specifically, you need a content-type header. If you want to show a download box then you also need a content-disposition header. Link to comment Share on other sites More sharing options...
erok Posted July 17, 2013 Author Share Posted July 17, 2013 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 ; 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 comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 What content type are you sending? Link to comment Share on other sites More sharing options...
erok Posted July 17, 2013 Author Share Posted July 17, 2013 I sent JPEG Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 "JPEG" isn't a valid content type. There are some common ones listed here: https://en.wikipedia.org/wiki/Internet_media_type#Type_image Link to comment Share on other sites More sharing options...
erok Posted July 17, 2013 Author Share Posted July 17, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 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 comment Share on other sites More sharing options...
erok Posted July 17, 2013 Author Share Posted July 17, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2013 Share Posted July 17, 2013 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 comment Share on other sites More sharing options...
erok Posted July 18, 2013 Author Share Posted July 18, 2013 . . $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 comment Share on other sites More sharing options...
birbal Posted July 18, 2013 Share Posted July 18, 2013 (edited) $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 July 18, 2013 by birbal Link to comment Share on other sites More sharing options...
erok Posted July 26, 2013 Author Share Posted July 26, 2013 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 comment Share on other sites More sharing options...
birbal Posted July 26, 2013 Share Posted July 26, 2013 $caption is string , image is binary data. you need imagettftext() to write text in image. Link to comment Share on other sites More sharing options...
erok Posted July 26, 2013 Author Share Posted July 26, 2013 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 comment Share on other sites More sharing options...
erok Posted August 2, 2013 Author Share Posted August 2, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2013 Share Posted August 2, 2013 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 comment Share on other sites More sharing options...
erok Posted August 2, 2013 Author Share Posted August 2, 2013 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 comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2013 Share Posted August 2, 2013 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 comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now