Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't get pictures to display properly

Status
Not open for further replies.

jgraves13

Programmer
Jan 31, 2007
42
US
Here is my segment of code I am using

$results = mysql_query("Select * from $table");
echo "<table border=\"1\">";
while ($row = mysql_fetch_assoc($results)):
echo "<tr><td>{$row['filename']}</td><td>{$row['filetype']}</td>
<td>{$row['filesize']}</td>
<td><img height=\"40\" width=\"40\" src=\"{$_SERVER['PHP_SELF']}?action=view&fileid={$row['fileid']}\" /> </td>
<td><a href=\"{$_SERVER['PHP_SELF']}?action=download&fileid={$row['fileid']}\">Download</a></td></tr>";
endwhile;
echo "</table>";

My images always come up broken. what am I doing wrong?
 
I should also add that I am using this simular code in another php file that uploads pictures to a database, and it displays a very small image to know that it uploaded. Add it works just fine... Now i trying to move this code to another php file, to show lots of small images that will eventually have a mouseover then pop up a larger image.... this done not work the images are broken??? Any ideas how to fix this?
 
this code
Code:
src=\"{$_SERVER['PHP_SELF']}?action=view&fileid={$row['fileid']}\"
indicates that your receiving script must have a branch that tests the incoming $_GET variable and diverts execution to a fileviewer.

such as

Code:
if (isset($_GET['action'])){
 if (isset($_GET['fileid'])){
   //somehow lookup the file location of that fileid
   //or db lookup
   
   $filename = ''//file address
   readfile($filename);
   exit(); //stop all remaining output

   //or
   echo mysql_result(mysql_query("select actualimage from table where fileid='".mysql_escape_string(trim($_GET['fileid']))."'"), 0,0);
 }
}

this is most likely where the code is going wrong so you would need to post this part of your code too, in order for us to help you debug it.
 
Okay, so this is the script that I am trying to use...When I use the download option I get a bunch of little squares with binary numbers in them...

$action = isset($_GET['action']) ? trim($_GET['action']) : "default";
switch ($action):
case "download":
if (isset($_GET['fileid'])):
$fileid = mysql_escape_string(trim($_GET['fileid']));
$result = mysql_query("Select * from $table where fileid='$fileid'") or die(mysql_error());
if (mysql_num_rows($result) !== 1):
echo "<br/>Error downloading file";
else:
$row = mysql_fetch_assoc($result);
header("Content-Type: {$row['filetype']}");
header("Content-Disposition: attachment; filename={$row['filename']};");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$row['filesize']}");
echo $row['contents'];
endif;
else:
//do nothing
endif;
break;
case "view":
if (isset($_GET['fileid'])):
$fileid = mysql_escape_string(trim($_GET['fileid']));
$result = mysql_query("Select * from $table where fileid='$fileid'") or die(mysql_error());
if (mysql_num_rows($result) !== 1):
echo "<br/>Error downloading file";
else:
$row = mysql_fetch_assoc($result);
header("Content-Type: {$row['filetype']}");
header("Content-Disposition: inline; filename={$row['filename']};");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$row['filesize']}");
echo $row['contents'];
endif;
else:
//do nothing
endif;
break;
default:


$results = mysql_query("Select * from $table");
echo "<table border=\"1\">";
while ($row = mysql_fetch_assoc($results)):
echo "<tr><td>{$row['filename']}</td>
<td>{$row['filetype']}</td>
<td>{$row['filesize']}</td>
<td><img height=\"40\" width=\"40\"src=\"{$_SERVER['PHP_SELF']}?action=view&fileid={$row['fileid']}\" /> </td>
<td><a href=\"{$_SERVER['PHP_SELF']}?action=download&fileid={$row['fileid']}\">Download</a></td></tr>";
endwhile;
echo "</table>";

endswitch;


All I really want to do is to load my photos from my
database into an array in php then call some kind of ramdom function to display them.... I would also like to link them to a photo album webpage.... one step at a time... Any help would be great... thanks and best regards
 
wow. that looks very familiar. Almost as if I wrote it myself ...

there are a couple of bugs in the code. they both relate to images have spaces in the filenames. this is a fixed variant. the changes are marked in red

note also that the data in the database needs to be good. i know that you have posted that you think this segment works ok but, if the code below does not work, then an examination of the upload code is next on the list.



Code:
 <?
$action = isset($_GET['action']) ? trim($_GET['action']) : "default";
  switch ($action):
   case "download":
    if (isset($_GET['fileid'])):
     $fileid = mysql_escape_string(trim($_GET['fileid']));
     $result = mysql_query("Select * from $table where fileid='$fileid'") or die(mysql_error());
      if (mysql_num_rows($result) !== 1):
       echo "<br/>Error downloading file";
      else:
       $row = mysql_fetch_assoc($result);
        header("Content-Type: {$row['filetype']}");
        header("Content-Disposition: attachment; filename=[red]\"[/red]{$row['filename']}[red]\"[/red];");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: {$row['filesize']}");
       echo $row['contents'];
       endif;
    else:
     //do nothing
    endif;
   break;
   case "view":
    if (isset($_GET['fileid'])):
     $fileid = mysql_escape_string(trim($_GET['fileid']));
     $result = mysql_query("Select * from $table where fileid='$fileid'") or die(mysql_error());
      if (mysql_num_rows($result) !== 1):
       echo "<br/>Error downloading file";
      else:
       $row = mysql_fetch_assoc($result);
       header("Content-Type: {$row['filetype']}");
       header("Content-Disposition: inline; filename=[red]\"[/red]{$row['filename']}[red]\"[/red];");
       header("Content-Transfer-Encoding: binary");
       header("Content-Length: {$row['filesize']}");
       echo $row['contents'];
       endif;
    else:
     //do nothing
    endif;
   break;
   default:


 $results = mysql_query("Select * from $table");
  echo "<table border=\"1\">";
   while ($row = mysql_fetch_assoc($results)):
    echo "<tr><td>{$row['filename']}</td>
          <td>{$row['filetype']}</td>
          <td>{$row['filesize']}</td>
          <td><img height=\"40\" width=\"40\"src=\"{$_SERVER['PHP_SELF']}?action=view&fileid={$row['fileid']}\" /> </td>
     <td><a href=\"{$_SERVER['PHP_SELF']}?action=download&fileid={$row['fileid']}\">Download</a></td></tr>";
   endwhile;
  echo "</table>";

 endswitch;
 ?>
 
LOL... your write, you did write this code... :) and it works like a charm. Here's a question for you, you taught me that I can use the commands: C; and echo<<<C
Why is it that sometimes I get error messages from this. If I replace the document with a backup or put the commmands in a different location it will work to differenciate between html and php... it's a weird clitch... and thanks you again, when I get home I will test this again too.
 
i think you are talking about the heredoc syntax
Code:
$var = <<<STR

This is a string
STR;

echo $var;

funny that you should say that you are getting glitches with this. I too have noticed over the past few weeks that strange errors relating to heredoc have crept in. have not had a chance to diagnose them but i suspect, in my case, that it is due to a change in php editor that happened around the same time. (in fact, i've just taken the time and it is due to dreamweaver indenting all code with two spaces)
 
jpadie,

Still no luck... I don't under stand what is wrong. I re-examined the upload file, the data is good. I can use the view and the download features just fine. I also even tried to run this script

<?php
if($id) {

@MYSQL_CONNECT("localhost","root","password");

@mysql_select_db("binary_data");

$query = "select * from $table where fileid=$id";
$result = @MYSQL_QUERY($query);

$data = @MYSQL_RESULT($result,0,"contents");
$type = @MYSQL_RESULT($result,0,"filetype");

Header( "Content-type: $type");
echo $data;
}
?>

then in index.php I tried to use <img src="getdata.php?id=3"> but this too failed... I am open to anything at this point.
 
stop using error suppressors in non-production code. re-run the code without the @ signs and report back on the errors you are getting.

unles you have register globals turned on, you should be testing for $_GET['id'] and not $id. If you do have register globals turned on, be aware of its security risks.
 
jpadie,

Here is a sample of what I am getting....

Warning: Cannot modify header information - headers already sent by (output started at /var/ in /var/ on line 134

Warning: Cannot modify header information - headers already sent by (output started at /var/ in /var/ on line 135

Warning: Cannot modify header information - headers already sent by (output started at /var/ in /var/ on line 136

Warning: Cannot modify header information - headers already sent by (output started at /var/ in /var/ on line 137
????JFIF????LEAD Technologies Inc. V1.01??C   %# , #&')*)-0-(0%()(??C   (((((((((((((((((((((((((((((((((((((((((((((((((((????"????9!1"AQaq?2??#?B??$C?R????????$!1"Qa#2A?? ???l?|r??a??C?`V?a ??N??QM37?T?61?CF?'?z6L???G(U8?4??hP?D?zQ`?OZK?t?@ $/????? u?P??y?=?(Q??@??po??? ??X?ZJJ???9)?D1???zQ?q? {Pp *)?$t?c?>???8C????P3!Q?<????u??R?? 8????*uOW?V18p}(??(_k?GRf?7?q???P}ia????b?????Wf?F??}??C4j?3(?:?b0A??G??j#??K ?qE??"?,????8c????]|?????Mp??X?NJ??=?85??????E?r)????s? b????????})h?????@Y '???n??????6 b??>rr(???rh\??G s??@??p1?)?@???w?x?ad??? ??D?H?^rqEj??? o zS?1?0?#?????~??k?z?co?????/???.W8??ys?G?Z?E,eC??)????????]-??? ????Q[&?~?^?{spvG???wL}?xr?Z?g??% o???????d,c?'??j?9??N?;H?Y?'#???? ?y_????_????????S??~7c?*???[3%?0????G???q?kx ??Ee?4?G??9??]?O][?U?q"?%s????&?d???n??<wL"??b=G?dE<?????VU???'??????z???~ICF??O4??1???!??L?Fd????i"?q?bLRE?M ???9?s?K<?e#?????8??D?yG?=V?7?Q?YC$?2?? ?5?{o5???\???s?s?WG??7J?????P
 
well. There you go! there's the problem. have a look at line 92 of index.php and you will see that you are outputting some information to the browser that it messing with your subsequent scripts. this can be as little as a space.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top