使用PHP和ajax从特定文件夹下载文件 [英] Download file from a specific folder using PHP and ajax

查看:67
本文介绍了使用PHP和ajax从特定文件夹下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ajax从spesicif文件夹下载不同类型的文件。问题是当执行代码II时收到此消息



[此处]

我找了类似的教程我找不到解决方案你能不能请帮助



我尝试了什么:



这是我的index.php文件

I am required to download different type of files from a spesicif folder using ajax .The problem is that when execute the code I I get this message

[ here]
I have looked for the similar tutorials I coundnt find a solution Could you please help

What I have tried:

This my index.php file

$(function(){
    	fetchData();
    
    	function fetchData(){
    	$.ajax({
    		url:"ajax.php",
    		method:"post",
    		success:function(data){
            $('#data').html(data);
    		}
    	})
    	}
    
    	$(document).on('click','.download',function(e){
    		e.preventDefault();
    var fileName=$(this).attr("id");
    	$.ajax({
    			url:"download.php",
    			method:"post",
    			data:{fileName:fileName},
    			success:function(e){
                    alert(e);
                }
    		})
    
    	});
    })



这个我正在使用的ajax.php文件填充文件信息表(比如名字,下载Count)






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


<?php

$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 == '..') {
            continue;
        }

        $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>
       </td>
       <td>'.$fileInfo[$value].'</td>
       </tr>';
    }
}

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

?>



这是我的下载php这里发生文件错误


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));
        ob_clean();
        flush();
        readfile($directory.'/'.$fileName);
        echo 'ok';
        exit;
    } else {
        echo 'no file';
    }
}











任何帮助都将不胜感激谢谢

推荐答案

(function(){
fetchData();

函数fetchData(){
(function(){ fetchData(); function fetchData(){


.ajax({
url: ajax.php
方法: 发布
成功:函数(数据){
.ajax({ url:"ajax.php", method:"post", success:function(data){


' #data')。html(data);
}
})
}
('#data').html(data); } }) }


这篇关于使用PHP和ajax从特定文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆