Jump to content

Recommended Posts

i've read the whole thread.. i'm gonna try to fix your code or look into it but since you are using mysql to store images my first question if would work with images i would ask somewhere or here on phpfreaks what would be the best solution for storing images for a gallery or something similar. and everyone would answer me the same thing a folder on hdd no database

in the end my advice is use folders to store images and mysql to store their for outputting them on your site.

 

P.S i think everyone will agreed with me

 

I found this out after I used this method, too lazy to switch now. I won't be uploading mass amounts of images, or for that matter really putting huge loads on the DB. Just trying to figure out why the upload form rejects .jpg's or .png's.

<?
if(isset($_REQUEST['id']))
{
             // get the file with the id from database
			include 'connect.php';

			$id    = $_REQUEST['id'];
			$query = "SELECT `img_name`, `img_type`, `img_size`, `img_data` FROM img_tbl WHERE id = '$id'";

			$result = mysql_query($query) or die(mysql_error());
			list($name, $type, $size, $content) = mysql_fetch_array($result);

			header("Content-length: $size");
			header("Content-type: $type");
			print $content;

}
?>

to do have this on a live server  ?

 

P.S i think everyone will agreed with me

Well it really does depends, but yeah by defaut i would go for file and folder, but their have been cases where a database blob was better.

 

it's good to know that sometimes it's better usign blob

Unbelievable I still can't figure out why this won't take my .jpeg's!

 

If anyone can help, please do!

 

// Loading images into Database

                if(isset($_REQUEST[submit]) && $_FILES[imgfile][size] > 0)
                {
                                $fileName       = $_FILES[imgfile][name]; // image file name
                                $tmpName     = $_FILES[imgfile][tmp_name]; // name of the temporary stored file name
                                $fileSize           = $_FILES[imgfile][size]; // size of the uploaded file
                                $fileType         = $_FILES[imgfile][type]; // file type

                                $fp                    = fopen($tmpName, 'r'); // open a file handle of the temporary file
                                $imgContent  = fread($fp, filesize($tmpName)); // read the temp file
                                fclose($fp); // close the file handle

                                $query = "INSERT INTO img_tbl (`img_name`, `img_type`, `img_size`, `img_data` )
                                                                 VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";

                                mysql_query($query) or die('Error, query failed');
                                $imgid = mysql_insert_id(); // autoincrement id of the uploaded entry

                    echo "<br>Image successfully uploaded to database<br>";

                }else die('You have not selected any image');

// End loading image

 

When I try to upload it I get Error, query failed.

Hi

 

Change the query line to mysql_query($query) or die('Error, query failed '.mysql_error()); just to check what the error message is.

 

All the best

 

Keith

 

Error, query failed You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

 

Line 2 in that file is below:

 

<html xmlns="http://www.w3.org/1999/xhtml">

Or more appropriately mysql_real_escape_string(), and on all variables:

 

$query = sprintf(
"INSERT INTO `img_tbl` (`img_name`, `img_type`, `img_size`, `img_data`) VALUES ('%s', '%s', '%s', '%s')",
mysql_real_escape_string($fileName),
mysql_real_escape_string($fileType),
mysql_real_escape_string($fileSize),
mysql_real_escape_string($imgContent)
);

bad, thank you very much! That fixed the problem.

 

One last thing, does anyone know how to load the image into a template? As in have the image loaded from the database to display on a page and have a "border" around it per say. However, the border must be a image I made saved as a .png so the middle of it is seethrough where the image I uploaded load's.

 

Sounds confusing, but if anyone knows I'd GREATLY appreciate it. Thanks so much!

You should use a script that outputs the image data with the proper header; let's call it image.php:

 

<?php
$name = $_GET['name'];
//here you connect to the database and retrieve image type ($type) and data ($data) based on the filename from above (or preferably an ID, if possible)
//define allowed headers (image types)
$headers = array('image/gif', 'image/jpeg', 'image/png');
//set header
header('Content-type: ' . $headers[array_search($type)]);
//output image data
echo $data;
?>

And then produce the image in your HTML via image.php?name=imagename. To get the border thingy going, you can arrange one div on top of another, the bottom one holding the 'real' image, and the one in the front holding the border image. Utilizing CSS all the way.

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.