Unable to download .sqlite file from the server - php

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"

Related

Wrong Response Body while trying to download photo using Retrofit2

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

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')); , ");"

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.

system call to gnuplot from php to generate plot?

So, I am using a php program to read a file, make some changes and then write it to a new file. After that, I call gnuplot, using a system call:
system('cat sarx.conf | /usr/bin/gnuplot');
sarx.conf has the gnuplot commands to generate the plot. The problem is if run my php from the command line (its on a linux server) it generates the image and stores it on the disk. But when I do the same thing by running the php on my browser it generates the image and tries to spit it out on the browser without actually storing it on disk.
Things i tried:
I though i might have had issues with permission settings but it didn't help.
I also hard coded the path where I want the image to be in sarx.conf. That didn't help either.
I also tried looking for it in the tmp directory --- no luck!!
Does anyone have any ideas on how can I get this to work? I need to store this image on disk so that my website can grab it to show the plot later. Is there any php stuff which can grab the image and write it to disk?
There is a great LGPL-licensed PHP interface to gnuplot here: http://www.liuyi1.com/PHP-GNUPlot/
Here is how you could do something similar:
$my_file = tempnam();
$handle = popen('gnuplot', 'w');
fwrite($this->ph, "Run some gnuplot commands here\n");
fwrite($this->ph, "set term png\n");
fwrite($this->ph, "set output ".$my_file."\n");
fwrite($this->ph, "replot\n");
flush($handle);
pclose($handle);
header('Content-Length: '.filesize($my_file));
header('Content-Type: image/png');
print file_get_contents($my_file);
unlink($my_file);

php: force download from different domains?

i wonder if you guys come up with some awesome solutions for my problem. the normal way is not working!
Well, i'm trying to force-download any file on any website with the following php script. I just pass ?p=http://www.whatever.com/images/flowers/rose.jpg to my url and the download prompts.
<?php
error_reporting(E_ALL);
if(isset($_GET['p'])) $path = $_GET['p'];
else echo "no path set!";
$file = $path;
//header('Location:' . $file); //works perfect, opens the file in the browser
//
header("Cache-Control: no-cache");
header("Expires: -1");
header("Content-Type: application/octet-stream;");
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
echo readfile($file);
?>
However, as I found out today filesize() just works with local files on my server not with an http request. The same applies to readfile()
Warning: filesize() [function.filesize]: stat failed for
pathtofiles…/downloader/d.php on line
15
Warning:
readfile(sphotos.ak.fbcdn.net/hphotos-ak-snc4/hs211.snc4/…)
[function.readfile]: failed to open
stream: HTTP request failed! HTTP/1.0
403 Forbidden in
pathtofiles…/downloader/d.php on line
16
i wonder if there are creative coders out there who can help me out here. Is there any chance for me to make that script work? I just want to forcedownload whatever file and url you pass along.
thank you advance,
regards matt
filesize() and readfile() can work with some protocols if your PHP settings allow.
The problem at hand is a more fundamental one, though:
HTTP request failed! HTTP/1.0 403 Forbidden in pathtofiles....
remember that when fetching a file using PHP, it does not have the user's login permissions available. The script instaince is acting like an independent browser. If a resource is password protected, you need to make your PHP script log in first.
You cannot 'force-download' for a file that is NOT under your control (as a remote file). The 'force-download' tells the browser to download the file that is about to be transmitted. Location: some-path tells the browser to look for the new location, thus listening the new location, not your current page.
One option, but not optimal, would be to make a local copy of the file (at your server), then retrieve it to the user. If the file is big enough, this will give the impression of a frozen page. To avoid this, you can read chunks of the remote file, and deliver after each read command.
Be aware, your code does not restrict what $file can download, thus allowing users to download virtually any readable file at the server, this is a security flaw.
The best way is to store filesize in your database if you have control over remote files and then use readfile function.
From PHP manual
readfile() You can use a URL as a filename with this function if the fopen wrappers have been enabled.

Categories