PHP rest api如何返回文件或下载

标签 php mysql api rest download

我有一个 rest api,端点是 getfilestart.php。它所做的是使用 PDO 在数据库中搜索文件名。如何获取文件并将其作为文件返回或使用 postman 或在浏览器中自动下载?我尝试了几种方法,但无法获取文件。目录在../files/

这里是下载函数

    public function downloadFile($param){
        $base_url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'];
     
        $q = "SELECT * FROM data_file WHERE module_id = ". $param;
        $stmt = $this->conn->prepare($q);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $filename = $row['file_name'];
        $request_url = $base_url .'/mtc_project_server/files/'.$filename;
    }

如果我转储 request_url 其 http://localhost/mtc_project_server/files/mir4.PNG 这是实际文件。我如何使用休息下载它?我尝试执行 cURL 和 file_get_contents,但仍然无法正常工作。提前致谢

最佳答案

从您的下载文件 URL $request_url
这是服务器端的强制下载源代码。

// https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($request_url) . ".jpg\""); 
readfile($request_url);
exit();// end process to prevent any problems.

这是在客户端下载的源代码。

$url = 'https://my-domain.tld/rest-api/path.php';// change this to your REST API URL.

// https://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl

$fileext = pathinfo($url, PATHINFO_EXTENSION);
echo $fileext;
set_time_limit(0);
// For save data to temp file.
$fp = fopen (__DIR__ . '/localfile.tmp', 'w+');

//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

// get curl response
$response = curl_exec($ch); 

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

fwrite($fp, $body);

curl_close($ch);
fclose($fp);

// rename temp file.
preg_match('/filename=[\'"]?([\w\d\.\-_]+)[\'"]?/i', $header, $matches);
var_dump($matches);
if (isset($matches[1]) && !is_file(__DIR__ . '/' . $matches[1])) {
    // if target rename file is not exists.
    rename(__DIR__ . '/localfile.tmp', __DIR__ . '/' . $matches[1]);
} else {
    // for debug
    echo 'something wrong!<br>';
    var_dump($matches);
    echo '<br>';
    echo 'File was downloaded into ' . __DIR__ . '/localfile.tmp';
    exit();
}

关于PHP rest api如何返回文件或下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70105506/

相关文章:

c++ - 写网络使用程序应该用什么API?

javascript - Facebook 错误 100 : You cannot specify a scheduled publish time on a published post

Python将图像保存到文件夹中

php - 使用 PHP 在 phpMyAdmin 数据库中存储表单输入

php - 如何按字段值的出现次数排序记录集并按它排序

javascript - Google Charts BarChart OnClick 或 OnSelect

php - 如果已存在则插入或更新 - 准备好的语句

php - 未设置域时产生 404 错误 (WHM/CentOS)

php - 带有数组接口(interface)的集合上的 array_map?

php - javascript长度持续时间