Jump to content

Upload image and displlay


yandoo

Recommended Posts

Hi there,

 

Im have simple code that i is form that allows users to upload images and then another page that displays them. Trouble is i cant get it working properly, i can upload the image but cannot display it...the image attributes dont appear?? 

 

Its taken from a turorial and i have a feeling that it has somerthing to do with outdated php syntax (lack of php by the <?)??...but im not sure what to do???

 

Heres the code that dislpays the images:

 

<?php
  mysql_connect("localhost", "root", "") or die(mysql_error());
  mysql_select_db("woodside"); 

$result = mysql_query("SELECT ImageId from image");
while ($row = mysql_fetch_array($result)) {
$ids[]=$row['ImageId'];
}
?>
<html>
<head>
<title>Image Loader</title>
</head>

<body>
select image:<br>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">id</td>
<td width="90%">Image</td>
</tr>
<tr>
<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach ($ids as $id) { ?>
<tr>
<td><a href="?id=<?= $id; ?>"><?= $id; ?></a></td>
</tr>
<?php } ?>
</table></td>
<td><?php if (isset($_GET['id'])) { ?>
<img src="image.php?id=<?= $_GET['id']; ?>"><?php } ?></td>
</tr>
</table>
</body>
</html>

 

If anybody could help me out that would be great :)

 

Thank You

Link to comment
Share on other sites

Hi, Yes thats right and im using this code to insert the image... I have also checked in phpmyadmin and can see the theres data in the Blob field....

 

Heres the Add_image code, incase it helps:

 

<?php
      if ($_POST['Submit']) {
        if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
          //print_r($_FILES);
          mysql_connect("localhost", "root", "") or die(mysql_error());
          mysql_select_db("woodside");
          $photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
           $query = sprintf("INSERT INTO image(Image, FileType) VALUES
('%s', '%s')", $photo, $_FILES['file']['type']);
           if (mysql_query($query)) {
            $messages[] = "Your files is successfully store in database"; 
           } else {
            $messages[]= mysql_error();
           }
          } else {
           $messages[]="The file is bigger than the allowed size please resize";
          }
        }
      ?>
      <html>
      <head>
      <title>Add Image</title>
      </head>    
      <body>
      <?php 
      if (isset($messages)) { 
        foreach ($messages as $message) {
         print $message ."<br>";
        } 
      }
      ?>
      <form action="" method="post" enctype="multipart/form-data" name="form1">
      <input type="file" name="file">
      <input type="hidden" name="MAX_FILE_SIZE" value="96000">
      <input type="submit" name="Submit" value="Submit">
      </form>
      </body>
      </html>

 

Thank You

 

Tom

Link to comment
Share on other sites

My advice to you is not to store the pictures in the database. You can just upload them to where ever you like then create the link to the picture and store the link in the database. Will make your life a whole lot easier. Problem with viewing the images is that you have to tell the browser it is an image using the header function then when you do that sometimes the text will not show up. It's just a nightmare. If you need help with some code to upload the image and store the link let me know.

 

Ray

Link to comment
Share on other sites

Hi Ray,

 

Yes some code to upload would be fantastic...I was wondering about that approach you mentioned, to physically store the images in a folder somewhere....

 

Is it ok to do it that way in terms of normalisation of data?? Im attempting to  build a database system for an animal refuge charity. On the table of records (detailing animals, that are housed), I was originally going to include a field in that table to store the  image of the animal. Then i started looking at that tutorial and thought to have seperate table for images (which would contain foreign key of other Animal record table)....

 

The approach you suggested does sound easier and make more sense but is there any disadvantages is that approach????

 

I hope im not waffling and making any sense....

 

Thank You

 

Tom 

Link to comment
Share on other sites

Hi there,

 

Ok yeah i can see the advantages in only storing the name or the pathname of the image in the database, that points to a folder with the images in. Think thisis the best approach now!

 

How do i do that? Do you know any tutorials or how to do it :)

 

 

Here the table structre for Animal table and Images table

Animal Table

AnimalID (P)

Name

Type*

Breed*

Age

Sex

etc..

 

Image Table

imageId (P)

Image

FileType

AnimalID* <--newly added field

 

 

 

Link to comment
Share on other sites

You don't really need 2 tables unless you plan on storing more than one picture even 2 or 3 you can keep in the same table. I wrote this with the intention of storing the link in the same table. Just add image to the table. It is also assumed that the images directory is in the same folder as this script. So in the example below this script is in the wwwroot/phpforum directory and the images folder is in the phpforum folder.

 

<?php
//image upload test
$file_dir = "C:/Inetpub/wwwroot/phpforum/images/";
$link_dir = "./images/";
$file_url = "http://192.168.20.114/phpforum/images";

if (isset($_POST['submit']))
{
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_type = $_FILES['image']['type'];
$uploadfile = $file_dir.basename($image_name);
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
// Query to insert data
$sql = "INSERT INTO `animaltable` (`Name`, `Type`, `Breed`, `Image`) VALUES ('".$_POST['pname']."', '".$_POST['ptype']."', '".$_POST['breed']."', '".$link_dir.$image_name."')";
echo "$sql<br />";
//mysql_query($sql)or die(mysql_error());

} else {
   echo "Possible file upload attack!\n";
}
// Uncomment these lines if you are having problems
//echo 'Here is some more debugging info:';
//print_r($_FILES);

print "</pre>";
  print "<center>Image path: $file_dir<br>\n";
  print "<center>Image name: $image_name<br>\n";
  print "<center>Image size: $image_size bytes<br>\n";
  print "<center>Image type: $image_type<p><br>\n\n";
      print "<img src=\"$file_url/$image_name\"><p>\n\n";
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
Pet Name <input type=text name=pname /><br />
Type <input type=text name=ptype /><br />
Breed <input type=text name=breed /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" accept=".jpg" size="20" name="image" title="Image Upload" /><br>

<input type="submit" name=submit value="Submit">

</form>

 

Uncomment the line for the query after you add all the fields for the table

 

Ray

Link to comment
Share on other sites

Hi,

 

Thats brilliant thanks. I dont even need to insert any other fields in the animal table as they would have already been entered at this point. So i gues i would only need to update the primary key AnimalID along with the image link as well.

 

Ok so ive tried testing and changed the 2 lines of code:

 

$file_dir = "C:/Inetpub/wwwroot/phpforum/images/";
$file_url = "http://192.168.20.114/phpforum/images";

 

 

to..:

$file_dir = "C:\wamp\www\Woodside\Images/";
$file_url = "http://192.168.2.2/Woodside/images";

 

When i test it out i get This error (HTTP 403 Forbidden)

 

also when i try uncommenting debugging bit...i get error:

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\Woodside\latestTEST.php on line 23

 

What am i doing wrong??

 

Thank You

 

Tom

Link to comment
Share on other sites

Hi there,

 

Is ok with respect to line 23 error message, i was just being silly and incommented the top line which has the word IF in it.....php then mistoke that for statement.

 

So no errosr appear now, but i m just refuded 404 error when click submit....

Wheh submit has been clicked and im taken to 404 error this is in the  browser addresses bar:

http://localhost/woodside/%3C?=$_SERVER['PHP_SELF']?>

 

Thanks for you help with this one :)

 

Tom

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.