+ Reply to Thread
Results 1 to 6 of 6

Thread: mysql insert image into database

  1. #1
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    mysql insert image into database

    hi i've created a script for users to upload images. but i keep getting this error.

    fopen() [function.fopen]: Filename cannot be empty
    this is the script.

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <?php
    $thisfile="uploadpic.php";

    $form = <<<FORM
    <form action="$thisfile" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
    <br/>
    <input type="submit" name="submit" value="Upload Image">
    </form>
    FORM;

    if (isset($_POST['submit']))
    {$fileName = $_FILES['photo']['name'];
    $tmpName = $_FILES['photo']['tmp_name'];
    $filesize = $_FILES['photo']['size'];
    $filetype = $_FILES['photo']['type'];
    if(!isset($_FILES['photo']))
    {$messgae = "Please select an image to upload.";}

    elseif($_FILES['photo']['size'] >= 500000)
    {$message = "Your image is too large. Please keep the image size under 500kb.";}

    else
    {$fp = fopen($tmpname, 'r');
    $photo = fread($fp, $filesize);
    $photo = mysql_real_escape_string($photo);
    fclose($fp);
    $query= "UPDATE databse
    SET photo = '$photo'
    WHERE email = '$email'";
    $result = mysql_query($query);
    if (mysql_affected_rows() == 1)
    {$message = "Your photo has been uploaded.";}
    else
    {$message = 'There was an error. Your photo has not been uploaded.';}
    }
    }
    else
    {$message = $form;}


    ?>
    </head>
    <body>
    <h1>Profile picture</h1>
    <?php echo $message; ?>
    </body>
    </html>
    Last edited by tillabong; 01-12-2010 at 09:33 AM. Reason: typing error

  2. #2
    xgreenberetx is offline x10Hosting Member xgreenberetx is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    57

    Re: mysql insert image into database

    For one you have to many < in front of the form tag
    Code:
    $form = <<<FORM
    should be
    Code:
    $form = <FORM
    Then change fopen to this
    Code:
    $fp = fopen($tmpname, "r");  //<-----use double quotes instead of single
    Last edited by xgreenberetx; 01-12-2010 at 10:34 AM.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: mysql insert image into database

    ]

    fopen() [function.fopen]: Filename cannot be empty
    this is the script.

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <?php
    $thisfile="uploadpic.php";

    $form = <<<FORM
    <form action="$thisfile" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
    <br/>
    <input type="submit" name="submit" value="Upload Image">
    </form>
    FORM;

    if (isset($_POST['submit']))
    {$fileName = $_FILES['photo']['name'];
    $tmpName = $_FILES['photo']['tmp_name'];
    $filesize = $_FILES['photo']['size'];
    $filetype = $_FILES['photo']['type'];
    if(!isset($_FILES['photo']))
    {$messgae = "Please select an image to upload.";}

    elseif($_FILES['photo']['size'] >= 500000)
    {$message = "Your image is too large. Please keep the image size under 500kb.";}

    else
    {$fp = fopen($tmpname, 'r') ;
    $photo = fread($fp, $filesize);
    $photo = mysql_real_escape_string($photo);
    fclose($fp);
    $query= "UPDATE databse
    SET photo = '$photo'
    WHERE email = '$email'";
    $result = mysql_query($query);
    if (mysql_affected_rows() == 1)
    {$message = "Your photo has been uploaded.";}
    else
    {$message = 'There was an error. Your photo has not been uploaded.';}
    }
    }
    else
    {$message = $form;}


    ?>
    </head>
    <body>
    <h1>Profile picture</h1>
    <?php echo $message; ?>
    </body>
    </html>
    Check where you use fopen. It is saying that $tmpname is empty.

    But how can that be? You have assigned a value to it.

    $tmpName = $_FILES['photo']['tmp_name'];

    Ooops. $tmpname != $tmpName
    Edit:
    For one you have to many < in front of the form tag
    Code:
    $form = <<<FORM
    should be
    Code:
    $form = <FORM
    The
    Code:
     
    <<<FORM
    Stuff
    FORM
    construct is not a form tag. It is a method of quoting a string called a heredoc
    Last edited by descalzo; 01-12-2010 at 10:28 AM. Reason: Automerged Doublepost
    Nothing is always absolutely so.

  4. #4
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    Re: mysql insert image into database

    Yeah its heredoc.

    I've change the $tmpname thing and the double quotation and i get this error.
    fopen() expects parameter 1 to be string, array
    fread(): supplied argument is not a valid stream resource
    fclose(): supplied argument is not a valid stream resource

    im having another problem. i tried uploading a file that is 209kb but it still comes out as my file being too large. am i missing out any detail on the file size part?

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: mysql insert image into database

    Quote Originally Posted by tillabong View Post

    im having another problem. i tried uploading a file that is 209kb but it still comes out as my file being too large. am i missing out any detail on the file size part?
    Try adjusting this line


    $form = <<<FORM
    <form action="$thisfile" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="25000"> Choose an image to upload: <input type="file" name="photo">
    <br/>
    <input type="submit" name="submit" value="Upload Image">
    </form>
    FORM;
    Nothing is always absolutely so.

  6. #6
    tillabong is offline x10Hosting Member tillabong is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    60

    Re: mysql insert image into database

    Ok thank you very much

+ Reply to Thread

Similar Threads

  1. MySQL Database Problem
    By peeman in forum Free Hosting
    Replies: 2
    Last Post: 12-11-2009, 02:02 PM
  2. How to create MySQL database and user
    By Jesse in forum Tutorials
    Replies: 11
    Last Post: 06-04-2008, 12:25 PM
  3. WP mysql error : Error establishing a database connection
    By orangpelupa in forum Free Hosting
    Replies: 2
    Last Post: 12-19-2007, 05:08 AM
  4. Database Mysql
    By TheCentaury in forum Free Hosting
    Replies: 3
    Last Post: 08-23-2005, 09:55 PM
  5. Could not connect to MySQL database?
    By chiamingen in forum Free Hosting
    Replies: 4
    Last Post: 03-16-2005, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers