使用 headers 从 CDN 提供 .APK 文件以在 Android 中安装

Serving .APK file from CDN with headers to install in Android

我想提供一个 .APK 文件供用户下载。我有一个 CDN,它工作正常。当我请求文件下载时,它从 CDN 下载。但我有一个问题。我的用户请求从 Android 设备下载,在这种情况下,下载纯 APK 文件会遇到麻烦,因为我希望用户安装那个 APK 文件并且使用纯 APK 这是不可能的,因为我知道。所以我创建了一个这样的 .php 文件并添加 'Content-Type: application/vnd.android.package-archive':

<?php

$file = 'myfile.apk'; //File that we want to send to user.

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.android.package-archive');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

当我请求download.php时,它的工作和用户可以下载并安装APK文件。现在我的问题是,在这种情况下,该文件是从 CDN 下载的吗?我想要从 CDN 提供 download.php 和 APK 文件,因为我没有足够的流量。

或者是否可以在不使用 php 的情况下将 'Content-Type: application/vnd.android.package-archive' 添加到从 CDN 下载文件?

PS: 当我请求纯APK文件时,因为它来自CDN,它像缓存一样立即下载,但是对于download.php,下载需要时间。这意味着在这种情况下它不是来自 CDN?

Or is this possible to add 'Content-Type: application/vnd.android.package-archive' to downloading file from CDN without php?

是的,在这种情况下下载必须正确。

But with download.php, It takes time to download. it means in this case it's not from CDN?

这需要时间,因为您使用 readfile 和输出缓冲。在这种情况下,只有在 php 将目标文件的内容完全加载到内存后才开始下载。如果您计划提供大的 apk 文件,这是一个潜在的问题。

你可以为他们服务,例如:

// set headers here ...
$output = fopen('php://out', 'a');
$target = fopen($target, 'r');

if (!$target || !$output) {
    //  throw error, if cant read file or 
}

// read target file, using buffer size 1024
while (!feof($target)) {
    fwrite($output, fread($target, 1024), 1024);
}

fclose($target);
fclose($output);