0

I am making a resource directory for my college. I don't know PHP and I am learning things along the way. I have created the upload part successfully using dynamic dropdown lists. I have added 3 levels, first to choose which stream/dept, then which semester and then which semester, and on clicking upload, the files goes to the desired directory correctly.

Now, I want to know how to display all the files that are being uploaded? They are all in the directory. How do I list them and show my users/visitors so that they can choose what to download? The example I have attached only has 2 engg streams, 2 sems under both, and 2 subjects under each sem. I want to list out all in a segregated way. Say a page that has CSE/SEM1/ Data Structures/all the files under this category, another page has CSE/SEM2/Networks/all the files under this category. And not only list out but also form a link so that they can download it.

My index page is there right?. It has a download page navigation. I want users to click that. On clicking that, they get redirected to page similar to the upload page with dynamic drop downs. They choose the stream, sem and subject and click next. (I have to make that page) They get redirected to a page which shows the list of all the files in THAT directory (stream/sem/subj/allfiles) alone. Also the list must be linked, so that on clicking that file name, they get to download the file which they want or have a download option next to the name.

Please check my attachment/link and please help me overcome this problem at the earliest.

http://www.mediafire.com/?hevt1f4a1rpbyvp

Thank You :)

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • I googled and I could only find ways to list the files in a directory and I only succeeded doing the same. But my demands are different like I mentioned above. I am referring some ways to redirect to pages using drop downs. But still facing problems on how to display the files in sub folders (stream/sem/subj/files.extension) and make it downloadable. – Ankur Sinha Nov 04 '12 at 07:49
  • check [this question](http://stackoverflow.com/questions/7254748/php-recursively-file-folder-scan-sorted-by-modification-date). It suggests using a library for this called [SPL Directory Iterator](http://php.net/manual/en/class.directoryiterator.php) or you can DIY as shown here in [this question](http://stackoverflow.com/questions/2398147/php-recursive-directory-path) – Pastor Bones Nov 04 '12 at 07:56
  • Yeah, fine with the display part. I understood that, will try it out. But I want to make the file downloadable too. How do I do that? – Ankur Sinha Nov 04 '12 at 08:04

1 Answers1

0

To serve a file for download, create a php script that reads the file, then serves it with the proper headers

// ensure the file exists
if(isset($_GET['file']) && file_exists($_GET['file'])){

  // Read the file some.zip
  $file = file_get_contents($_GET['file']);

  // Set headers to serve the file for download
  header('Content-Type: application/zip');
  header('Content-Disposition: attachment; filename="$_GET['file']"'); // name file here
  header('Content-Length: ' . strlen($file)); // length of the file

  // echo the file
  echo $file;
}
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
  • Sort of understood, could you please edit your code and add comments next to each statement explaining what each statement does? – Ankur Sinha Nov 04 '12 at 08:31
  • it's pretty simple, but there's your comments – Pastor Bones Nov 04 '12 at 08:32
  • Thanks a lot sir. I have a doubt, so for every file I must name the filename is it? Won't it read by itself? – Ankur Sinha Nov 04 '12 at 12:14
  • no, this is after you've listed the files in your dropdown and they make a choice...you send the path/filename to this script via get or post. I updated my answer to show that – Pastor Bones Nov 04 '12 at 13:43
  • Sir, I am not listing the files in the drop down. I am listing the engineering stream first, based on the choice, the semesters are shown, based on the semester choice, subjects are shown, on clicking the next button after having chosen all the 3, they get redirected to a page which has all the files of that particular subject. – Ankur Sinha Nov 05 '12 at 01:17
  • Sir, I have got the following error: Parse error: syntax error, unexpected 'file' (T_STRING) in C:\xampp\htdocs\SRMUARD\file.php on line 11. This line: header('Content-Disposition: attachment; filename="$_GET['file']"'); // name file here. – Ankur Sinha Nov 05 '12 at 05:01
  • Alternatively, I tried this but it gives access to the site completely which I don't want. '.$file.''; } } closedir($handle); } ?>

    List of files:

    – Ankur Sinha Nov 05 '12 at 05:02