Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2012, 05:32 PM   PM User | #1
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
how to list all the files in current directory

i uploaded pictures to a directory and in that directory i will make an index.php file and with this file i want to be able to list all the pictures in this folder clickable so that my friend who lives far from me can click each of them and look at it. how can i do that?
ketanco is offline   Reply With Quote
Old 01-29-2012, 06:14 PM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
PHP Code:
$folderPath 'whereismyfiles';
$fList = array();
if (
$handle opendir($folderPath)) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && substr($file,0,2) != "._" && $file != "..") {
            
$fList[] = $file;
        }
    }
    
closedir($handle);
}
// this is the files list
sort($fList);
print_r($fList); 
jmj001 is offline   Reply With Quote
Old 01-29-2012, 07:29 PM   PM User | #3
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
Quote:
Originally Posted by jmj001 View Post
PHP Code:
$folderPath 'whereismyfiles';
$fList = array();
if (
$handle opendir($folderPath)) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && substr($file,0,2) != "._" && $file != "..") {
            
$fList[] = $file;
        }
    }
    
closedir($handle);
}
// this is the files list
sort($fList);
print_r($fList); 
thanks, for the 'whereismyfiles' i typed '../mycurrentfoldername' and it listed the file names but how can i make each item in the list a clickable item so that when clicked the picture will open?
ketanco is offline   Reply With Quote
Old 01-29-2012, 08:09 PM   PM User | #4
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
PHP Code:
// loop through the files and read the functions
$j count($fList);
for(
$i 0$i $j$i++){
     echo 
'<a href="">'.$fList[$i].'</a><br>';

you will need to figure out the href=""
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
ketanco (01-29-2012)
Old 01-29-2012, 08:16 PM   PM User | #5
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Here's a solution which will just iterate the images for you and make them clickable.

PHP Code:
<? function getFileExtension($filename)
{
  
$path_info pathinfo($filename);
  return @
$path_info['extension'];
}

 function 
getFileList($dir)
  {
    
// array to hold return value
    
$retval = array();

    
// add trailing slash if missing
    
if(substr($dir, -1) != "/"$dir .= "/";

    
// open pointer to directory and read list of files
    
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(
false !== ($entry $d->read())) {
      
// skip hidden files
      
if($entry[0] == ".") continue;
      if(
is_dir("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry/",
          
"type" => filetype("$dir$entry"),
          
"size" => 0,
          
"lastmod" => filemtime("$dir$entry")
        );
      } elseif(
is_readable("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry",
          
"type" => getFileExtension("$dir$entry"),
          
"size" => filesize("$dir$entry"),
          
"lastmod" => filemtime("$dir$entry")
        );
      }
    }
    
$d->close();

    return 
$retval;
  }
  
$dirlist getFileList(".");
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pictures</title>
</head>
<blockquote><br />
 // <b>Pictures</b> //<br /><br /><table border="1">
<tr><th width="200">Name</th><th>Size</th><th>Last Mod.</th></tr>
<?PHP
  
// output file list as table rows
  
foreach($dirlist as $file) {
    if((
$file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue;
    echo 
"<tr>\n";
    echo 
"<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
    echo 
"<td>{$file['size']}</td>\n";
    echo 
"<td>",date('r'$file['lastmod']),"</td>\n";
    echo 
"</tr>\n";
  }
?>
</table>
<body>
</body>
</html>
weir-07 is offline   Reply With Quote
Old 01-29-2012, 09:21 PM   PM User | #6
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
Quote:
Originally Posted by jmj001 View Post
PHP Code:
// loop through the files and read the functions
$j count($fList);
for(
$i 0$i $j$i++){
     echo 
'<a href="">'.$fList[$i].'</a><br>';

you will need to figure out the href=""
ok now it lists the files as clickable links but when i click does not open the pictures. how can i open that picture when i click its link?

Last edited by ketanco; 01-29-2012 at 09:38 PM..
ketanco is offline   Reply With Quote
Old 01-29-2012, 09:25 PM   PM User | #7
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss.
weir-07 is offline   Reply With Quote
Old 01-29-2012, 09:39 PM   PM User | #8
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
Quote:
Originally Posted by weir-07 View Post
The code I posted - copied and pasted into an index file in your pictures folder will do the trick without any fuss.
actually i tried yours too but it only listed the titles of a table and that was it. i copied and pasted your code as it is. what might be wrong?
ketanco is offline   Reply With Quote
Old 01-29-2012, 09:45 PM   PM User | #9
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Woops should have tested that!

Remove the line;
PHP Code:
 if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 
weir-07 is offline   Reply With Quote
Users who have thanked weir-07 for this post:
ketanco (01-29-2012)
Old 01-29-2012, 09:56 PM   PM User | #10
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
Quote:
Originally Posted by weir-07 View Post
Woops should have tested that!

Remove the line;
PHP Code:
 if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 
woow very nice thanks a lot!!! it even zooms in once i open picture....
ketanco is offline   Reply With Quote
Old 01-29-2012, 11:06 PM   PM User | #11
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
Quote:
Originally Posted by weir-07 View Post
Woops should have tested that!

Remove the line;
PHP Code:
 if(($file['type'] != 'jpg')||($file['type'] != 'jpeg')||($file['type'] != 'gif')||($file['type'] != 'png')) continue; 

one more thing... what if i wanted the pictures to display without clicking, as soon as i open the webpage? what part of the code would i change and how? (i know for many pictures it would be a very very slow opening webpage but thats another thing...)
ketanco is offline   Reply With Quote
Old 01-29-2012, 11:09 PM   PM User | #12
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Make a html document.
Name it index.
Then insert your pictures into that.

You can google how to do that, I'm sure
weir-07 is offline   Reply With Quote
Old 01-30-2012, 12:45 AM   PM User | #13
ketanco
Regular Coder

 
Join Date: Jul 2009
Posts: 170
Thanks: 24
Thanked 0 Times in 0 Posts
ketanco is an unknown quantity at this point
ok i know how to do that but for that i would have to type all file names one by one... with your method, i dont... what if i want to share 400 photos with my friend... thats whyi asked that
ketanco is offline   Reply With Quote
Old 01-30-2012, 09:14 PM   PM User | #14
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Coming back to this, just change the a tags to img tags, with the src as $file['name'] OR change the inside of the a tag an img tag, again with $file['name'] as the src.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 01-30-2012, 10:28 PM   PM User | #15
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
I can't believe no one has suggested using glob() yet:
PHP Code:
$files glob'./files/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}'GLOB_BRACE );

foreach ( 
$files as $file )
{
    echo 
'<a href="' $file '">'basename$file ), '</a><br />';

__________________
ZCE

Last edited by kbluhm; 01-30-2012 at 10:33 PM..
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:49 AM.


Advertisement
Log in to turn off these ads.