SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    List Out The Files in a Directory for Download

    Hello everyone,

    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.

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

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

    Thank You

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    If you have a directory that stores these files you could do
    Code PHP:
    <?php
      if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            $thelist .= '<li><a href="'.$file.'">'.$file.'</a></li>';
          }
        }
        closedir($handle);
      }
    ?>
    <h1>List of files:</h1>
    <ul><?php echo $thelist; ?></ul>

    I can't take credit for this code (I found it here: http://stackoverflow.com/questions/1...uploaded-files), but it should do what you want.
    If there's anything you don't understand, just post back here.

  3. #3
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Listing out is fine. But what if I want to make those files downloadable?

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ankur218 View Post
    Listing out is fine. But what if I want to make those files downloadable?
    The files are downloadable. That's what the <a href="'.$file.'">'.$file.'</a> part does.

  5. #5
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do I do with this code but? Just name it as download.php and save it?

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Uh, where to begin?
    You wrote that you have created a file uploader and that the files are saved to a directory on your server.
    The script I pointed you at opens the current directory, reads whatever is in it (ignoring "." and "..") and outputs the name of each file it finds, with a link to the file (for download purposes) in an unordered list.

    Just drop it into your folder containing the uploaded files and run it.
    Then you should be able to figure out what is going on.

  7. #7
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, but then there are around 1000 such directories where each directory will have around some 10-20 files. How do I solve that problem? To put this file with different file name inside each directory? Any shortcuts?

    Let me give this a shot meanwhile and figure out the mechanism. Thank you


    Edit: Just checked it out. Works like a charm. But I put this along with my directory which has index.php files and all. So it shows all the folders and files. It's opening the file too. But like I said, my problem is other wise. How do I rectify that?

    Also, can't I add any CSS to it and make it look interactive? And add a download option to it?

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ankur218 View Post
    Yeah, but then there are around 1000 such directories where each directory will have around some 10-20 files. How do I solve that problem? To put this file with different file name inside each directory?
    Please don't put this file in 1000 different directories. That will be a nightmare to maintain.
    You need some sort of method of keeping track of your uploads.

    How about writing a file's details to a database when it is uploaded (i.e. its path on the server, its title, etc.)
    That way you are much more flexible about displaying links to your downloads.
    You'll also be able to filter them by name, type etc.

    If you really had to, I suppose you could just make the above script recursive and have it traverse all directories and files within a parent directory and then spit out a hugely long list.
    This is however, almost certainly, a bad idea.

  9. #9
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, this is how I want it to be. If you've downloaded my example which I posted, you will notice a download page in the navigation bar. Have a look at the upload page too.

    The upload page follows dynamic drop down list. Uploader chooses the engineering stream, then semester, based on the above two, it displays subjects, uploader chooses the subject, and clicks upload. I have made directories and sub directories for all. The file automatically gets uploaded to stream/sem/subj based on the choice.

    I want my users to be shown a similar page in the download page, instead of upload button, it has a next button. So on clicking next button, visitor gets redirected to a page which lists out all the files under the specified stream/sem/subj and also has a download button or option next to it for download.

    Because there are 20 engg streams, 8 sem, each sem having 8 subjects, that is around 20 x 8 x 8 = 1280 subjects (1280 pages I must make I guess to redirect them accordingly).

    Am I clear now?

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    That's cool.
    Then I guess a database is the way to go.

  11. #11
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I have to brush up my SQL now and move to advanced mode and also learn PHP completely.

    But I hope this can be done right? How long does it take to learn both PHP and SQL?

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What other programming languages do you know, and how long have you used them for?

  13. #13
    SitePoint Enthusiast ankur218's Avatar
    Join Date
    Jul 2012
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Programming: C++ 3 years, C 3 years
    Markup: HTML 5 years, CSS 6 months, JS less than a month (Still learning and doing the course in codecademy)

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,074
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ankur218 View Post
    So I have to brush up my SQL now and move to advanced mode and also learn PHP completely.
    To "learn PHP completely" takes quite a while, I should imagine, not to mention an immense amount of dedication.
    However, to accomplish the task in hand doesn't require much more than a good grasp of the language.
    If you are looking for somewhere to start, I would really recommend this book: PHP & MYSQL NOVICE TO NINJA
    It has a whole chapter on file uploads and a whole bunch more.

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You'll fly through it. I think of PHP as essentially being a wrapper over C (I don't know where I read that, or whether I am correct or not).

    Quote Originally Posted by Wikipedia
    In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax. if conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, Java and Perl.
    Possibly the most difficult thing for you to get your head round will be that PHP is the P in the LAMP stack, and you have to appreciate which bit does which job best, as there can be slight overlaps and of course complexities caused by not realising which part of the stack an error occurs in.

    You don't mention SQL RDBMSs (or Mysql the M in the stack) so that might hold you up for a bit too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •