Help with Image file Upload and display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
harshasks
Forum Newbie
Posts: 4
Joined: Thu Aug 30, 2007 6:11 am

Help with Image file Upload and display

Post by harshasks »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi I need help with regard to Image Upload and Image Display


This is the code I am using but there are errors at few places , as I am new to Php please help me

Code: Select all

<?php


$conn = mysql_connect("localhost", "root", "") 
  OR DIE (mysql_error());
@mysql_select_db ("imagedb", $conn) OR DIE (mysql_error());

// Do this process if user has browse the 
// file and click the submit button
if ($_FILES) {
  $image_types = Array ("image/bmp",
                        "image/jpeg",
                        "image/pjpeg",
                        "image/gif",
                        "image/x-png");
  if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) {
    $userfile  = addslashes (fread 
                 (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                 filesize ($_FILES["userfile"]["tmp_name"])));
    $file_name = $_FILES["userfile"]["name"];
    $file_size = $_FILES["userfile"]["size"];
    $file_type = $_FILES["userfile"]["type"];

    if (in_array (strtolower ($file_type), $image_types)) {
      $sql = "INSERT INTO image "
             . "(image_type, image, image_size, image_name, image_date) ";
      $sql.= "VALUES (";
      $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
             . "'{$file_name}', NOW())";
      @mysql_query ($sql, $conn);
      Header("Location:".$_SERVER["PHP_SELF"]);
      exit();
    }
  }
}

// Do this process of user has click 
// a file name to view or remove
if ($_GET) {
  $iid = $_GET["iid"];
  $act = $_GET["act"];
  switch ($act) {
    case rem:
      $sql = "DELETE FROM image WHERE image_id=$iid";
      @mysql_query ($sql, $conn);
      Header("Location:./index.php");
      exit();
      break;
    default:
      [b]print "<img src="image.php?iid=$iid">";[/b]//error here 
      break;
  }
}

?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File: 
<input type="file" name="userfile"  size="40">
<input type="submit" value="submit">
</form>
<?php
  $sql = "SELECT * FROM image ORDER BY image_date DESC";
  $result = mysql_query ($sql, $conn);
  if (mysql_num_rows($result)>0) {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $i++;
      $str .= $i.". ";
      $str .= "<a href='index.php?iid=".$row["image_id"]."'>"
           . $row["image_name"]."</a> ";
      $str .= "[".$row["image_date"]."] ";
      $str .= "[".$row["image_size"]."] ";
      $str .= "[<a href='index.php?act=rem&iid=".$row["image_id"]
           . "'>Remove</a>]<br>";
    }
    print $str;
  }
?>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please use

Code: Select all

tags when posting php code. see also: http://forums.devnetwork.net/faq.php?mode=bbcode
Post Reply