howto get image from db and store it in directory

Posted on 2007-10-01
Last Modified: 2013-12-13
I use this code to show image that stored in database :

$sql    = "SELECT * FROM picture WHERE username='$u'";
$result = mysql_query ($sql);
if (mysql_num_rows ($result)>0) {
  $row = @mysql_fetch_array ($result);
  $image_type = $row["image_type"];
  $image = $row["image"];
  Header ("Content-type: $image_type");
  print "$image";

my problem now, how to get the file and save it to directory?
Question by:hendri2
    1 Comment
    LVL 16

    Accepted Solution

    The easiest way is to click "save image" in the browser...

    However you can write a php script for example (like the one you are writing now) and instead of writing to the client (print "$image") writing to a file:

    $file = fopen("/home/user/images/img1.jpg","w");
    fwrite($file, $image);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now