Jump to content

PHP Upload Image Turns Into Gibberish


Wayniac

Recommended Posts

I realize its not the best thing to do, but for the sake of learning I am uploading an image to a MySql database, which works fine. But when I try and retrieve it, many lines of 3Éí5æ/Hÿ�M‘þs?ô’_ó¤¦Èÿ�9ŸúIláà7 stuff appears.

 

Not sure if its a quick fix or not, should you need the code, just let me know.

Link to comment
Share on other sites

Thank you so much for your reply!

 

Below is how I am uploading it: (Did you require the HTML?)

<?php
//error_reporting(0);
include("config.php");
// Connect to database

if ($_REQUEST[completed] == 1) {
$newname = uniqid("whc").".jpg";
move_uploaded_file($_FILES['imagefile']['tmp_name'],
"../upload/$newname");
} 

  if(isset($_POST['submit']))

  {

  $newname = mysql_real_escape_string(file_get_contents("../upload/$newname"));
  //$newname = mysql_real_escape_string($_POST['imgdata']);

      $title = mysql_real_escape_string($_POST['title']);

      $age = mysql_real_escape_string($_POST['age']);

      $testimonial = mysql_real_escape_string($_POST['testimonial']);

              if(!$title){

                     echo "Error: Entry title is a required field.";

                     exit();

              }

$result = mysql_query("INSERT INTO album (imgdata, title, dtime, age, testimonial)

                       VALUES ('$newname','$title',NOW(),'$age','$testimonial')",$connect);

          echo "<b>Entry UPDATED Successfully!<br>You'll be redirected to View Album page after<br>(4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=index.php>";

  }

else

  {



      ?>

 

<?php
include("config.php");

$image = stripslashes($_REQUEST[image]);
$result = mysql_query("select * from album where albumid=\"".
addslashes($image).".jpg\"");
$myrow = mysql_fetch_assoc($result);
$imagebytes = $myrow[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
?>

 

Here is where the image is being displayed, or at least should be, all that is being displayed here is all that gibberish and the text entries which work fine.

 

			<?php

include("config.php");

        $albumid = $_GET['albumid'];
        
        $result = mysql_query("SELECT * FROM album WHERE albumid='$albumid' ",$connect);
        while($myrow = mysql_fetch_assoc($result))
             {
               echo "<b>Title: </b>";
               echo $myrow['title'];
               echo "<b><br>Posted: </b><i>";
               echo $myrow['dtime'];
		   echo "</i><b><br>Age (Years):</b>: ";
               echo $myrow['age'];
               echo "<br><br><a href=\"javascript:self.history.back();\"><-- Go Back</a>";
		   echo "<b><br><br><br><br>Image: <br><br></b>";
		   echo "<img src=get_image.php?image={$myrow['imgdata']}>";
             }

?>

 

Once again, thank you for offering your assistance.

Link to comment
Share on other sites

What is the column type of the MySQL column you are uploading it to? Is it blob, if not it should be.

 

This SQL statement is bad:

 

$result = mysql_query("select * from album where albumid=\"".
addslashes($image).".jpg\"");

 

It should be:

$result = mysql_query("select * from album where albumid='".
addslashes($image).".jpg'");

 

As string values inside of MySQL need to be encapsulated in Single quotes only to be valid syntax.

Link to comment
Share on other sites

Well the text you are getting is the correct text as that is the binary code of the image. Looking at the code the only thing I can think of is there is some reason the content-type is not taking so the browser does not know it is suppose to display it as an image instead of the binary code. Perhaps try not using mysql_real_escape_string on the image when you input it into the database and see if that makes any difference?

 

Other than that I am not sure.

Link to comment
Share on other sites

I believe I may have found why, but keep getting a parse error. I changed:

echo "<img src=get_image.php?image={$myrow['imgdata']}>";

 

To give them quotes around the src:

echo "<img src="get_image.php?image={$myrow['imgdata']}">";

 

But now this parse error pops up??

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /mnt/w0712/d16/s01/b02c73a9/www/example.ca/testalbum/read_more.php on line 374

Link to comment
Share on other sites

That corrected the parse error premiso, thank you, but unfortunately your right, it never fixed the problem.

 

Hi Stephen, could you explain to me or give an example on how I might accomplish such? I never heard about base64_encode before... (still fairly new and learning)

Link to comment
Share on other sites

  if(isset($_POST['submit']))

  {

     $newname = base64_encode(file_get_contents("../upload/$newname"));
     //$newname = mysql_real_escape_string($_POST['imgdata']);

 

For the displaying file:

<?php
include("config.php");

$image = stripslashes($_REQUEST[image]);
$result = mysql_query("select * from album where albumid=\"".
addslashes($image).".jpg\"");
$myrow = mysql_fetch_assoc($result);
$imagebytes = $myrow[imgdata];
header("Content-type: image/jpeg");
print base64_decode($imagebytes);
?>

 

Is what he means. Not sure if it will work, but I do remember something like this is the past workings. Worth a shot.

Link to comment
Share on other sites

That did something... It made it so that all the gibberish binary content disappeared but the small image icon 24x24 pixels is there by itself now. When I right click on it and view the pictures properties it says unknown size.

 

Really appreciate the help.. REALLY DO!

Link to comment
Share on other sites

I was getting confused on your display image script (because I thought it actually took image data through the url, which couldn't have worked), but basically what premiso said is correct. Not sure why it outputs a small icon :\ Can you PM or post some of your data in the album table?

Link to comment
Share on other sites

Ah, I meant the actual data itself (like the base64_encrypted data). Right now you could try using this code instead of the old one:

(below)

 

Try creating another album and see if you get the same little icon (and check if the information is different in the table for the two different images).

 

EDIT:

Actually, after looking at it, this ($result = mysql_query("select * from album where albumid='".addslashes($image).".jpg'");) would not work if your "albumid" is an auto_incrementing integer. Try using these:

<?php
include("config.php");

$image = stripslashes($_GET['image']);
$result = mysql_query("select * from album where albumid='".
addslashes($image)."'");
$myrow = mysql_fetch_assoc($result);
$imagebytes = $myrow['imgdata'];
header("Content-type: image/jpeg");
print base64_decode($imagebytes);
?>

(the display script):

<?php

include("config.php");

        $albumid = $_GET['albumid'];
        
        $result = mysql_query("SELECT * FROM album WHERE albumid='$albumid' ",$connect);
        while($myrow = mysql_fetch_assoc($result))
             {
               echo "<b>Title: </b>";
               echo $myrow['title'];
               echo "<b><br>Posted: </b><i>";
               echo $myrow['dtime'];
            echo "</i><b><br>Age (Years):</b>: ";
               echo $myrow['age'];
               echo "<br><br><a href=\"javascript:self.history.back();\"><-- Go Back</a>";
            echo "<b><br><br><br><br>Image: <br><br></b>";
            echo "<img src=\"get_image.php?image={$myrow['albumid']}\">";
             }
          
?>

Link to comment
Share on other sites

Okay so I tried that and the second set has no data for the image. The other entries still work fine, but 0 bytes comes up in the database under the imgdata field.

 

This only happened when I added the base64_decode / encode. Before this, it was uploading data, but giving me that binary mumbo jumbo...

Link to comment
Share on other sites

How do you say I love you in EVERY different language??? That worked!!! You can only imagine how I feel now... I scared my dog Ollie by screaming the loudest YES ever. Thank you all, is there any information I can post to help future viewers?

Link to comment
Share on other sites

Can't think of anything else, but I recommend you check your personal messages. Also, there is a "Solved" button somewhere around your topic you can press xD. I think it's at the very bottom, but I haven't posted any topics too recently so I don't remember.

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.