|
Posted by Andreas Edin on 10/24/05 19:03
Feudalac! wrote:
> Andreas Edin wrote:
>
> >
> > It's pretty easy upload and download images or other files
> > to and from a MySql server.
> >
> > Here is an example how to upload it:
> > <?php
> > require('connect/open_connect.php');
> >
> > $form_description = $_POST['form_description'];
> > $form_type_upload = $_POST['type_upload'];
> > $data = addslashes(fread(fopen($_FILES['form_data']['tmp_name'],
> > "rb"), filesize($_FILES['form_data']['tmp_name'])));
> > $form_data_name = $_FILES['form_data']['name'];
> > $form_data_size = $_FILES['form_data']['size'];
> > $form_data_type = $_FILES['form_data']['type'];
> > }
> >
> > $result=MYSQL_QUERY("INSERT INTO binary_data (categori,
> > description,bin_data,filename,filesize,filetype) ".
> > "VALUES
> > ('$form_type_upload','$form_description','$data','$form_data_name','
> > $f or m_data_size','$form_data_type')");
> >
> > require('connect/close_connect.php');
> > ?>
> >
> > Here is an example how to download for viewing or download:
> > <?php
> >
> > if($_GET['id']) {
> > require('connect/open_connect.php');
> >
> > $query = "select description, bin_data, filename, filesize,
> > filetype from binary_data where id=".$_GET['id'];
> > $result = @MYSQL_QUERY($query);
> >
> > $data = @MYSQL_RESULT($result,0,"bin_data");
> > $type = @MYSQL_RESULT($result,0,"filetype");
> > $size = @MYSQL_RESULT($result,0,"filesize");
> > $name = @MYSQL_RESULT($result,0,"filename");
> > $desc = @MYSQL_RESULT($result,0,"description");
> >
> > require('connect/close_connect.php');
> >
> > header("Content-type: $type");
> > header("Content-length: $size");
> > header("Content-Disposition: attachment; filename=$name");
> > header("Content-Description: $desc");
> > echo $data;
> >
> > };
> > ?>
> >
> > Internet explorer will try to open det file/image, and if it can't
> > open it with a program it will send you a download prompt insteed.
> >
> > Good Lucky! If you want more detailed description just say it.
> > Best regards Andreas Edin, Sweden
>
> but what if i don't want to download them? i want to show them in a
> html page?
It's exactly what this code does
> > header("Content-type: $type");
> > header("Content-length: $size");
> > header("Content-Disposition: attachment; filename=$name");
> > header("Content-Description: $desc");
> > echo $data;
Content-type:
Sends information to the browser what type of document it is.
Content-length:
Describes how many bytes the browser shall expect to get
Content-Disposition:
How it will be sent, as an attachment, and what name it shall get.
Content-Description:
A short description about the file
Now you have sent all the information your browser need. But you don't
really have to send "Content-Disposition" and "Content-Description" it
works well without them.
The next step is to send the file from the database. And that's easy
just "echo $data;" in this example.
But if the browser doesn't know how to open the file by it's
content-type, it will automaticly open a download prompt.
If you want more detailed description just say it.
Best regards Andreas Edin, Sweden
--
[Back to original message]
|