Wrong Response Body while trying to download photo using Retrofit2 - php

I'm using Retrofit 2.0.0-beta2 and I need to download some files from my PHP server. My first approach which worked was to directly use the GET method from its relative server path and I was getting the correct bytes.
Now I've tried something more secure that delivers the file to me based on some checks. It automatically fetches the file path from the DB and checks if the user session is correct. This works in browser tests, both Chrome PC and Chrome from Android correctly download some photos.
I'm serving the file using the X-Sendfile header like so:
header("X-Sendfile: $file_name");
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
The Android-side call looks like this:
#Streaming
#GET("/card/download")
Call<ResponseBody> getCard(#Query("filename") String filename);
All I'm getting when opening the files is the echoed text response from server. Is there any way I can receive the "correct" files?

Apparently there was some sort of problem installing the mod.
I also updated OkHttp to version 2.7.0

Related

Download FTP file to client w/ Readfile()

I'm building a web based file management interface for our clients, where I am attempting to facilitate a download from a remote (FTP) server to the client without downloading to the local web server first.
I've found that Readfile() does exactly what is needed, working perfectly for both web based downloads as well as from public FTP servers. The problem is that when specifying credentials via the FTP url, it apparently no longer works. I've found other reports of this online but thus far no solutions or workarounds.
$file_url = 'ftp://username:password#198.2.148.130/198.2.148.130%20port%2025665/server.properties';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
Is there any workaround methods that would make this operate as expected? I am stumped as to how this can be fixed, where it seems like a bug moreso than a limitation.
The URL-encoded spaces (%20) in the URL seems to the culprit.
Just use literal spaces:
$file_url = 'ftp://username:password#198.2.148.130/198.2.148.130 port 25665/server.properties';
It's perfectly valid to specify the credentials this way in PHP FTP URL wrapper.

Unable to download .sqlite file from the server

I am using the following code which I referred from the php.net example:
<?php
// open the file in a binary mode
$name = './db.sqlite';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($name));
// dump the database and stop the script
fpassthru($fp);
exit;
?>
When I try to run this PHP script from the URL of my browser, I get the warning:
Cannot modify header information - headers already sent by
My requirement is to basically set up a PHP script which I can use to download the .sqlite file in my iOS app. But I think first it must work directly through the URL. I am new to PHP so please guide me.
I also tried to find out why am I getting this warning in the following link:
http://www.tech-recipes.com/rx/1489/solve-php-e
But it didn't solve my problem.
Question 2.) I have planned to use NSURLSession to get the file in my iOS app's sandbox, once the PHP script is up and working. Is this the correct way?
I could not get my PHP working correctly so I found a workaround.
I used NSURLRequest to point directly to the sqlite file residing on the server and then used NSData to store the contents of it. I then stored the file in the sandbox of my app(simulator) and I am able to query the database.
Important lesson that I learnt: "SQLite database files are not human-readable"

PHP script to download file in mobile Browser (Android)

I want my mobile webapp be able to download files. My Android device unfortunately tells me it does not support file download via jquery plugin jquery.fileDownload.js...
So I just send a jsonP (cross domain, because my app runs on another server) request.
My php script looks
ob_clean();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="file.JPG"');
readfile('/var/.../file.JPG');
die();
In my android chrome browser nothing happens after I send the jsonp request.
What I am doing wrong? Isnt it possible to download files via jsonp? Same code tested
on my desktop browser works perfectly.
Thanks in advance.
JSONP requires you to send back actual executable javascript code. You're just dumping out some essentially random bytes, which your browser will try to execute, and fail miserably.
You need to do some reading first: http://en.wikipedia.org/wiki/JSONP
You'd need to respond with a proper JSONP callback, and include your file data as a parameter, e.g.
header('Content-Type: text/javascript');
echo "jsonCallback(", json_encode(file_get_contents('your file here')); , ");"

PHPExcel not downloading in mobile Safari

I'm using PHPExcel to generate Excel files on the fly within my PHP application. I'm using the Excel2007 format.
When a user visits the URL that creates and forces the download of the Excel file, everything works great in all browsers except for mobile Safari (iPhone and iPad).
Here are my headers and the readfile method:
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-disposition: attachment; filename=' . $file_name . '.xlsx;');
header('Content-Length: ' . filesize($path_to_file . '.xlsx'));
readfile($path_to_file . '.xlsx');
When I browse in mobile Safari to the URL that is supposed to download the .xlsx file I can actually see the tabs representing each worksheet of the file, but I don't see the actual data like so:
Furthermore, there are two additional weird behaviors I'm encountering with this:
If I download this file on a desktop browser and email it to myself and open it with the Mail app in iOS, the file displays correctly.
If I then take that attachment from the Mail app in iOS and import into, say, Dropbox, it does NOT display properly (it displays the same as the screenshot above).
In Chrome, the file downloads properly and opens in Excel or even Numbers as expected, but in the console I see this message: Resource interpreted as Document but transferred with MIME type application/vnd.ms-excel:
Also, per the PHPExcel documentation, in place of readfile I have also tried:
$objWriter->save('php://output');
That, however, produces an error in mobile Safari that reads:
OfficeImportErrorDomain Error 912
To eliminate the Content-type as being the issue I've experimented by adjusting the Content-type to other values (such as application/vnd.ms-excel or even application/download). Unfortunately (though not surprisingly) those don't work either.
Any guidance is greatly appreciate.
I use ->addHeaderLine('Content-Type', 'application/octet-stream'). Otherwise Safari displays error.
Although this header makes it to display xlsx file in browser, not downloading. I haven't found any solution to force mobile Safari to download.

simultaneously download on server and on user

I am currently developing an application in PHP in which my server (a dedicated server) must to download a file, and the user should download the file in same time.
Here is an example :
Server start to download a file at a time A.
User wants to download this file at the time A + 3 seconds (for example)
I already solved the problem :"If the user downloads the file faster than the server..". But I didn't know how to make a php script in which the user is gonna to download the full file (it means that the size must be the full size of the file, not the size it's currently downloaded at the time A+3seconds). I already make that :
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$data['name'].'";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$data['size']);
readfile($remoteFile);
But it doesn't work, the user is gonna download just the size it is currently on the server (which corrupt the file) and not the full file...
If you have any solution, thank you.
You could probably pipe the file manually, by opening the connection and reading until you're past all headers. Then once you've figured out the Content-Length, send that to the user and just echo all remaining data you get (do use flush() and avoid output buffers).
Pseudocode(-ish):
open the file
# grab headers
while you didn't get all HTTP headers:
read more
look for the Content-Length header
send the Content-Length header
# grab the file
while the rest of the request isn't done
read more
send it to the user
flush the buffers
done
Expanding on #Tom answer, you can use cURL to greatly simplify the algorithm by using the CURLOPT_HEADERFUNCTION and CURLOPT_READFUNCTION callbacks - see curl_setopt().
Don't send the content-length header. It's not required assuming you're using http 1.1(your webserver almost certainly does). Drawback is their browser cant show download time/size remaining.

Categories