I am required to download different type of files from a spesicif folder using ajax .The problem is that when execute the code I get this message this I have looked for the similar tutorials I coundnt find a solution Could you please help

This my index.php file

<!DOCTYPE html>
<div class="table-responsive">
<div id="data"></div>


    function fetchData(){

var fileName=$(this).attr("id");


this my ajax.php file that I am using populate the file Information table (like names, and download Count)


$handleDir = opendir($directory) or die('error');
$fileName = array();
$count = (count(scandir($directory)) - 2);
$output .= '<table class="table table-bordered">
<th width="20%">File Name</th>
<th width="10%">Download Count</th>
if ($count) {
    while ($file = readdir($handleDir)) {
        if ($file == '.' || $file == '..') {

        $fileName[] = $file;

    $query = 'select * from download_manager order by  id desc';
    $fileInfo = array();
    $result = mysqli_query($link, $query);
    if (mysqli_num_rows($result)) {
        while ($singleFile = mysqli_fetch_array($result)) {
            $fileInfo[$singleFile['filename']] = $singleFile['downloads'];

    foreach ($fileName as $key => $value) {
        $output .= '<tr><td >
        <span class="download" id="'.$value.'">'.$value.'</span>

$output .= '</table>';
echo $output;


And this is my download php file error occurs in here

if (isset($_POST['fileName'])) {
    $fileName = $_POST['fileName'];

    if (file_exists($directory.'/'.$fileName)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.$directory.'/'.$fileName);
        header('Content-Length: '.filesize($directory.'/'.$fileName));
        echo 'ok';
    } else {
        echo 'no file';

any help would be much appreciated Thank you

You can't use AJAX to force / suggest a browser to download a file. With AJAX you can send data and receive data in JS. What you could do is use AJAX (sending some parameters) to give the authority to a user to access a class / file in another tab to download the file. But would be a lot more straightforward just to use JS in order to create and post a form containing those data in another tab in a hidden iframe.

I have accomplished this with a hidden iframe. I use perl, not php, so will just give concept, not code solution.

Client sends Ajax request to server, causing the file content to be generated. This is saved as a temp file on the server, and the filename is returned to the client.

Client (javascript) receives filename, and sets the iframe src to some url that will deliver the file, like:

$('iframe_dl').src="/app?download=1&filename=" + the_filename
Server slurps the file, unlinks it, and sends the stream to the client, with these headers:

Content-Disposition:'attachment; filename=the_filename'
I personally use this on my site.
Works like a charm.

commented: Plagiarism without attribution. -3
commented: Plagiarist! You got this from: https://stackoverflow.com/a/9858790/4629068 -1