I want to display profile image for each account as the user loggs into their profile.Similar to the social website profile image.
I've managed to store the images into the database using php.
Problem is i cant display images at all.the rest of the data display fine.

I have the following code which cant work:

  1. main profile page has the following script:

<?php echo '<img src="XXXXX_XXX.php?id=$Current_Person_Id">';


2.the image page has the following script:


$result = mysqli_query($link,"select from *** where Person_Id=$Current_Person_Id ");

////starting the inner validation

//else if($result){

while($row = mysqli_fetch_array($result))
{
//$row_color = ($row_count % 2) ? $color1 : $color2;
$content=$row['Photo_Content'];
$name=$row['Photo_Name'];
$type=$row['Photo_Type'];
$size=$row['Photo_Size'];
$row_count++;
}
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content; }


    I am not sure whether this is the only problem, but you are currently sending the file as an attachment. Remote te content-disposition and it should improve.

    What errors do you get after that, when you just open de url for the image?

      I've commented out the " content-disposition " but it doesnt show the image.
      It only shows the "image absent" icon.

      Do you have a code that I can use to try and display the images in php?...I'm storing the images in mysql.

        nope, I do not store images in a database.

        As before: What errors do you get when you open just the image?

          You are trying to have the user download the image.

          Replace

          header("Content-Disposition: attachment; filename=$name");

          With a [man]header[/man] specifying the content type ... i.e. image/jpeg

            Threads merged - please don't post multiple threads for the same issue.

            Why are you using a while() loop? You can only display one image at a time, so you should get rid of the loop as well as the non-existant variable $row_count that you try to increment.

            As leatherback asked above, what if you visit the URL to the image directly? Do you see any PHP errors being outputted?

              I've tried this but no luck yet.

              header("Content-length:$size");
              header("Content-type:$type");
              header("Content-Disposition: inline; filename=$name");
              echo $content;

              Note:

              The variables:$size,$type,$name are stored in database too.
              I'm able to print all of them except the content.
              Could it be my php configuration orsomething to do with images?

                What if you add this:

                print_r(array_keys($row))

                . Does it show 'Photo_Content' as an existing key?

                  when I add print_(array_keys($row));.....then I get the following error:

                  Warning: array_keys() [function.array-keys]: The first argument should be an array in C:\Program Files\Apache Group

                    Read the suggestions.

                    • What errors do you get when you view just the image
                    • Remove the content-disposition and replace by a heder identifying it as an image

                      I added the following print_(array_keys($row)); and got the image attached here

                        the file attached is just a "snag it " image of the browser screen.
                        I still havent been able to display the image..

                          wuodpam wrote:

                          I still havent been able to display the image..

                          Right, but as has been suggested numerous times, what happens if you go to the URL of the (nonfunctional) image PHP script? Do you see a PHP error message? Can you give us the URL to this script?

                          (Also, now that we know that the Photo_Content key is indeed correct, you can remove the print_r() statement).

                            I'h currently developing the application on localhost...so it wont be possible to access it from outside...but here is my entire code for image page:

                            session_start();

                            error_reporting (E_ALL ^ E_NOTICE);

                            if(!empty($GET['id'])){
                            $Person_Id=$
                            GET['id'];
                            }
                            if(empty($_GET['id'])){
                            $Person_Id="";
                            }

                            $Person_Name=$SESSION['Person_Name'];
                            //$
                            SESSION['Person_Last_Name']= $Person_Last_Name;
                            $Person_City=$SESSION['Person_City'];
                            $Person_State=$
                            SESSION['Person_State'];
                            $Current_Id=$_SESSION['$Current_Person_Id'];

                            //include('global_vars.php');
                            $dbServer='localhost';

                            // username and password to log onto db server
                            $dbUser='XXX';
                            $dbPass='XXXXX';

                            // name of database
                            $dbName='XXXXX';

                            $link = mysqli_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");

                            // print "Connected successfully<br>";
                            mysqli_select_db($link,"cpool") or die("Could not select database");

                            //if($_GET['id']==$Current_Id){

                            //$result = mysqli_query($link,"select * from person where Person_Id=$Current_Id and Person_Name='$Person_Name'");

                            $result = mysqli_query($link,"select Photo_Content,Photo_Name,Photo_Type,Photo_Size from person where Person_Id='$Person_Id'");

                            ////starting the inner validation

                            //else if($result){
                            $row_count=0;

                            while($row = mysqli_fetch_array($result))
                            {
                            //$row_color = ($row_count % 2) ? $color1 : $color2;
                            $content=$row["Photo_Content"];
                            $name=$row['Photo_Name'];
                            $type=$row['Photo_Type'];
                            $size=$row['Photo_Size'];

                            //print_r(array_keys($row));

                            $row_count++;
                            }
                            // print_r(array_keys($row)) ;

                            //header("Content-length:$size");

                            header("Content-type:$type");
                            //header("Content-Disposition:filename=$name");
                            //header('filename=$name');
                            //Content-Disposition: attachment;
                            echo $content;


                              I got this error(on snag it image) again as I tried to display directly on the image file.I cant make a thing of these weird xters.

                              I also used "addslashes()" while inserting the image content into the database.

                              The image is fine in the database as i can click it from the content column and view it...only the php/browser display part is my pain...

                                wuodpam wrote:

                                I also used "addslashes()" while inserting the image content into the database.

                                [man]addslashes/man should never be used to sanitize data for database input; instead, you should use a database-specific function such as [man]mysql_real_escape_string/man (as the two functions are not equivalent).

                                Do you have display_errors set to On and error_reporting set to E_ALL? The text in the snapshot you posted does indeed appear to be binary data from an image file. Are there any spaces above or below the <?php ?> tags that are perhaps being outputted, corrupting the binary data?

                                Could you perhaps save the output of the image script to a .txt file and post it for us to examine?

                                  I'm eventually able to display the image on the image file(ONLY).
                                  The problem was to do with the stripslashes...this code did the magic:

                                    echo stripslashes($content);

                                  However the code below cannot display it on the profile still....is this calling code correct?:
                                  <?php echo '<img src="display_image.php?id=$Current_Person_Id">';

                                    wuodpam wrote:

                                    this code did the magic:

                                    echo stripslashes($content);

                                    If you're having to add stripslashes() on content coming from the DB, then you have problems with inserting the data. Check if magic_quotes_gpc is enabled, and, if so, DISABLE it immediately. You should try to re-insert the data properly in your DB as well after doing so.

                                    wuodpam wrote:

                                    is this calling code correct?

                                    Well, on the actual page you're trying to display the image, it really has nothing to do with PHP - if going to the actual URL of the image script displays the image, then yes, you should be able to simply output the appropriate HTML to display the image. Perhaps check the HTML source of the page and ensure that the IMG element looks correct (e.g. the URL was correctly outputted)?

                                      If you do a "view source" of the page you will see where the problem is.

                                      <?php echo '<img src="display_image.php?id=$Current_Person_Id">';
                                      needs to be
                                      <?php echo '<img src="display_image.php?id='.$Current_Person_Id . '">';

                                        Woops, nice catch HalfaBee - completely missed the single-quoted string!

                                          Write a Reply...