Home > Archive > PHP Language > July 2004 > downloading file from mysql server
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
downloading file from mysql server
|
|
| Asit van der Heijden 2004-07-27, 8:55 pm |
| Hi all,
I've been working on this for a while, but i don't get it to work.
My idea is to upload a file to a mysql database. i`m using the longblob format.
The upload is working, but when I try to download the file (most of the files will be jpgs) it doesn't work.
I download a file exactly the same size as the original one but can't open the file.
when i do a cmp with the downloaded file and the orignal i get the following output:
asit@asitws:~$ cmp 23m1.jpg 23m.jpg
23m1.jpg 23m.jpg differ: byte 1, line 1
asit@asitws:~$
I just want to download the file or display the picture.
the source files:
*********upload file*********
<?
//connect function
require('../functies.php');
if(FALSE==($rDbConn=connectdb()))
{
exit;
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
if ($_FILES['binFile']['size'] >0) {
$data = addslashes(fread(fopen($_FILES['binFile'
]['tmp_name'], "rb"), $_FILES['binFile']['size']));
$strDescription = addslashes($_POST['txtDescription']);
$sql = "INSERT INTO tbl_Files ";
$sql .= "(description, bin_data, filename, filesize, filetype) ";
$sql .= "VALUES ('$strDescription', '$data', ";
$sql .='"'.$_FILES['binFile']['name'].'","'.$_FILES['binFile']['size'].'","'.$_FILES['binFile']['type'].'");';
if ( !$result = mysql_query($sql))
{
echo "jammer";
}
mysql_free_result($result);
echo "successfully added to our database.<br><br>";
echo "<a href='main.php'>Continue</a>";
}
else
{
echo "Empty file";
}
mysql_close();
}
else
{
?>
<HTML>
<BODY>
<FORM METHOD="post" ACTION="<? $_SERVER['PHP_SELF'] ?>" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<TABLE BORDER="1">
<TR>
<TD>Description: </TD>
<TD><TEXTAREA NAME="txtDescription" ROWS="5" COLS="50"></TEXTAREA></TD>
</TR>
<TR>
<TD>File: </TD>
<TD><INPUT TYPE="file" NAME="binFile"></TD>
</TR>
<TR>
<TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<?
}
?>
*************download file*********
<?php
require('../functies.php');
if(FALSE==($rDbConn=connectdb()))
{
exit;
}
$sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=9";
$result = @mysql_query($sql);
$data = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
header("Content-type: $type");
header("Content-Disposition: image/jpeg; filename=$name");
print $data;
//echo $data gives the same problem
?>
| |
| Kannan S 2004-07-28, 3:56 pm |
| Hello ,
Remove the following line
header("Content-Disposition: image/jpeg; filename=$name");
try now..
Sureshkannan
On Wed, 28 Jul 2004 00:25:41 +0000, Asit van der Heijden wrote:
> Hi all,
>
> I've been working on this for a while, but i don't get it to work.
>
> My idea is to upload a file to a mysql database. i`m using the longblob format.
> The upload is working, but when I try to download the file (most of the files will be jpgs) it doesn't work.
> I download a file exactly the same size as the original one but can't open the file.
> when i do a cmp with the downloaded file and the orignal i get the following output:
>
> asit@asitws:~$ cmp 23m1.jpg 23m.jpg
> 23m1.jpg 23m.jpg differ: byte 1, line 1
> asit@asitws:~$
>
> I just want to download the file or display the picture.
>
>
> the source files:
>
> *********upload file*********
>
> <?
>
> //connect function
> require('../functies.php');
>
> if(FALSE==($rDbConn=connectdb()))
> {
>
> exit;
> }
> if($_SERVER['REQUEST_METHOD']=='POST')
> {
> if ($_FILES['binFile']['size'] >0) {
>
> $data = addslashes(fread(fopen($_FILES['binFile'
]['tmp_name'], "rb"), $_FILES['binFile']['size']));
> $strDescription = addslashes($_POST['txtDescription']);
> $sql = "INSERT INTO tbl_Files ";
> $sql .= "(description, bin_data, filename, filesize, filetype) ";
> $sql .= "VALUES ('$strDescription', '$data', ";
> $sql .='"'.$_FILES['binFile']['name'].'","'.$_FILES['binFile']['size'].'","'.$_FILES['binFile']['type'].'");';
> if ( !$result = mysql_query($sql))
> {
> echo "jammer";
> }
>
> mysql_free_result($result);
> echo "successfully added to our database.<br><br>";
> echo "<a href='main.php'>Continue</a>";
> }
> else
> {
> echo "Empty file";
> }
> mysql_close();
>
> }
>
> else
> {
> ?>
> <HTML>
> <BODY>
> <FORM METHOD="post" ACTION="<? $_SERVER['PHP_SELF'] ?>" ENCTYPE="multipart/form-data">
> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
> <INPUT TYPE="hidden" NAME="action" VALUE="upload">
> <TABLE BORDER="1">
> <TR>
> <TD>Description: </TD>
> <TD><TEXTAREA NAME="txtDescription" ROWS="5" COLS="50"></TEXTAREA></TD>
> </TR>
> <TR>
> <TD>File: </TD>
> <TD><INPUT TYPE="file" NAME="binFile"></TD>
> </TR>
> <TR>
> <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
> </TR>
> </TABLE>
> </FORM>
> </BODY>
> </HTML>
> <?
> }
>
>
> ?>
>
>
>
>
> *************download file*********
> <?php
> require('../functies.php');
> if(FALSE==($rDbConn=connectdb()))
> {
>
> exit;
> }
> $sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=9";
>
> $result = @mysql_query($sql);
> $data = @mysql_result($result, 0, "bin_data");
> $name = @mysql_result($result, 0, "filename");
> $size = @mysql_result($result, 0, "filesize");
> $type = @mysql_result($result, 0, "filetype");
>
> header("Content-type: $type");
> header("Content-Disposition: image/jpeg; filename=$name");
> print $data;
>
> //echo $data gives the same problem
> ?>
| |
| Asit van der Heijden 2004-07-28, 3:56 pm |
| Hi,
that didn't work, i got the following message:
The image ?http://localhost/~asit/dvd/file/download.php? cannot be displayed, because it contains errors.
the line "header("Content-Dispositio........" was only for downloading the image.
thanks,
Asit
Kannan S <kannan@yahoo.com> wrote:
> Hello ,
>
> Remove the following line
>
> header("Content-Disposition: image/jpeg; filename=$name");
>
> try now..
>
> Sureshkannan
>
> On Wed, 28 Jul 2004 00:25:41 +0000, Asit van der Heijden wrote:
>
>
| |
| Jukka Hassinen 2004-07-28, 3:56 pm |
| Propably the data is stored incorrectly in the database. If I remember
correctly, $_FILES seems to be already escaped by PHP, so try removing
addslashes() from the line:
$data = addslashes(fread(fopen($_FILES['binFile'
]['tmp_name'], "rb"),
$_FILES['binFile']['size']));
Therefore this should do the trick:
$data = fread(fopen($_FILES['binFile']['tmp_name
'], "rb"),
$_FILES['binFile']['size']);
"Asit van der Heijden" <asit@xs6.xs4all.nl> wrote in message
news:41077d7c$0$49711$e4fe514c@news.xs4all.nl...
> Hi,
>
> that didn't work, i got the following message:
> The image ?http://localhost/~asit/dvd/file/download.php? cannot be
displayed, because it contains errors.
>
> the line "header("Content-Dispositio........" was only for downloading the
image.[color=darkred]
>
> thanks,
>
> Asit
>
> Kannan S <kannan@yahoo.com> wrote:
>
format.[color=darkred]
files will be jpgs) it doesn't work.[color=darkred]
open the file.[color=darkred]
following output:[color=darkred]
"rb"), $_FILES['binFile']['size']));[color=dark
red]
..='"'.$_FILES['binFile']['name'].'","'.$_FILES['binFile']['size'].'","'.$_FI
LES['binFile']['type'].'");';[color=darkred]
ENCTYPE="multipart/form-data">[color=darkred]
COLS="50"></TEXTAREA></TD>[color=darkred]
WHERE id_files=9";[color=darkred]
| |
| Asit van der Heijden 2004-07-29, 8:55 pm |
| That didn't work either.
but i found the problem, it was in a include, there was a <CR> before the <?php. didn't know
that matters. Thanks all for support,
Asit
Jukka Hassinen <Jukka.Hassinen@hut.fi> wrote:
> Propably the data is stored incorrectly in the database. If I remember
> correctly, $_FILES seems to be already escaped by PHP, so try removing
> addslashes() from the line:
> $data = addslashes(fread(fopen($_FILES['binFile'
]['tmp_name'], "rb"),
> $_FILES['binFile']['size']));
>
> Therefore this should do the trick:
> $data = fread(fopen($_FILES['binFile']['tmp_name
'], "rb"),
> $_FILES['binFile']['size']);
>
>
>
> "Asit van der Heijden" <asit@xs6.xs4all.nl> wrote in message
> news:41077d7c$0$49711$e4fe514c@news.xs4all.nl...
> displayed, because it contains errors.
> image.
> format.
> files will be jpgs) it doesn't work.
> open the file.
> following output:
> "rb"), $_FILES['binFile']['size']));
> .='"'.$_FILES['binFile']['name'].'","'.$_FILES['binFile']['size'].'","'.$_FI
> LES['binFile']['type'].'");';
> ENCTYPE="multipart/form-data">
> COLS="50"></TEXTAREA></TD>
> WHERE id_files=9";
>
>
|
|
|
|
|