1

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!

george
  • 1,386
  • 5
  • 22
  • 38

1 Answers1

1

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

cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
  • thank you, can you explain how sdcard_image should be declared? Isnt this a file that should be like File sdcard_image =Environment.getExternalStorageDirectory(); ? Also I do not want to download only images but other type of files too (eg txt) – george Jul 08 '12 at 16:47
  • [here](http://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory?answertab=active#tab-top) - is an example of writing/reading file with no matter what type of file it is. For downloading it, you need `BufferedInputStream` and `File` class – cyborg86pl Jul 08 '12 at 17:22