Jump to content

PHP images help ?


twilkai

Recommended Posts

guys please help me .. i dont know how to insert images in my database .. and display it.. please help me.. i have a question table and choices .. in question table i have answer and question .. how should i code in order to insert and display the images ..

Link to comment
Share on other sites

Yo buddy.

It's not hard.

Indeed - there are a lot of tutorials on-line, but here is a good example for you to use...hope it helps.

 

I inlcuded 2 different ways to insert an image.  The first way is the OO approach.  Second is the procedural way.

Not sure how familiar you are with Object Oriented Programming, but that would be the way that I would do it - cleaner and more modular.

Also make sure that in your db script you have declared your columns that hold the images as blob type.

You can read about that here if you aren't familiar with it...

http://dev.mysql.com/doc/refman/5.0/en/blob.html

 

good luck.

 

<?php
// Open a connection to the DB
//$conn = new mysqli("localhost", "lamp", "", "images");

/*
echo "Thanks for uploading file :" . $_FILES['userfile']['name'] . "<br>";
echo "File size is : " . $_FILES['userfile']['size'] . "<br>";
echo "File type is : " . $_FILES['userfile']['type'] . "<br>";
echo "File tmp_name is : " . $_FILES['userfile']['tmp_name'] . "<br>";
*/

//$stmt = $conn->prepare("INSERT INTO images VALUES(NULL, ?)");
//$stmt->bind_param("s", $data);
//$file = $_FILES['userfile']['tmp_name'];
//$fp = fopen($file, "r");
//$size = 0;
//while ($data = fread($fp,1024)) {
//    $size += strlen($data);
//    $stmt->send_long_data(0,$data);
//}
//if ($stmt->execute()) {
//    print "$file ($size bytes) was added to the files table\n";
//} else {
//    die($conn->error);
//}
//
//
//echo '<html>';
//echo '<head></head>';
//echo '<body>';
//echo '<br><a href="download.php">Retrieve Image</a>';
//echo '</body>';
//echo '</html>';
//end oo approach

// Begin procedural style
// database connection
mysql_connect("localhost", "lamp", '') OR DIE (mysql_error());

// select the db
mysql_select_db ('images') OR DIE ("Unable to select db".mysql_error());

// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

// our sql query
$sql = "INSERT INTO images (image) VALUES ('{$imgData}')";

if(!mysql_query($sql)) {
    echo 'Unable to upload file';
}
else
{
    echo 'upload SUCCESS!';
}

echo '<html>';
echo '<head></head>';
echo '<body>';
echo '<br><a href="download.php">Retrieve Image</a>';
echo '</body>';
echo '</html>';


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.