Question

Possible Duplicate:
scandir fail to open directory

I fixed part of my scandir() problem, but I do not like the arrays. I want it to display the files that are uploaded and give links to them so you would be able to click the images and view them, or click zip files and download them. Problem is I have no idea how to do this.

<?php
 $files = scandir('../snaps'); 
 print_r($files);
 ?>

My scandir so far.

Was it helpful?

Solution

If you give more details it might help, I think this code will help for the links:

    <?php
$files = scandir('../snaps'); 

foreach($files as $file){ ?>
<a href="../<?php echo $file; ?>"><?php echo $file; ?></a>
<?
}
?>

This is a function to create the Zip files

    <?
function create_zip($files = array(),$destination = '',$overwrite = false, $distill_subdirectories = true) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {

    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
          // echo 1;
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
        if ($distill_subdirectories) {
        $zip->addFile($file, basename($file) );
        } else {
        $zip->addFile($file, $file);
        }
        }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

    //close the zip -- done!
    $zip->close();


    //check to make sure the file exists
    //return file_exists($destination);
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename={$destination}");
    ob_clean();
    flush();
    readfile($destination);
    //echo $destination;
  }
  else
  {
    return false;
  }
}

?>

And this is the way you can call it:

    $files = scandir('../snaps');

    $to_zip = array();
    foreach($files as $file){
        if($file != "." AND $file !=".."){
            $to_zip[] = $folder . "/".$file;
        }
    } 

$zip = create_zip($to_zip, "test.zip", true);   

OTHER TIPS

Try:

function print_dir($dir) {
    if ($handle = opendir($dir)) {
        $files = array();
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $files[] = $file;
            }
        }
        echo '<table>';
        foreach($files as $file) {
            echo '<tr><td style="border:none;"><a href="path/to/' . $file . '"><img src="images/icons/file.png" /></a></td><td style="font-size:18px; border:none; padding-top:28px"><a href="path/to/' . $file . '">' . $file_name . '</a></td></tr>';
        }
        echo '</table>';
        closedir($handle);
    }
}

You can use glob too.

$files = glob('../snaps/*');

foreach($files as $f) {
  echo '<a href="'.$f.'">'.$f.'</a><br />';
}

That's a very simple example - you may want to check that the current item is not ".." or "." at each pass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top