Jump to content

Pass parameter to function in another page


Go to solution Solved by 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

 

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.