Jump to content

Urgent help needed with uploading and downloading files from directory!


jonathan89

Recommended Posts

Hi to all, I have a problem with my coding. Whenever I run my php script on a webpage, it turns up a blank page. Its supposed to retrieve the files from the upload folder and display it on the webpage. Can anyone help me check to see what's wrong?

 

 

<?php

 

$folder = $_SERVER['DOCUMENT_ROOT']."upload"; // the folder which you want to open

 

function select_files($dir) {

    global $PHP_SELF;

    $teller = 0;

   

    if ($handle = opendir($dir)) {

        $mydir = "<p>These are the files in the directory:</p>\n";

        $mydir .= "<form name=\"form1\" method=\"post\" action=\"".$PHP_SELF."\">\n";

        $mydir .= "  <select name=\"file_in_folder\">\n";

        $mydir .= "    <option value=\"\" selected>... \n";

        while (false !== ($file = readdir($handle))) {

            $files[] = $file;

        }

        sort($files);

        foreach ($files as $val) {

            if ($val != "." && $val != "..") {

                $mydir .= "    <option value=\"".$val."\">";

                $mydir .= (strlen($val) > 30) ? substr($val, 0, 30)."...\n" : $val."\n";

                $teller++;   

            }

        }

        $mydir .= "  </select>";

        $mydir .= "<input type=\"submit\" name=\"download\" value=\"Download\">";

        $mydir .= "</form>\n";

        closedir($handle);

    }

    if ($teller == 0) {

        echo "No files!";

    } else {

        echo $mydir;

    }

}

if (isset($download)) {

    $fullPath = $folder.$_POST['file_in_folder'];

    if ($fd = fopen ($fullPath, "r")) {

        $fsize = filesize($fullPath);

        $path_parts = pathinfo($fullPath);

        $ext = strtolower($path_parts["extension"]);

        switch ($ext) {

            case "png":

            header("Content-type: image/png");

            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");

            break;

            case "zip":

            header("Content-type: application/zip");

            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");

            break;

            default;

            header("Content-type: application/octet-stream");

            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");

        }

        header("Content-length: $fsize");

        header("Cache-control: private");

        while(!feof($fd)) {

            $buffer = fread($fd, 2048);

            echo $buffer;

        }

    }

    fclose ($fd);

    exit;

}       

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.