Jump to content

Pass parameter to function in another page


hikaru12

Recommended Posts

I have the following code to download my files. The problem is the path is currently hardcoded. I'm planning on putting this whole code in a function that will take in a path as a parameter. My problem is my main file (which will pass the parameter) is located elsewhere.
 
For example, I have a file called index.php which displays all the files for download and download.php. Index.php should include a way to pass a parameter to the function located in download.php so I can download files from different paths. Ask for clarification if needed! Thanks.
 
include 'db.php';
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script


$path = "repository/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", ' ', $_GET['download_file']); // simple file name validation
$fullPath = $path.$dl_file;


if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: text/php");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
        break;
        // add more headers for other content types here
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        break;
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($f

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.