Jump to content

PHP images help ?


twilkai

Recommended Posts

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
https://forums.phpfreaks.com/topic/171465-php-images-help/#findComment-905989
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.