Page 1 of 1

Images | storing and reading

Posted: Tue Oct 04, 2005 5:00 am
by ATH0
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi !

This is sample script that should do the image upload:

Code: Select all

<?php
if ($submit) {
    mysql_connect("localhost","x","x");
    mysql_select_db("binary_data");

    $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
    $result= mysql_query("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
        "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

    $id= mysql_insert_id();
    print "<p>This file has the following Database ID: <b>$id</b>";
    mysql_close();

} else {
?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="form_description"  size="40">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>

<?php } ?>
I dont know what is wrong in this code, but nothing happens. You can make upload and there is no error, but there is no real upload to database ( empty database ). He? The uploaded file has cca 10K to max 300K


2.)
If i want to display 10 images ( sorted thru some filter ) how can i make this?
Im making a web page with this infos:

picture1 info link
picture2 info link
picture3 info link
picture4 info link
picture5 info link
picture6 info link

All pictures should be readed from the db. So, you got displayed picture, info, and link to some popup window where you can get more infos about the selected picture. How to make this?

ATH0


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Oct 04, 2005 6:56 am
by feyd
  1. your code is reliant on register_globals being on, which they should not be (security issues most often)
  2. you are storing binary data into your table, therefore you need a script to be called each time you want to display a single record from that table. It's often recommended to not store actual files inside a database if avoidable because of the performance issues associated with making, say ten separate page requests to display images (which is what your browser has to do.)
Some further reading:
viewtopic.php?t=37077
viewtopic.php?t=36110

Posted: Tue Oct 11, 2005 3:08 am
by ATH0
After searching the forum for such code i think it will be the best to ask...

The simple one:
If i upload 100 images to remote server, im making just image uploads ( no description , image name a so on).
I do this on the way that i read whole directory an after that i make upload.

Problems:
I need to save image description, image and image name, so i can not use the example written before.

Its a bit tricky (for me) but it would be easyer if i would have cca 10 image upload , 10 image description, and 10 image name field and one submit button , so i can send them all in one click...

How to make this and is there any code/tutorial on this ?

ATH0

Posted: Tue Oct 11, 2005 3:19 am
by feyd
you can have multiple file upload fields in a form. Naming them all specific names such as file[0], file[1], etc...

Posted: Tue Oct 11, 2005 3:39 am
by ATH0
is there any tutorial on this ?
All this will go then thru file stream ?

Posted: Tue Oct 11, 2005 11:00 am
by ATH0
This is example with blob. How to make this on "regular basis". Just path in db.

Code: Select all

<?php

// Connect to database

$errmsg = "";
if (! @mysql_connect("localhost","a","a")) {
        $errmsg = "Cannot connect to database";
        }
@mysql_select_db("testBase");

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time 

$q = <<<CREATE
create table pix (
    pid int primary key not null auto_increment,
    title text,
    imgdata blob)
CREATE;
@mysql_query($q);

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
        // Need to add - check for large upload. Otherwise the code
        // will just duplicate old file 
        // ALSO - note that latest.img must be public write and in a
        // live appliaction should be in another (safe!) directory.
        move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));
        if (strlen($instr) < 149000) {
                mysql_query ("insert into pix (title, imgdata) values (\"".
                $_REQUEST[whatsit].
                "\", \"".
                $image.
                "\")");
        } else {
                $errmsg = "Too large!";
        }
}

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
        $title = htmlspecialchars($row[title]);
        $bytes = $row[imgdata];
} else {
        $errmsg = "There is no image in the database yet";
        $title = "no database image available";
        // Put up a picture of our training centre
        $instr = fopen("../wellimg/ctco.jpg","rb");
        $bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
        header("Content-type: image/jpeg");
        print $bytes;
        exit ();
        }
?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src= width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>
</body>
</html>