Jump to content


Photo

file_exists function issue


  • Please log in to reply
12 replies to this topic

#1 Outlawpd

Outlawpd
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 October 2006 - 08:42 PM

Good Day all,

I have an issue where I can't seem to get pictures displayed properly from a particular directory when using file_exist function.

First let me explain a little bit about what I am trying to do.

I have users upload pictures using a form, then pictures are stored under that particular users #id folder/directory, for example: 89099887/whatever.jpg. The picture is not renamed, it keeps its original name.

Now each user is categorized, so for example when clicked on Miami, it will display a list of all users who are under that category. It should display the thumbnail picture and users name right under, in that order for each user. See below:

USER #1 PICTURE
USER #1 NAME

USER #2 PICTURE
USER #2 NAME

USER #3 PICTURE
USER #3 NAME

Here is my code:
<?php
$result = mysql_query("SELECT clients_name, id FROM clients_info WHERE city='$city' AND $category='x'group by clients_name");
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  $filename="/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
  if (file_exists($filename)) 
  {
    $dir = "/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) 
    {
      $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }
     print "<TR><TD><img src=/bella/admin/client_pics/$row[1]/$files[2]></TD></TR>";
  } 
  else {
      echo "This client has no pictures";
  }


  print("<TR><TD><center>");
/*    <img src=/phpThumb/phpThumb.php?src=/bella/admin/client_pics/$row[1]/$files[2]&w=100></br> */
    print ("<a href=view_profile.php?id=$row[1] target=frame3>$row[0]</a></br></TD></TR>");

}
clearstatcache (); 
mysql_free_result($result);
?>

Now the above code works partially, it displayes the very first pictures called janedoe.jpg. Then it tries to look for exact same picture in other users directory. Of course it does not find it and it returns broken image. See below:

USER #1 PICTURE <- OK
USER #1 NAME

USER #2 PICTURE <- BROKEN IMAGE
USER #2 NAME

USER #3 PICTURE <- BROKEN IMAGE
USER #3 NAME

Any ideas? I hope you understand what I am trying to do, if you can help me I would appeciate it very much.

#2 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 09 October 2006 - 09:07 PM

I have a dedicated box and a shared box, they obviously have difference security permissions.  I write my stuff on a Windows box so either way it's all screwed up by the time I get to post it on a production box.  The only reason I mention this is that when I use file_exists() I try to locate it using the base path to the file which works fine on my local/dedicated box (c:/apache/htdocs/etc/ or /etc/usr/web) but on my shared box those don't work because of (not exactly sure) a base path set in the php.ini so I have to do it virtually using the url to check (http://www.url.com/image.jpg).  I know that's long winded and I could have said it in 1 sentence but I'm enjoying the typing right now =)
I'm considered good looking in some countries

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 09 October 2006 - 09:17 PM

Your using file_exists () on a directory handle not on the file, so how can file_exists () tell you that file is there. You need to change your logic, so you use file_exists() on the file. This way you don't show <img> tag if the file isn't there!

me!

#4 Outlawpd

Outlawpd
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 October 2006 - 09:29 PM

Thanks. I see what you are saying, but doesn't file_exists function scan the folder for any files? If there are files then it returns TRUE, if not then FALSE.

This would be a bit easier if I knew the name of the file which I could do by renaming the file on the upload. But that is whole another thing.

I believe that the problem is in the loop. It does show me the first image (xxx1.jpg) but then the second and third fail because it is still looking for xxx1.jpg in other directories. I gues some kind of reset for an array, to clear it so that it pulls correct pic at the begining of each loop. Any ideas?

#5 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 09 October 2006 - 09:34 PM

No, file_exists () will only test the param passed to it, it does not scan a directory. So if you give a path + file name, it will check to see if it is there, if it is it returns TRUE, if it's not there, it returns FALSE, if TRUE the result is cached!


me!

#6 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 09 October 2006 - 10:33 PM

How about this :

<?php
$result = mysql_query("SELECT clients_name, id FROM clients_info WHERE city='$city' AND $category='x'group by clients_name");
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  $filename="/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
  if (file_exists($filename)) 
  {
    $dir = "/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) 
    {
      $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }



     $PictNum = rand(2, count($files)); // choose random picture



     print "<TR><TD><img src=/bella/admin/client_pics/$row[1]/$files[$PictNum]></TD></TR>";
  } 
  else {
      echo "This client has no pictures";
  }


  print("<TR><TD><center>");
/*    <img src=/phpThumb/phpThumb.php?src=/bella/admin/client_pics/$row[1]/$files[2]&w=100></br> */
    print ("<a href=view_profile.php?id=$row[1] target=frame3>$row[0]</a></br></TD></TR>");



unset($files); // Clear previous users pictures



}
clearstatcache (); 
mysql_free_result($result);
?>


Your wer not clearing previous users pics


#7 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 09 October 2006 - 10:58 PM

You also need to change the way you test for existing pictures:

<?php
$result = mysql_query("SELECT clients_name, id FROM clients_info WHERE city='$city' AND $category='x'group by clients_name");
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  $filename="/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
  if (file_exists($filename)) 
  {
    $dir = "/data/11/0/77/86/729738/user/744719/htdocs/bella/admin/client_pics/$row[1]/";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) 
    {
      $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }



   $PicCount = count($files); // Count number of files found
  }
if ($PicCount >2)// this is how you know there are pics and not just directories
  {
     $PictNum = rand(2, $PicCount); // choose random picture



     print "<TR><TD><img src=/bella/admin/client_pics/$row[1]/$files[$PictNum]></TD></TR>";
  } 
  else {
      echo "This client has no pictures";
  }


  print("<TR><TD><center>");
/*    <img src=/phpThumb/phpThumb.php?src=/bella/admin/client_pics/$row[1]/$files[2]&w=100></br> */
    print ("<a href=view_profile.php?id=$row[1] target=frame3>$row[0]</a></br></TD></TR>");
unset($files); // Clear previous users pictures
}
clearstatcache (); 
mysql_free_result($result);
?>


#8 Outlawpd

Outlawpd
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 10 October 2006 - 02:13 AM

Thanks a lot to everyone for their help. UNSET is what I was looking for all this time.

Firemankurt, I modifed script according to your comments. It works, however, sometimes it will display one, two, or even three broken images, then again it may display all of them correctly? It might have something to do with random image selection.

When I right click on one of the broken images I see the following:

http://mywebsite.com...lient_pics/84/    <-- notice missing pic info

Any ideas?

#9 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 10 October 2006 - 03:23 AM

You could echo some variables to help find the problem:
...
print "<TR><TD><img src=/bella/admin/client_pics/$row[1]/$files[$PictNum]><br/>/bella/admin/client_pics/$row[1]/$files[$PictNum]</TD></TR>";
...

Might also want to check substring of $filename to see if it ends in ".jpg" or ".gif" before you actually print it.

...
{
     $Exten = substr($filename, -3);
     if ($Exten=="jpg" || $Exten=="gif") $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }
...


#10 Outlawpd

Outlawpd
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 October 2006 - 02:44 AM

I added troubleshooting code as you suggested. As you can see below, the first two pics I can see the path to the file and filename. The third one however, is missing the filename, yet I can't figure out why.

b/admin/client_pics/84/athena2.gif

/b/admin/client_pics/82/angie1.gif

/b/admin/client_pics/85/

This is my latest code:
<?php
$result = mysql_query("SELECT clients_name, id FROM clients_info WHERE city='$city' AND $category='x'group by clients_name");
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  $filename="/data/11/0/77/86/729738/user/744719/htdocs/b/admin/client_pics/$row[1]/";
  if (file_exists($filename)) 
  {
    $dir = "/data/11/0/77/86/729738/user/744719/htdocs/b/admin/client_pics/$row[1]/";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) 
    {
      $files[]=$filename;
     $Exten = substr($filename, -3);
     if ($Exten=="jpg" || $Exten=="gif") $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }
   $PicCount = count($files); // Count number of files found
  }
if ($PicCount >2)// this is how you know there are pics and not just directories
  {
     $PictNum = rand(2, $PicCount); // choose random picture

     print "<TR><TD><img src=/phpThumb/phpThumb.php?src=/b/admin/client_pics/$row[1]/$files[$PictNum]&w=100>/b/admin/client_pics/$row[1]/$files[$PictNum]</TD></TR>";
  } 
  else {
      echo "This client has no pictures";
  }


  print("<TR><TD><center>");
/*    <img src=/phpThumb/phpThumb.php?src=/b/admin/client_pics/$row[1]/$files[2]&w=100></br> */
    print ("<a href=view_profile.php?id=$row[1] target=frame3>$row[0]</a></br></TD></TR>");
unset($files); // Clear previous users pictures
}
mysql_free_result($result);
?>


#11 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 12 October 2006 - 06:17 AM

<?php
$result = mysql_query("SELECT clients_name, id FROM clients_info WHERE city='$city' AND $category='x'group by clients_name");
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
  $filename="/data/11/0/77/86/729738/user/744719/htdocs/b/admin/client_pics/$row[1]/";
  if (file_exists($filename)) 
  {
    $dir = "/data/11/0/77/86/729738/user/744719/htdocs/b/admin/client_pics/$row[1]/";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) 
    {



//      $files[]=$filename;  ---- Remove this ----



     $Exten = substr($filename, -3);
     if ($Exten=="jpg" || $Exten=="gif") $files[]=$filename;
       //echo "filename: $filename : filetype: " . filetype($dir . $filename) . "\n";
    }
   $PicCount = count($files); // Count number of files found
  }
if ($PicCount >2)// this is how you know there are pics and not just directories
  {
     $PictNum = rand(2, $PicCount); // choose random picture

     print "<TR><TD><img src=/phpThumb/phpThumb.php?src=/b/admin/client_pics/$row[1]/$files[$PictNum]&w=100>/b/admin/client_pics/$row[1]/$files[$PictNum]</TD></TR>";
  } 
  else {
      echo "This client has no pictures";
  }


#12 Outlawpd

Outlawpd
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 October 2006 - 08:16 PM

Thanks so much for your help and time.

I tried and it did not work. HEhe, I hope you don't give up on me. Any other ideas?

#13 Firemankurt

Firemankurt
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts
  • LocationWashington State

Posted 13 October 2006 - 05:34 AM

if you add

var_dump(file);


at the end of your script does it show any values in "file" array?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users