Jump to content

grabbing an image(blob) from datatabse


welshbryant

Recommended Posts

Hey guys im new to the forumns and figured since it seems like people here are very knowledgable, i'd see if anyone might be able to help me find my error in my php code.  I am pulling the images from a database and have verified they went in properly.  Below is the code i used to upload the photo(which works) and the code for retreiving the image (which is broken).  The photo is saved in a table named photo in a field named uploaded_photo.  any advice on what i should do or something to try would be greatly appreciated.

 

addphoto2.php

 

        <b> Add a Gallary</b>

            <form name="form1" method = "post" enctype = "multipart/form-data">

 

      Photographer name: <input type="text" name="authorname" size="40" />      <br />

      Upload File: <input type="file" name="filename" id = "filename" size="40" />    <br />

      File Type: <select name = "select">

                  <option value = "JPEG">JPEG</option>

                  <option value = "GIF">GIF</option>

                  <option value = "PNG">PNG</option>

                  <option value = "other">Other</option>

                  </select>

            <br />

      Photo Name: <input type = "text" name = "photoname" />          <br />

      Photo Description: <input type = "text" name = "photodescription" />

                                                                <br />

 

      <input type="submit" value = "submit" />

        <input type="reset" value = "reset"/>

 

 

    </form>

   

<?php

include("connect.php") ;

 

$authname = $_POST['authorname'];

$phototype = $_POST['select'];

$photoitself = $_POST['filename'];

$photoname = $_POST['photoname'];

$photodesc = $_POST['photodescription'];

 

$image = fopen($_FILES['filename']['tmp_name'],"rb");

$readimage = fread($image,$_FILES['filename']['size']);

$newimage = mysql_escape_string($readimage);

 

 

 

$query ="INSERT INTO photo(photo_id, photo_type, photo_name, author_name, photo_description, uploaded_photo) VALUES(' ','$phototype','$photoname','$authname', '$photodesc', '$newimage')";

$result = mysql_query($query);

 

 

 

 

 

 

the code for retreviing the images is testbrowse.php:

 

<?

include("imgheader.php") ;

?>

 

<html>

 

<head>

<title>Sans Titre</title>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<meta name="generator" content="HAPedit 3.1">

 

</head>

<body bgcolor="#FFFFFF">

 

 

 

 

<?php

 

include("connect.php");

 

$query = "SELECT uploaded_photo FROM photo";

$results = mysql_query($query) or die(mysql_error()) ;

 

// need to fetch the array to get this line to work:

$row = mysql_fetch_assoc($results);

$news = ImageCreateFromString($row['uploaded_photo'])  ;

$final = imagejpeg($news);

echo $final;

 

?>

 

when calling testbrowse.php, all i get is the box with the red x inside for a broken image link.  I've been searching for answers through google however after 3 days and finding nothing i figured maybe i'd ask my fellow phpers. Thanks in advance

Link to comment
Share on other sites

is your image path returning correctly?

 

looks to me like the code for for test.php is trying to reference a graphic within the same directory as the script.

 

this line specifically

$news = ImageCreateFromString($row['uploaded_photo'])   ; //is looking for the graphic in the root directory of test.php

 

should maybe read

 

$img_path = 'directory path/on server/';//location where images are stored on server
$img_location = '' . $img_path . '' . $row['uploaded_photo'] . ''; //build link to photo

$news = ImageCreateFromString($img_location); //make image based of path instead of only image name

 

there maybe a way to simplify this but I broke it down so I could explain it.

 

chris

Link to comment
Share on other sites

hey severn,

 

i appreciate your help and can see how that would work however there is a little issue with the code you provided.  When i upload the file to the database, it stores them in the databse as the blob and not in a directory on the file server.  I guess i would like to know how i would go about storing the images on my file server or a workaround for having the images just stored in the database.  Is there a way to find the file location (such as a php command for the server side)? 

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.