Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP生成下载链接_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript PHP生成下载链接

Javascript PHP生成下载链接,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,假设我想生成一个下载链接并将其放入标记中 我的php脚本: function download_link(){ $this_id = "d"; //this is the name of file from server $original_filename = 'xample.pdf'; //This come from database $ext = pathinfo($original_filename, PATHINFO_EXTENSION); $fil

假设我想生成一个下载链接并将其放入
标记中

我的php脚本:

function download_link(){
    $this_id = "d"; //this is the name of file from server
    $original_filename = 'xample.pdf'; //This come from database
    $ext = pathinfo($original_filename, PATHINFO_EXTENSION);

    $file = '../uploads/'.$this_id.'.'.$ext;
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/'.$ext);
        header('Content-Disposition: attachment; filename='.$original_filename);//Rename the file with its original filename
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        return readfile($file);//Here where i want  to return the generated url
    }                       
    return '#'; //Or return nothing if file doesn't exist

   echo '<a href="'.function download_link().'"></a>'; //And put it here, the generated url
下载.php

$this_id = $_POST['server_file_name'];
$original_filename = 'xample.pdf'; //This come from database
$ext = pathinfo($original_filename, PATHINFO_EXTENSION);

    $file = '../uploads/'.$this_id.'.'.$ext;
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/'.$ext);
        header('Content-Disposition: attachment; filename='.$original_filename);//Rename the file with its original filename
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        echo readfile($file);//Here where i want  to return the generated url
        exit();
    }  die('1');
但不起作用。
有人能帮我吗?
谢谢

您正在返回。 这就是为什么浏览器开始下载您返回的文件

您需要做的是生成指向文件的字符串。 如果您的“uploads”目录可以通过url访问,那么downloads.php应该如下所示:

$this_id = $_POST['server_file_name'];
$original_filename = 'xample.pdf'; //This come from database
$ext = pathinfo($original_filename, PATHINFO_EXTENSION);

$file = '../uploads/' . $this_id . '.' . $ext;
if (file_exists($file)) {
    echo 'www.myserver.com/uploads/' . $this_id . '.' . $ext;
    exit();
} 
die('1');

如果无法从外部访问uploads目录,则需要先将文件复制到公共目录。

乍一看,我发现了几个问题

下载函数不会返回文件的链接,而是输出文件本身,因此刷新页面时文件正在下载是合乎逻辑的

另外,我可以看到您正在使用
函数download\u link()
调用您的函数,而它应该是直接调用
函数download\u link()

正确的方法是让下载链接指向执行下载链接功能的文件(例如:)

当然,建议在URL中使用id而不是文件名,并应用所需的所有安全性等


在download\u file.php文件中,您可以调用
下载链接($filename)
或更好的
下载链接($id)
并从数据库或存储文件的任何位置获取文件名,然后像现在一样输出文件。

不起作用。
是一个非常可怕的错误描述……没有在同一个函数中生成链接和下载,有一个生成链接的函数,然后在单击链接时调用另一个生成下载的函数,并提示使用或另存为对话框打开
。当您在ajax中调用“window.location.href=response;”时,链接将出现在元素内部。单击Success Callback OK,下面是我所做的,使用
window.location=response
im只会在新窗口中提示,但不会下载任何文件。这是我的php脚本:
$ext=pathinfo($row['file\u name',pathinfo\u扩展名)$原始文件名=$row['file_name']$文件='../uploads/'.$this_id.'.$ext;标题(“内容类型:应用程序/”$ext);标题(“内容传输编码:二进制”);标题(“内容处置:附件;文件名=\”.$original\u filename.\”);die(readfile($file))
正如您在问题开头所说:“假设我想生成一个下载链接并将其放入标记中。”您不需要打开新窗口,也不需要自动开始下载。只需返回包含href的字符串并将其写入。只要点击它之后,如果您的上传目录是可访问的,那么您将能够下载该文件
$this_id = $_POST['server_file_name'];
$original_filename = 'xample.pdf'; //This come from database
$ext = pathinfo($original_filename, PATHINFO_EXTENSION);

$file = '../uploads/' . $this_id . '.' . $ext;
if (file_exists($file)) {
    echo 'www.myserver.com/uploads/' . $this_id . '.' . $ext;
    exit();
} 
die('1');