Re: offering downloads of blobs (mysql) with php

  • From: Alex Hall <mehgcap@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Mon, 2 May 2011 07:34:16 -0400

The script is the first thing on the page, except the sql query to
actually find the blob content. I don't know, maybe I should create a
temporary file from the blob content and then download that...

On 5/2/11, Jacob Kruger <jacobk@xxxxxxxxxxxxxx> wrote:
> Not too sure about actually sending binary data if it's not in a physical
> file as such, but I did just test/try out the following to double check on
> the content type for a zip file.
>
> This worked well enough on my local WAMP server:
> <?php
> $PhysicalFileName = "pythonScripts.zip";
> header('HTTP/1.1 200 OK', True, 200);
> header('Content-Description: File Transfer');
> header('Content-Type: application/zip');
> header('Content-Disposition: attachment; filename="other.zip"');
> header('Content-Transfer-Encoding: binary');
> header('Expires: 0');
> header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
> header('Pragma: public');
> header('Content-Length: ' . filesize($PhysicalFileName));
>
> // readfile() will simply pass the file from the drive straight to the
> client.
> readfile($PhysicalFileName);
>
> // Exit to close the process.
> exit;
> ?>
>
> You can also test it here, and one other thing I thought of is you need to
> place this all above any actual HTML markup on the page, or else, yes, it
> might jumble itself up a bit since then that textual content will already
> have possibly been passed to the browser as such, but, you can try out the
> above test script here:
> http://www.blindza.co.za/test/
>
> Stay well
>
> Jacob Kruger
> Blind Biker
> Skype: BlindZA
> '...fate had broken his body, but not his spirit...'
>
> ----- Original Message -----
> From: "Alex Hall" <mehgcap@xxxxxxxxx>
> To: <programmingblind@xxxxxxxxxxxxx>
> Sent: Saturday, April 30, 2011 6:08 PM
> Subject: Re: offering downloads of blobs (mysql) with php
>
>
>> Thanks. Your script says to use readfile, but I have a blob, not a
>> file, so the few tutorials I have found say to echo the blob's
>> content. When I do that, though, I get the same result you would get
>> if you opened a zip file (my tests are all on zip files) with notepad;
>> mostly nothing readable. I have hard-coded the type to be
>> "application/zip" since I can't find a way to make php guess the mime
>> type from the file name, but I still get no download, just all that
>> gibberish text printed to the page. Readfile() does nothing, as I
>> expected since this is not actually a file, just the file's binary
>> data. Below is my script. Let me know if it does not come out right.
>>
>> <?php
>> include('php/common.php');
>> if(isset($_GET['id'])){
>> $id=$_GET['id'];
>> $con=dbConnect()
>>  or die("The file you requested could not be retrieved because the
>> database appears to be down. Please try again later.");
>> $sql='select m.filename, m.filesize, m.type, m.content, p.title from
>> media m, podcasts p where m.podcast='.$id.' and p.podcast_id='.$id;
>> //echo $sql.'<br>';
>> $res=mysql_query($sql)
>>  or die(heading(1, 'Error').'<p>There was a problem finding the file
>> you requested. Please try again later.</p><p>'.mysql_error().'</p>');
>> $title=mysql_result($res, 0, 'title');
>> $size=mysql_result($res, 0, 'filesize');
>> $filename=mysql_result($res, 0, 'filename');
>> $type=mysql_result($res, 0, 'type');
>> $content=mysql_result($res, 0, 'content');
>> //echo $title.' (title), '.$filename.' (file name), '.$size.' bytes,
>> type='.$type.'<br><br>';
>> header('HTTP/1.1 200 OK', True, 200);
>> header('Content-Description: File Transfer');
>> header('Content-Type: '.$type);
>> header('Content-Disposition: attachment; filename='.$filename);
>> header('Content-Transfer-Encoding: binary');
>> header('Expires: 0');
>> header('Pragma: public');
>> header('Content-Length: ' .$size);
>> readfile($content);
>> exit;
>> }
>> else{ //error out; no file to download
>> ?>
>>
>> <html>
>> <head>
>> <title>No File to Download</title>
>> </head>
>>
>> <body>
>> <?php
>> include_once('php/nav.php');
>> echo heading(1, 'No File Selected');
>> echo '<p>Please select a file; this page will only work if given a
>> valid file number to retrieve.</p>';
>> ?>
>> </body>
>> </html>
>> <?php } ?>
>>
>> On 4/30/11, Jacob Kruger <jacobk@xxxxxxxxxxxxxx> wrote:
>>> That would generally be related to redirecting content from another
>>> source,
>>> so it wants to change the file/content type that the browser is expecting
>>> as
>>> such.
>>>
>>> For example, the one thing I have is checking if someone is logged in to
>>> a
>>> site, before sending them a word document directly from a PHP script -
>>> idea
>>> is partly so they wouldn't be able to know/see the actual file path/URL,
>>> but
>>> the PHP script tells the browser it's receiving a word document, and then
>>> passes it through to the browser:
>>>
>>> $PhysicalFileName = "cnsttn/constitution.doc";
>>> header('HTTP/1.1 200 OK', True, 200);
>>> header('Content-Description: File Transfer');
>>> header('Content-Type: application/msword');
>>> header('Content-Disposition: attachment; filename="constitution.doc"');
>>> header('Content-Transfer-Encoding: binary');
>>> header('Expires: 0');
>>> header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
>>> header('Pragma: public');
>>> header('Content-Length: ' . filesize($PhysicalFileName));
>>>
>>> // readfile() will simply pass the file from the drive straight to the
>>> client.
>>> readfile($PhysicalFileName);
>>> exit;
>>> //that last statement closes the browser/server transaction/communication
>>> as
>>> such
>>>
>>>
>>> Jacob Kruger
>>> Blind Biker
>>> Skype: BlindZA
>>> '...fate had broken his body, but not his spirit...'
>>>
>>> ----- Original Message -----
>>> From: "Alex Hall" <mehgcap@xxxxxxxxx>
>>> To: "programmingblind" <programmingblind@xxxxxxxxxxxxx>
>>> Sent: Saturday, April 30, 2011 2:34 AM
>>> Subject: offering downloads of blobs (mysql) with php
>>>
>>>
>>>> Hi all,
>>>> I have a database project due next Wednesday. It is done, but I am
>>>> adding things since I have the time (and I really don't want to do my
>>>> calc homework right now). One of my databases is for podcasts, the
>>>> other for articles. The articles one is fine; I can serve that content
>>>> since it is stored in a longtext field, so I need only echo it to the
>>>> page. The podcast table is different, though. I looked up this stuff
>>>> already, but was taken to a script that was using a header() function
>>>> (php) and giving it mime information about the file, then it echoed
>>>> the blob, and that was it. I am wondering:
>>>> 1. What is all this mime stuff about?
>>>> 2. My only experience with files and webpages is putting a link to a
>>>> file, even a binary one, and letting the browser take care of it. Why,
>>>> then, did the script I found echo the blob and leave it at that? It
>>>> was not generating an html page with a link; the link was to
>>>> download.php, and download.php was the one doing all this with the
>>>> mime information and the echoing of the blob.
>>>> 3. Does anyone know of a good resource to find out more about serving
>>>> blobs so users can download them? Most of what I find is about
>>>> displaying images stored as blobs, but I want to offer a download link
>>>> for a given podcast's media file.
>>>> Thanks in advance.
>>>>
>>>> --
>>>> Have a great day,
>>>> Alex (msg sent from GMail website)
>>>> mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
>>>> __________
>>>> View the list's information and change your settings at
>>>> //www.freelists.org/list/programmingblind
>>>>
>>>
>>> __________
>>> View the list's information and change your settings at
>>> //www.freelists.org/list/programmingblind
>>>
>>>
>>
>>
>> --
>> Have a great day,
>> Alex (msg sent from GMail website)
>> mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: