简体   繁体   English

PHP:从服务器下载文件

[英]PHP: Download file from Server

I have tried all sorts of codes I've found but nothing has worked for me... 我已经尝试过各种找到的代码,但是对我没有任何帮助...

When I use readfile() or fopen() or something like this: 当我使用readfile()或fopen()或类似的东西时:

function makeDownload($file, $dir, $type) {

header("Content-Type: $type");

header("Content-Disposition: attachment; filename=\"$file\"");

readfile($dir.$file);

}

... A download is started but the file is always empty... ...开始下载,但文件始终为空...

here's the last code I've tried: 这是我尝试过的最后一个代码:

$filename = "gandalf.jpg";
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo " filename NULL";
echo $err;

} else {
// define the path to your download folder plus assign the file name
$path = 'upload/'.$filename;

// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
    echo "file exists";
    // get the file size and send the http headers
    $size = filesize($path);
    header('Content-Type: image/png');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Transfer-Encoding: binary');
    // open the file in binary read-only mode
    // display the error message if file can't be opened

    //readfile($path.$filename);
    $file = @ fopen($path, 'rb');
    if ($file) {
        // stream the file and exit the script when complete
        fpassthru($file);
        exit;
    } else {
        echo $err;
    }
} else {
    echo $err;
}
}

This is the source of my code: Source code 这是我的代码的来源源代码

I hope you can help me :) 我希望你能帮帮我 :)

I use google chrome I need your help for this little project: Link to Project 我使用谷歌浏览器我需要您为这个小项目提供帮助: 链接到Project

When you click on a file in the drop down appears "herunterladen" (download in german) here is where I want to start the php download code 当您单击文件时,在下拉菜单中显示“ herunterladen”(德语下载),这是我要启动php下载代码的地方

I just need the most basic download function for PHP... why is there an upload example on w3schools but NOT a download example? 我只需要PHP的最基本下载功能...为什么在w3schools上有一个上传示例,但没有一个下载示例? oO O

$fileSource= $_GET['fileSource'];     //If you are passing the filename as a URL parameter
$fileSource= "sample.pdf"     //If the filename is fixed in the current folder
if($fileSource) {
     $filesize = filesize($fileSource);
     $path_parts = pathinfo($fileSource);
     $ext = strtolower($path_parts["extension"]);
     switch ($ext) {
     case "pdf":
         header("Content-Disposition: attachment; 
         filename=\"".$path_parts["basename"]."\""); // use 'attachment' to 
         force a download
         header("Content-type: application/pdf"); // add here more headers 
         for diff. extensions
         break;
    default:
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    if($filesize) {
         header("Content-length: $filesize");
    }
    readfile($fileSource);
    exit;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM