For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > October 2005 > image into mysql and out of mysql









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 image into mysql and out of mysql
Feudalac!

2005-10-21, 6:57 pm

What i need is to store lost of small images into mysql database and
read them later and display them.

i know that it can be done, but don't know how it can be done...


thanks
Jamie Davison

2005-10-21, 6:57 pm


On 10/21/05 9:07 AM, in article djap76$nq1$1@ss405.t-com.hr, "Feudalac!"
<feudalac@gmail.com> wrote:

> What i need is to store lost of small images into mysql database and
> read them later and display them.
>
> i know that it can be done, but don't know how it can be done...
>
>
> thanks



See . .

http://www.phpbuilder.com/columns/florian19991014.php3










Andreas Edin

2005-10-22, 3:55 am


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_dat
a']['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,f
iletype) ".
"VALUES
('$form_type_upload','$form_description'
,'$data','$form_data_name','$for
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
Feudalac!

2005-10-24, 6:56 pm

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_dat
a']['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,f
iletype) ".
> "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?

Andreas Edin

2005-10-24, 6:56 pm

Feudalac! wrote:

> Andreas Edin wrote:
>
>
> 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
[color=darkred]


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

--

rich@newsguy.com

2005-10-24, 6:56 pm

In article <djiluo$6tr$1@ss405.t-com.hr>, Feudalac! says...
>
>Andreas Edin wrote:
>
>
>but what if i don't want to download them? i want to show them in a
>html page?
>


If you didn't want to store and retrieve the images through MySQL, you can store
the path information instead. That way you only have to use and maintain a
simple string of text.

Since you want to display images in your HTML, they would usually be stored in
your web space. You can use SQL to build some type of query (e.g. sort by date,
name, etc) and dynamically print the results in your HTML using the file name
and/or path information.

Rich
--
Newsguy -- http://newsguy.com

Feudalac!

2005-10-25, 7:56 am

Andreas Edin wrote:

> Feudalac! wrote:
>
>
>
>
> 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


but i must display some text and then the image, after that some more
text and another image...

the example above is very helpful but the main problem stil exists...

I have a catalog with very small images (filesize no more that 2k) and
want to store those images in a database. when the catalog is displayed
- every image must be next to the corresponding type of product...
Feudalac!

2005-10-25, 7:56 am

rich@newsguy.com wrote:

> In article <djiluo$6tr$1@ss405.t-com.hr>, Feudalac! says...
> addslashes(fread(fopen($_FILES['form_dat
a']['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']; >> }
> ('$form_type_upload','$form_description'
,'$data','$form_data_name','$f
>
> If you didn't want to store and retrieve the images through MySQL,
> you can store the path information instead. That way you only have to
> use and maintain a simple string of text.
>
> Since you want to display images in your HTML, they would usually be
> stored in your web space. You can use SQL to build some type of query
> (e.g. sort by date, name, etc) and dynamically print the results in
> your HTML using the file name and/or path information.
>
> Rich
> --
> Newsguy -- http://newsguy.com


that i allready have solved but i am limited to the number of files i
can have and allso have a problem uploading them thru the web interface
(no write rights on the desired folder...)
Andreas Edin

2005-10-25, 6:57 pm

Feudalac! wrote:

> Andreas Edin wrote:
>
>
> but i must display some text and then the image, after that some more
> text and another image...
>
> the example above is very helpful but the main problem stil exists...
>
> I have a catalog with very small images (filesize no more that 2k) and
> want to store those images in a database. when the catalog is
> displayed - every image must be next to the corresponding type of
> product...


Just create two table's in your MySQL database. One with you images and
one with the meta information about the image. Decide witch one you are
going to use as primary listing. And for each itteration in the primary
table you do a sub query in the other table. And for each itteration
you create a new output to the webbrowser.


If you want more detailed description just say it.
Best regards Andreas Edin, Swede
--

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com