Jump to content


Photo

[solved]how to get all files shown in a directory


  • Please log in to reply
10 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 08:39 PM

Hi iam working on a small script nearly done but how do you get the files in a directory and pull all the pictures on a page.

what do i do, I got to some how get all the pictures in a array then output all those images.

got know i dear as this is not a script for a database it's  a stand alone.

please help lol............................

all examples needed cheers.


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 08:42 PM

Look at the glob() function http://www.php.net/glob

Ken

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 09:14 PM

Can not use blob sorry php two old.

ok i got this so far but i need to get the file names in a array to get them out how cheers.



<?php
$dir = "uploads/";

if (is_dir($dir)) {

   if ($dh = opendir($dir)) {

       while (($file[] = readdir($dh)) !== false) {

foreach($file AS $a => $b);
echo "$a:$b";


       }

       closedir($dh);
   }
}


?>

result well wired ha.

0:.1:..2:colmputer3.gif3:tick.gif
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 09:25 PM

This code is a modification of code in the PHP manual:
<?php
$files = array();
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           $files[] = $file;
       }
   }
   closedir($handle);
}
//
// dump the $files array
//
echo '<pre>' . print_r($files,true) . '</pre>';
//
// echo each filename on it's own line
//
echo implode("<br>\n",$files) . "<br>\n";
//
//  using foreach to get each file
//
echo ' ===============<br>';
foreach($files as $fn)
     echo $fn . "<br>\n";
?>

Ken

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 09:46 PM

Thank you.

this is the out put ok.

but i have no luck in selecting the array of file number can you help please cheers.
Array
(
    [0] => colmputer3.gif
    [1] => tick.gif
)

colmputer3.gif
tick.gif
===============
colmputer3.gif
tick.gif




<?php
$dir="uploads/";
$files = array();
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
          $files[] = $file;
      }
  }
  closedir($handle);
}
//
// dump the $files array
//
echo '<pre>' . print_r($files,true) . '</pre>';
//
// echo each filename on it's own line
//
echo implode("<br>\n",$files) . "<br>\n";
//
//  using foreach to get each file
//
echo ' ===============<br>';
foreach($files as $fn)
    echo $fn . "<br>\n";

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 09:55 PM

Your notice nearly there know, But a problam the pictures from the array comes out twice can someone help please cheers thank you.

is the while loop making two array names or what ????

<?php
$dir="uploads/";
$files = array();
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
          $files[] = $file;



echo "<img src='uploads/$files[0]'></img>";


echo "<img src='uploads/$files[1]'></img>";


      }
  }
  closedir($handle);
}
//
// dump the $files array
//
echo '<pre>' . print_r($files,true) . '</pre>';
//
// echo each filename on it's own line
//
echo implode("<br>\n",$files) . "<br>\n";
//
//  using foreach to get each file
//
echo ' ===============<br>';
foreach($files as $fn)
    echo $fn . "<br>\n";

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 10:05 PM

Done it thank  for all your help, Know i can get all the pictures out onto 1 page cheers.

<?php
$dir="uploads/";
$files = array();
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
          $files[] = $file; }
  }
  closedir($handle);
}

for($i=0; $i<count($files); $i++) {

echo "<img src='uploads/$files[$i]'></img>";
}


?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 10:05 PM

Why are you putting these two lines in the loop that gets the files into the array?
<?php
echo "<img src='uploads/$files[0]'></img>";
echo "<img src='uploads/$files[1]'></img>";
?>

In order to create the HTML to display the images, use this:
<?php
foreach ($files as $file)
    echo '<img src="uploads/' . $file . '"></img>';
?>

Ken


#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 10:08 PM

Whats wrong the way i done it above lol....................
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 10:56 PM

can you help i  am trying to get the width of image ='200' picture but dosent work

<?php
$dir="uploads/";
$files = array();
if ($handle = opendir($dir)) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           $files[] = $file; }
   }
   closedir($handle);
}

foreach($files as $file) {



echo "<html><body bgcolor='blue' border='4'  ><br><table border='4' bordercolor='black'align='center'><td><img

src='uploads/$file'></td></table></img></html></body>";
}


?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 11 July 2006 - 11:01 PM

heres a file manager i created! you can upload/download/and create new files in it! see if theres anything of use to you! it took me forever to get it right! It works with a database to just organise the files! you dont need the database!

<?PHP
SESSION_START();
$folder=$_SESSION['folder'];
$host = "localhost"; 
$user = "*********"; 
$pass = "*********"; 
$db = "**********"; 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to 
connect!"); 
mysql_select_db('mattswebpage_com_-_misc') or die ("Unable to select database!"); 
$user2=$_SESSION['user'];
$abcd=$_SESSION['user'];
if(file_exists("userfolders/$folder/_vit_cnf")){
rmdir("$folder/_vti_cnf");
}
echo "<head><title>$folder</title></head>";
if(is_dir("userfolders/$folder/")) {
echo "<div align=center><h1>$abcd's $us, $user2 Folder</h1></div>";
echo "<table align=center border=1><tr><td align=left>Upload to your folder!<br><form enctype=multipart/form-data method=POST>File<input type=file name=ph><br><input type=submit name=upload value=upload></form></td><td align=right><p align=left><form method=POST>Create a New file!<br>File Name:<input type=text name=new> . <select name=end>
<option value='html'>html
<option value='css'>css
<option value='txt'>txt
<option value='php'>php
<option value='htm'>htm
</select><br><input type=submit name=submit2 value=Create></form></td></tr></table><br>";

if(isset($_POST['submit2'])){

$first=$_POST['new'];
$end=$_POST['end'];
$name="$first.$end";
$page = fopen("userfolders/$folder/$name", 'x+');
$content=" ";
fwrite($page, $content);
$cpage="INSERT INTO files VALUES('','$name','$end','$user2')";
$result = mysql_query($cpage) or die ("Error in query: $cpage. ".mysql_error()); 

}


if(isset($_POST['upload'])){
$dest1=strtolower("userfolders/$folder/".$_FILES['ph']['name']);
$dest = str_replace(" ","_", $dest1);
copy($_FILES['ph']['tmp_name'],"$dest");
$filer=$_FILES['ph']['name'];
$type=$_FILES['ph']['type'];
$cpage="INSERT INTO files VALUES('','$filer','$type','$user2')";
$result = mysql_query($cpage) or die ("Error in query: $cpage. ".mysql_error()); 
       print "File has been successfully uploaded!<br>";
	}
}
$dir = "userfolders/$folder/";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}



$delete = array();
echo "<table align=center cellspacing=0 cellpadding=5>";
$bg = '#eeeeee';
echo "<tr><td>File</td><td>Download</td><td>Delete</td></tr><form method=POST>";
$re = array_search('..', $files);
$rem = array_search('.', $files);
unset($files[$rem], $files[$re]);
$totalp = count($files);
foreach($files as $id => $file){
$file1 = str_replace(" ", "_", "$file");
$file2 = str_replace(".", ".", "$file1");
$nfile = base64_encode("$file1");
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg . '">';
echo "<td align=left><a href=page.php?f=$nfile>$file2</a></td><td><a href=download.php?file=$file><b><font color=black>Download</font></b></a></td><td><input type=checkbox name=delete[] value=$file></td>";
echo '</tr>';
}
echo "<tr><td>Total Files: $totalp</td><td></td><td><input type=submit name=subm value=Delete></td></tr></table></form>";

if(isset($_POST['subm'])){
$delete=$_POST['delete'];

foreach($delete as $rfiles){
@unlink("userfolders/$folder/$rfiles") or die ("The file doesnt exist!");
$rem="DELETE FROM files WHERE Name='$rfiles'";
$result = mysql_query($rem) or die ("Error in query: $rem. ".mysql_error()); 
}
}
?>

"Syntax error" .. WHERE THE **** IS IT?!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users