www.webdeveloper.com
Results 1 to 2 of 2

Thread: my image upload php script won't work plz help

  1. #1
    Join Date
    Oct 2010
    Posts
    13

    my image upload php script won't work plz help

    hi i am trying to upload an image but it doesn't work I have been trying for ages but without any luck. I was wondering if anyone would be kindly enough to take a look at it

    Basicly i'm getting my echo error message "problem uploading image" displayed constantly, even before i try upload it keeps saying it. It is also not uploading.
    my database
    id (int5) auto_increment
    image_name (varchar100)
    image (blob)

    Here is my code i have two php files.
    Code:
    //addproduct.php
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <table width="600" height="382" border="3" BORDERCOLOR="#CD3788" bgcolor="#FFFFFF" align="center" cellpadding="0" cellspacing="2">
    <tr>
    <form action="addproduct.php" method="post" enctype="multipart/form-data">
    <td>
    <table width="100%" border="0" BORDERCOLOR="#CD3788" cellpadding="3" cellspacing="1">
    <tr>
    <td colspan="3"><strong>Add Product</strong></td>
    </tr>
    
    <tr>
    <td><font color="black">Add Image</font></td>
    <td>:</td>
    <td><input type="file" name="image" id="image" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Add">
    </td>
    </tr>
    </table>
    </td>
    
    </form>
    </tr>
    </table>
    
    <?php
    // connect to host and database
    include('config.php') or die(mysql_error());
    
    // file properties
    $file = $_FILES['image']['tmp_name'];
    
    if (!isset ($file))
    echo "Please select an image";
    else
    {
    $image = addslashes (file_get_contents ($_FILES['image'] ['tmp_name']));
    $image_name = addslashes ($_FILES['image'] ['image_name']);
    $image_size = getimagesize ($_FILES['image'] ['tmp_name']);
    
    if ($image_size==false)
    echo "Thats not an image";
    else
    {
    if (!$insert = mysql_query ("INSERT INTO products VALUES ('','$image_name','$image')"))
    echo "Problem uploading image.";
    else
    {
    $lastid = mysql_insert_id ();
    echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";
    }
    }
    
    }
    ?>
    </body>
    </html>
    Code:
    //get.php
    
    <?php
    include('config.php') or die(mysql_error());
    
    $id = addslashes($_REQUEST['id']);
    
    $image = mysql_query("SELECT * FROM products WHERE id=$id");
    $image = mysql_fetch_assoc ($image) ;
    $image = $image ['image'];
    
    header ("Content-type: image/jpeg");
    
    echo $image;
    ?>
    please help.
    thankyou for your time

  2. #2
    Join Date
    Oct 2010
    Posts
    13
    i have now updated it. It now adds to a database but does not display. Can anyone help please

    Code:
     
    //addproduct.php
    <table width="600" height="382" border="3" BORDERCOLOR="#CD3788" bgcolor="#FFFFFF" align="center" cellpadding="0" cellspacing="2">
    <tr>
    <form action="addproduct_action.php" method="post" enctype="multipart/form-data">
    <td>
    <table width="100&#37;" border="0" BORDERCOLOR="#CD3788" cellpadding="3" cellspacing="1">
    <tr>
    <td colspan="3"><strong>Add Product</strong></td>
    </tr>
    
    <tr>
    <td><font color="black">Add Image</font></td>
    <td>:</td>
    <td><input type="file" name="image" id="image" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Add">
    </td>
    </tr>
    </table>
    </td>
    
    </form>
    </tr>
    </table>
    Code:
    //addproduct_action.php
    <?php
    include('config.php');
    $file = $_FILES['image']['tmp_name'];
    if (!isset ($file)){
      echo "Please select an image";
    }else{
      $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
      $image_name = addslashes($_FILES['image']['name']);
      $image_size = getimagesize($_FILES['image']['tmp_name']);
    } 
    if($image_size==false){
      echo "Thats not an image";
    }else{
      
      $insert = mysql_query("INSERT INTO products VALUES ('','$image_name','$image')") or die("Problem uploading image.");
      echo mysql_error();
      $lastid = mysql_insert_id();
      echo "Image uploaded. <br />Your image:<p /><img src=get.php?id=$lastid>";
    }
    ?>
    Code:
    //get.php
    <?php
    include('config.php') or die(mysql_error());
    
    $id = addslashes($_REQUEST['id']);
    
    $image = mysql_query("SELECT * FROM products WHERE id=$id");
    $image = mysql_fetch_assoc ($image) ;
    $image = $image ['image'];
    
    header ("Content-type: image/jpeg");
    
    echo $image;
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles