我想从我的设备中选择文件(android),然后使用 php 在本地服务器上传,并将其路径保存到我的数据库。 接下来我想从上传位置下载它。 我怎样才能做到这一点 ?帮助我。
答案 0 :(得分: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.");
}