0

我想从我的设备(android)中选择文件,然后使用php上传到本地服务器并将其路径保存到我的数据库。接下来我想从那个上传位置下载它。我怎样才能做到这一点 ?帮我。

4

1 回答 1

0

我会向您的 php 服务器使用 http 请求,这是一个完整的工作示例: http ://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

要让客户端下载,我会创建一个 /download.php 文件并使用 get 参数将数据库 pk 设置为他们想要下载的 url。接下来我将读取文件,然后使用标题将 PHP 的输出更改为文件,以便客户端通过 http 下载。

代码示例:

$attachment_location = "filePath";
if (file_exists($attachment_location)) {
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: attachment; filename=filePath");
    readfile($attachment_location);
die();
} else {
    die("Error: File not found.");
}
于 2016-08-02T18:04:17.950 回答