I am kind of new to php. I try to make an app in android that downloads a file from the eClass platform. I developed this script:
<?php
$code=$_GET['code'];
$code = stripslashes($code);
$code = mysql_real_escape_string($code);
$filename=$_GET['filename'];
$filename = stripslashes($filename);
$filename = mysql_real_escape_string($filename);
$path = "C:\\xampp\\htdocs\\openeclass-2.5\\courses\\".$code."\\dropbox\\".$filename;
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); //
use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
?>
which downloads the file when called from browser (in my localhost) and given code and filename as parameters in the url. Now I want to construct a function in my android app that calls this script, gets the file and saves it somewhere in the sd-card
How can I achieve this? Also is it possible to be done in the emulator too? Thank you in advance!
Unfortunately you can't force image download via php. What You need is creating a class that will stream file from web location and save it to sd-card.
I have simmilar code in my app that does both:
try {
newurl = new URL(iurl); // iurl is a String value
b = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
b.compress(CompressFormat.JPEG, 100, new FileOutputStream(
sdcard_image)); // sdcard_image is a String value of local filename (including dir)
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Hope this would help