Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>
    
    //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
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Separate the HTML form from the script.
    Make them two different files. That will
    help you a lot with troubleshooting your
    problem.


    .

  • #3
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    post here config.php file listing. I think that the problem is in mysql connection.
    the result of including config.php is true so your script will never die with mysql connection error.


  •  

    Posting Permissions

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