October 22, 2024
Chicago 12, Melborne City, USA
jQuery

php readfile() problem on download content (i read all related posts before send it)


I’m having trouble creating a secure download on my site, it’s not uploaded yet and it’s currently being tested on the xampp server on my PC. It should be noted that my operating system is Windows 11
this is my code:
user click on a download link (jquery):

$(document).on('click','.downfile',function(e){
  e.preventDefault();
  var reqfile=$(this).next().val();
  $.post('php/downloadfile.php',{
    reqfile:reqfile
    },function(data){});
});

I divided the download file into two parts because someone on this site said that this might be the problem.
so first part is:

<?php
$file_path = "../uploads/".$_POST['reqfile'];
if (file_exists($file_path)){
  $_SESSION['download']=$file_path;
  header('location: startdownload.php');
}
?>
<?php
session_start();
header('Content-Description: File Transfer');
header('Content-Type: '. mime_content_type($_SESSION['download']));
header('Content-Disposition: attachment; filename="'.basename($_SESSION['download']).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($_SESSION['download']));
ob_clean();
flush();
ob_end_flush();
readfile($_SESSION['download']);
exit;
?>

and result is:
firefox developer mode
As you can see, the file is loaded correctly but not downloaded.
And the interesting thing is that the same code was working last week and the file was being downloaded. I have only made a small change in the code that has nothing to do with this section and moved the files folder one level further, for example:
It was uploads/1/test.jpg and now it is uploads/1/1/test.jpg

please help me to solve this problem.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video