treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Creating a file browser

  • I have made PHP script which allow user to upload files to the site directory:
    <?php
    if (isset($_FILES ['documents'])) {
    $errors=array () ;
    $allowed_ext= array ('doc','docx','ppt','pptx','pages','html','xls','xlsx','numbers','pdf');

    $file_name= $_FILES ['documents'] ['name'];
    $file_ext= strtolower(end(explode ('.', $file_name)));
    $file_size= $_FILES ['documents'] ['size'];
    $file_tmp= $_FILES ['documents'] ['tmp_name'];


    if (in_array($file_ext, $allowed_ext) === false) {
    $errors[] ='Extension not allowed';
    }

    if ($file_size > 3145728) {
    $errors[] ='file size must be under 3mb';
    }
    if (empty($errors)) {
    if (move_uploaded_file ($file_tmp, 'uploads/'.$file_name)) {
    echo 'File uploaded successfully';

    } else {
    foreach ($errors as $errors){
    echo $error, '<br/>';
    }
    }
    }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
    <p>
    <input type="file" name="documents"/>
    <input type="submit" value="Upload"/>
    </p>
    </form>
    </body>
    </html>

    I want to create a file browser that will allow users to view and download the files that are uploaded. How do I do this?
  • using opendir()

    example:

    <?php
    $dir = "/path/to/your/directory/";
    if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
    echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
    }
    closedir($dh);
    }
    }
    ?>