Jump to content

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


redarrow

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This code is a modification of code in the PHP manual:
[code]<?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";
?>[/code]

Ken
Link to comment
Share on other sites

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";

?>
Link to comment
Share on other sites

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";

?>
Link to comment
Share on other sites

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>";
}


?>
Link to comment
Share on other sites

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

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

Ken
Link to comment
Share on other sites

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>";
}


?>
Link to comment
Share on other sites

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!

[code]<?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());
}
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.