FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Moving images from database to filesystem





Aredon
Well I started off creating a gallery for my website, and without thinking I began storing the images in our MySQL database. This caused two issues, firstly it seems to load much slower than I would expect, and secondly the thumbnail script I have doesn't work so well for compressing the BLOB data.

I would like to migrate the BLOB data from the database to the file system, but after a short while on google I can't seem to find any scripts of the kind to base a new one off of. Does anyone have a general idea of how this could be accomplished?
jmraker
You could just write a php script that reads every record in the table, gets the blob data and writes it to a file with a unique file name and the right file extension and the script can put the filename in another field.

Once you verify the files are ok and the site is updated so the blob isn't used anymore you can drop the field or set them all to null

You will need write permission in the directory you're putting the files because the script is probably running in a user profile that can't create, delete or edit files/directories.

Code:
<?php
// This might work if all images are .jpg files
$sql = 'SELECT id, image FROM table1'
$res = mysql_query($sql);
while($rec = mysql_fetch_array($res)){
   $name = 'table1_image_' . $rec['id'] . '.jpg';
   file_put_contents($name, $rec['image']);
   $sql2 = 'UPDATE table1 SET image_file="' . $name . '" WHERE id=' . $rec['id'];
   mysql_query($sql2);
}
?>
JVermolen
I guess you already have a HTML form like this in your file:
Code:

<form name="fileuploadform" method="post" enctype="multipart/form-data">
<input type="file" name="picture" />
<input type="submit" name="submit" value="upload picture! />
</form>


then you'll need a php script to upload the file to a certain area in your filesystem, i take the folder pictures in the below example, and write the name of the picture to the database:

upload script:
Code:

// first we need to know if the form is posted
if (isset($_POST['submit'])) {

//file upload
//below is what types of files are alowed and the max size allowed in bytes (below = 150kb)
if ((($_FILES["picture"]["type"] == "image/png") || ($_FILES["picture"]["type"] == "image/jpeg") || ($_FILES["picture"]["type"] == "image/jpg")) && ($_FILES["picture"]["size"] < 153600)) {

// Where the file is going to be placed
$target_path = "pictures/";

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['picture']['name']);

//move the file
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
     //file is moved
    $afbeelding = basename( $_FILES['picture']['name']);
   echo "The file ".  basename( $_FILES['picture']['name']).
    " has been uploaded";
} else {
     //error during moving
    echo "There was an error uploading the file, please try again!";
       // set the picture to no-image available
   $picture = "no-image.jpg";
}
   }
   else {
       // file did not match allowed file types or size
   echo "File type is not allowed or to large (150KB max)";
// set the picture to no-image available
   $afbeelding = "no-image.jpg";
   }
// now there is a } missing thats in the part below!


set pictures name into database:
Code:

// do require file with database connection or set database connection here
$query = "INSERT INTO tablename (picture) VALUES ('$afbeelding')";
      mysql_query($query) or die(mysql_error());


Dont forget to change some values to your own distinct ones and set the mysql column 'picture' to format text.

Hope this is what you mean
Aredon
Vermolen wrote:
I guess you already have a HTML form like this in your file:
Code:

<form name="fileuploadform" method="post" enctype="multipart/form-data">
<input type="file" name="picture" />
<input type="submit" name="submit" value="upload picture! />
</form>



Thanks, but all I needed was file_put_contents() which jmraker already posted. I was just looking for an example to pull the function from. I wasn't sure where to look for a BLOB to file conversion.
Related topics
*OFFICIAL* Which Browser do you use?
Windows File System Explained
image gallery: suggesions reqd
Gimp work for sale by commission
database backup
The logo of olimpic games
Trying to start out!!!
My phpBB 2.0.22 mods are giving problems
xMPACT.com is alive!! - And Updated 7-17-2008!!!!
Dreamweaver < Serif webplus
About to Give up on OpenOffice
Next Gen CUDA: Fermi
Do subliminal messages really work?
As we are all using computers why can't we work from home
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.