Is it possible to store images in a folder but managed by MySql?

They have: 9 posts

Joined: Jan 2002

Hello.

I have a script that stored an image via mysql.

Is it possible to store images in a folder but managed by MySql?

Is it possible to store them into a folder?

Also, is there a way to link or display that image like in a php code or html?

Can we link to it like http:// or something similar?

Thanks.

They have: 9 posts

Joined: Jan 2002

Hello.

Here is the mysql dump file.

:

<?php
CREATE TABLE content
(
  
id mediumint(8) NOT NULL auto_increment,
  
category tinyint(4) DEFAULT '0',
  
date timestamp(14),
  
body_text text NOT NULL,
  
title tinytext NOT NULL,
  
binary_file blob NOT NULL,
  
PRIMARY KEY (id)
);
?>

How can we add a table to it that will store the location of a file, and be accesed again to be viewed in html or php code, uploaded and stored via the following php code, but not currently the technique used to upload the image]:

<?php

if (is_uploaded_file($userfile)) {
   
copy($userfile, \"C:\apache\htdocs\file\\");
} else {
    echo \
"Possible file upload attack\: filename '$userfile'\";
}
/* ...or... */
move_uploaded_file(
$userfile, \"C:\apache\htdocs\file\\");
?>

in such a way that it is processed through mysql, but the image is stored in a folder on the server, and not directly in the mysql database itself. So that only the location of the image on the server is stored in mysql automatically,

The image is currently called using code like:

domain.com/graphic_view.php?id=4

where id=4 is the id of the binary file stored in the mysql database.

At the moment the file is drectly stored and not some folder using the following code:

<?php
$query
= \"INSERT INTO $user_tablename VALUES(NULL,'$category', NULL,'$enabled_url_text', '$title', '$imagedata')\";
   
$result = mysql_query($query);
    if(!
$result) error_message(sql_error());
?>

and is displayed using the code [in the graphic_view.php file which is used to display the image at the moment]:

<?php
while ($row  mysql_fetch_array($news_query)){
       
$idnum=$row[ \"id\"];
       
$graphic=$row[ \"binary_file\"];
?>

Any ideas? Again, short of it, currently the image is directly stored in the mysql database, and I want only the image location to be stored in the mysql database, and image stored in a folder?

Thanks.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

If you're comfortable enough storing binary data (the image) in the database then converting it to use the filename instead should be a snap.

Just add a column to the table called ImageName or something. Then decide whether you want to store the whole path to the file, or if you always know that the images are going to be in a certain area you can just store the file name.

Then when the image is uploaded, extract the file name and store it in the database. You can still continue to use the same linking method (id=integer), but instead of the image being pulled as binary out of the database it will link to an image on your site.

I don't know enough about PHP to recommend how to do this, but it shouldn't be that difficult.

PJ | Are we there yet?
pjboettcher.com

They have: 17 posts

Joined: Jan 2002

Hi

don't store images in the database, it is best to store there
location, name, size, date_added, image_type and image_size_one, image_size_two

then draw that info from the database........

Example.........

say you have a forum............

In the forum a user can attach a file to a post so users can download the file.........

When you run you query on the post (topic_id or post_id)
you would add one field to your post table.........

field name = post_files, values 1 or 0 default = 0

then you could include a (if) statement to check if there is files to add......

Example.......

<?php
if ($row[post_files] == \"1\") {

$sql = \"SELECT * files WHERE post_id = '$id' AND topic = '$topic_id'\";

$result = mysql_db_query($db, $sql);
if(!
$result) {
$error = mysql_error();
die(
$error);
}
while (
$data = mysql_fetch_array($result)) {
echo \"<td><a href=\\"
\" . $data[location] . \"\\">\" .
$data[name] . \"</A> Type: \" . $data[file_type] . \"
Size: \" .
$data[file_size] . \"Date Added: \" .
$data[date_added]\";
}
?>

This is the best way to do it......

F!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.