Solved

html and php file list for download

Posted on 2006-06-12
12
717 Views
Last Modified: 2012-06-27
Hi all

In this question:

http://www.experts-exchange.com/Web/Web_Languages/Q_21833113.html

I got some handy assistance generating an HTML page that lists all the files in a given folder, and making them available for download.  The one I'm using is this one (see original thread above for author credit):

<?php

function make_row ($s) {
  return "<tr><td>$s</td><td><a href='files/$s'>Download</a></td></tr>\n";
}

$files = glob('*');
// Fetch all file names

$files = array_map("make_row", $files);
// Format the file names for output

$str = implode("", $files);
// Join the array to make it a string

$str = "<table border='1' bordercolor='white'><col style='background-color: #CCCCFF;'></col><col style='background-color: #9999FF;'></col>$str</table>";
// Format it further

echo $str;
// Print the string.

?>

What I'd like to do next is:

- Specify a directory for the file listing (so I can list files from a dir other than the one the script exists in)
- Make a left-click trigger a 'Save file as...' dialog box, rather than relying on users right-clicking
- Show the date and time of the files listed
- Not show the complete path to the file on the page - just the filename.  I appreciate the path has to exist in the link to the file, of course.

Thanks !


Simon
0
Comment
Question by:SimonUK
  • 5
  • 5
12 Comments
 
LVL 10

Expert Comment

by:ClickCentric
ID: 16891188
For part 1 - Just set the directory wherever you're opening the directory to read the files.
For parts 2,4 - list the files in a div with a link to the files.  Then if they click on it, they'll be prompted to download it (typically, at least...what kind of files are they? )
For part 3 - Use stat() http://us3.php.net/manual/en/function.stat.php to get the file date/time and date() http://us3.php.net/manual/en/function.date.php to display the date/time with the filenames.
0
 
LVL 1

Author Comment

by:SimonUK
ID: 16892013
Hi

Thanks for your suggestions:  I don't know enough about php to know how to set the directory and how to list the files in a div - the script above was written for me at the link shown, so I'd need an idea of the alterations required to the code, please!

The links to the manual are very handy though; I'll read up!


Simon
0
 
LVL 1

Author Comment

by:SimonUK
ID: 16975245
Anyone able to further this, please?

Thanks


Simon
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Accepted Solution

by:
murking earned 500 total points
ID: 17012159
tried + tested:

<?php

if(isset($file)){
      $fd=fopen($file, "r");
      if(!$fd) {
            print "error opening the file $file for download";
            exit;
      }
      // send the file to the browser
      header("Content-type: application/octet-stream");
      header("Content-length: " . (string)(filesize($file)));
      header("Content-Disposition: inline;"      . "filename=\"" . $file . "\";");
      fpassthru($fd);
      // delete the temporary file
      unlink($file);
      exit;  
}

function make_row ($s, $t) {
  return "<tr><td>$s</td><td>".date('m/d/Y h:i:s A',$t)."</td><td><a href='$PHP_SELF?file=$s'>Download</a></td></tr>\n";
}

$path = ""; /* here is where you will specify the directory -if empty then it will browse the dir where the script is located*/

$files = glob($path.'*.*');  /* the *.* was needed to filter directories -which can't be downloaded */
// Fetch all file names

$times = array_map("filectime", $files);
// Get each file's ctime

$files = array_map("make_row", $files, $times);
// Format the file names for output

$str = implode("", $files);
// Join the array to make it a string

$str = "<table border='1' bordercolor='white'><col style='background-color: #CCCCFF;'></col><col style='background-color: #CCCCFF;'></col><col style='background-color: #9999FF;'></col>$str</table>";
// Format it further

echo $str;
// Print the string.

?>

Hope this helps! Greetings,

--murki
0
 
LVL 2

Expert Comment

by:murking
ID: 17012188
Some observations:

-You should specify the directory where you want to browse in the variable $path, e.g. $path = "/" for root...

-The directories will not be listed

Greetings,

---murki
0
 
LVL 1

Author Comment

by:SimonUK
ID: 17068379
Thanks Murki

I've tried the code... I can get a list of files OK, and the date/time is there - but the path still appears (I only want to show the filename - is that possible?) and if I left-click, the browser locks up.  It appears to be trying to open the file directly in the browser - what I want to achieve is a Save File As... dialog box when users left click.

This is because not everyone knows to right click on files to download... so they left click and the browser tries to open the file.

That said, I'm using IE7 beta, so maybe that's the problem.  Will try with IE6, meantime do you have any further suggestions?

Thanks again


Simon
0
 
LVL 2

Expert Comment

by:murking
ID: 17148456
I get the Save As... dialog when left clicked. Have you verified this yet ?
0
 
LVL 1

Author Comment

by:SimonUK
ID: 17148506
Still same issue but thanks for checking again... will try again with alternate setups ASAP - apols for slow response!

Simon
0
 
LVL 2

Expert Comment

by:murking
ID: 17464654
I'm pretty positive that my solution worked, i coded everything it was needed, so I think the points should be given to me :)

greetings,

--murki
0
 
LVL 1

Author Comment

by:SimonUK
ID: 17464678
I never did get it working, but ran out of time to check other platforms / versions of Explorer, so I agree that murking should get the points.  I'll try further with the code and report back, and start a new thread if I need further help !


Simon
0
 
LVL 2

Expert Comment

by:murking
ID: 17464695
thanks simon, any other help u need, just ask...

--murki
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clean URL from GET variables 2 101
Node js - send response after all promises are resolved 1 56
MVC and Angular 2 102
CSS display:none AND display:block on same element 3 90
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question