Uploading and retrieving images in the database

Hi,

Please can someone help me with the code for uploading the image into database and displaying it.

I am not getting how to do it

Well, if that is really what you want to do, we can help. But, you should understand that it is not the way you should do it. First, images put a lot of load on a database. Normally, most programmers save the pictures in a folder as it is much simpler to do. Then, the name of the picture and the path to the folder is stored in the database. Since most pictures are viewed by either tags or with a JQuery slideshow or FLASH viewer, they all prefer just a folder and filename for the displaying of the image. Now, some programmers do store icons and other small images inside of databases. But, again, they add to the overhead.

So, here is how you load a picture that has been posted from a form into a database:
(This is just general code, not test or set up for your example since you did not post any code…)
[php]

<?PHP // Let's guess it is a photo and you call the HTML FORM's field for it "photo" $image = file_get_contents($_FILES['photo']['tmp_name']); $image = mysql_real_escape_string($image); mysql_query("INSERT INTO {$table} SET imagedata='$image'"); ?>

[/php]
There is no error checking in this code. It is just the basics for you to get started with.
Normally, you select the image you want to upload from a HTML FORM. I called the file field “photo”.
Then, when the form POSTS to the PHP code, you get the contents of that file.
And, then insert it into the database table. Note that the field inside the database table must be a “blob”
field or “mediumblob” depending on how large the image might be.

Well, there is a start for you. When you get stuck, show us some code and we will help you further…

Hi,

Thanks!
I tried doing it.

But getting like this
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at

If i submit the form data to a different page for processing, it works. But if i submit data in the same page it is giving me this error. I am bit confused about session_start() where to use it.

here is my code

[php]<?php

            if (isset($_SESSION['error']))

            {

                echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";

                unset($_SESSION['error']);

            }

            ?>

            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

            <p>
             <label class="style4">Category Name</label>

               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="categoryname" /><br /><br />

                <label class="style4">Category Image</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                <input type="file" name="image" /><br />

                <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

               <br />

            </p>

            </form>




                         <?php

session_start();

require(“includes/conn.php”);

function is_valid_type($file)

{

$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");



if (in_array($file['type'], $valid_types))

    return 1;

return 0;

}

function showContents($array)

{

echo "<pre>";

print_r($array);

echo "</pre>";

}

$TARGET_PATH = “images/category”;

$cname = $_POST[‘categoryname’];

$image = $_FILES[‘image’];

$cname = mysql_real_escape_string($cname);

$image[‘name’] = mysql_real_escape_string($image[‘name’]);

$TARGET_PATH .= $image[‘name’];

if ( $cname == “” || $image[‘name’] == “” )

{

$_SESSION['error'] = "All fields are required";

header("Location: managecategories.php");

exit;

}

if (!is_valid_type($image))

{

$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";

header("Location: managecategories.php");

exit;

}

if (file_exists($TARGET_PATH))

{

$_SESSION['error'] = "A file with that name already exists";

header("Location: managecategories.php");

exit;

}

if (move_uploaded_file($image[‘tmp_name’], $TARGET_PATH))

{

$sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";

$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

header(“Location: mangaecategories.php”);

exit;

}

else

{

$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";

header("Location: mangagecategories.php");

exit;

}

?> [/php]

Well, you didn’t show your code the first post…

You can not send out HTML and then start a session in PHP.
(HTML sends a header to the browser and so does session_start(); and you can only have ONE header sent.
So, make the first two lines…

<?PHP session_start(); and remove the start later in your code. Then retest and see what other errors you might have... Good luck... And, good night, it's 1:30 AM here... tomorrow...
Sponsor our Newsletter | Privacy Policy | Terms of Service