Jump to content

Image View


ktalebian

Recommended Posts

Hi

I have uploaded images on my mySQL db using PHP.

Now I want to display them.

The code I use is

<?php
ob_start();
// Connected to db

$id = $_GET["id"];
$id = 4;
function clean($input, $maxlength)
{
$input = substr($input, 0, $maxlength);
$input = EscapeShellCmd($input);
return ($input);
}
$file = clean($file, 4);

$query = "SELECT name, type, content FROM `upload` WHERE id = '$id'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$type = $row["type"];
$name = $row["name"];
$content = $row["content"];
}

// Output the MIME header
header("Content-Type: {$type }");
// Output the image
echo $content;
?>

The problem with this code is that this has to be the first thing in the page. Also, anything that comes after it is not displayed!

Why?

Thank you

Link to comment
Share on other sites

If $row['content'] is the path to the image, then why not use:

 

echo "<img src=\"{$row['content']}\" />"

 

In working with images, ive usually set the content-type header before outputing an image with imagejpeg() or similiar functions, so im not sure it will work with just an echo. U can consider using gd functions if u cant get this to work:

header("Content-Type: image/jpeg");
$imgHandler = imagecreatefromjpeg($row['content']);
imagejpeg($imgHandler);

 

Still mentioning, if $row['content'] is the path to the image. Imagecreatefromjpeg() will copy the image and create an image resource which is outputed to the browser using imagejpeg. There are also imagecreatefromgif() imagecreatefrompng() and imagegif() imagepng().

 

Also when making a query find an ID, u know there is only one returned record so u dont need a while loop. Just "$row=mysql_fetch_array($result);" will do it.

 

Hope it was helpful.

Link to comment
Share on other sites

Hi

Ok, none of those codes worked!

The image is stored in the db, so I cannot use img src.

As for the

header("Content-Type: image/jpeg");

$imgHandler = imagecreatefromjpeg($row['content']);

imagejpeg($imgHandler);

when I run it, it says function imagecreatefromjpeg is not defined!

Link to comment
Share on other sites

Thanks for the reply:

Ok, using phpinfo, I have this

 

Configure Command  cscript /nologo configure.js "--enable-snapshot-build" "--with-gd=shared" 

 

As for the content, the content is actually the image. I uploaded the images into the db, the actual image and not a url.

 

As for the loop, I understand all that. Assuming that is not case, I still cannot display the image using your new method, and using the old method, that code must be the only thing in the page or else it would not work.

 

Allright, I may not have the GD installed. I downloaded the package from the website. Now how do install it?

Link to comment
Share on other sites

did u use smth like:

$im = imagecreatefromgif('filename.gif');
imagegif($im);

 

Im not sure when u say that the image is saved in the db. U should have images stored in a folder and save in db only the path or just the filename. Not sure how u stored them as images in db!!

Link to comment
Share on other sites

Yes that is the code that I have used.

And yes, I uploaded the images using the following code:

 


<?php
// Gathering data from form
$download_name = $_POST["download_name"];
$id = $_POST["id"];
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// Creating the $content file
move_uploaded_file($_FILES['userfile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$content = addslashes(fread($instr,filesize("latest.img")));
// Connecting to db ...
// Uploading file to db
$query = "INSERT INTO upload (name, size, type, content, download_name, type_id ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$download_name', '$id')";		
mysql_query($query); 
?>

 

the field "content" is a blob.

Link to comment
Share on other sites

That is an interesting approach but id say pointless. Why should u upload an actual image to the database, when u first upload it? Just the upload should have been fine. Id suggest to modify your code and change the part where u write the image data to the db. Just write the imagefile ex:

 

$fileName = $_FILES['userfile']['name'];
$query = mysql_query("INSERT INTO upload (filename) VALUES ('$filename')"); //and all the other fields

 

In this way u use the db to know what records have that image and show the actual image with a html img tags, gd or whatever u like. Believe me it will be a lot easier.

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.