hikaru12 Posted June 24, 2015 Share Posted June 24, 2015 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 24, 2015 Share Posted June 24, 2015 What about passing the value as a hidden field from the first script to the form on the second one? Quote Link to comment Share on other sites More sharing options...
Solution hikaru12 Posted June 29, 2015 Author Solution Share Posted June 29, 2015 What about passing the value as a hidden field from the first script to the form on the second one? I ended up using a session instead but thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.