SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: image upload

  1. #1
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    image upload

    Hi guys,
    I have this code that I use to upload images. It works fine, but when I click on upload with no file inserted I get:

    Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home2/ciudada2/public_html/images.php on line 21

    Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home2/ciudada2/public_html/images.php on line 23
    That's not an image

    How can I make these Warnings disappear, or at least not have the client see them if he clicks upload without inserting a file?


    code:
    PHP Code:
    <?php

    //connect to databse
    include ('connection.php');

    //file properties
    $file $_FILES['image']['tmp_name'];

    if (!isset(
    $file))
        echo
    "Please select an image.";
    else
      {
        
    //addslashes helps prevent sql injections
        
    $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 
    "That's not an image";
              else
              {
                if (!
    $insert mysql_query("INSERT INTO images VALUES ('','$image_name','$image')"))
                   echo
    "Problem uploading image";
                                else
                                {
                                 
    $lastid mysql_insert_id();
                                 echo
    "Image uploaded.<p /> Your image:<p /><img src=getimage.php?id=$lastid>";
                                }
              }

      }

    ?>

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,398
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    To suppress the errors add a @ to the function:
    PHP Code:
    @getimagesize() 
    But you should do more than that and at least display an error saying no image selected and end. I have not fully read your code but you could be adding empty records to your database otherwise.

  3. #3
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    God, I've been working on this for 2 hours now and then you come and solve it with one symbol. Such a newb.

    Thanks Rubble.

  4. #4
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you tell me why, when using the following code, it keeps on echoing "image too big" no matter what size my image is?


    PHP Code:
    <?php

    //connect to databse
    include ('connection2host.php');

    //file properties
    $file $_FILES['image']['tmp_name'];


    if (!isset(
    $file))
        echo
    "Please select an image.";
    else
      {
        
    //addslashes helps prevent sql injections
        
    $image = @addslashes(file_get_contents($_FILES['image']['tmp_name']));
        
    $image_name addslashes($_FILES['image']['name']);
        
    $size = @getimagesize($_FILES['image']['tmp_name']);

        if(
    $size==FALSE)
        echo 
    "That's not an image";
              else
              {                    
                                 
                            if (
    $size 100000)
                            {
                             echo
    "image too big";
                            }else
                                      {

                if (!
    $insert mysql_query("INSERT INTO images VALUES ('','$image_name','$image')"))
                   echo
    "Problem uploading image";
                                else
                                {
                                 
    $lastid mysql_insert_id();
                                 echo
    "Image uploaded.<p /> Your image:<p /><img src=getimage.php?id=$lastid height=\"$height\" width=\"$width\">";
                                }
                                      }
              }

      }

    ?>

  5. #5
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I found my mistake.

    I used " if ($size > 100000)" echo "image too big"; but $size= @getimagesize($_FILES['image']['tmp_name']);

    so I changed it to @getimagesize($_FILES['image']['size']);

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you please give me the code of html file u r using with this to upload data on database... i need it...........

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,402
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Adam,

    Just a quick heads up - the mysql functions that you're using in your script have been depreciated, and will be removed from PHP. You should think about switching to using the mysqli or PDO extensions instead.

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    730
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    100000 is rather small I would think for image uploading.
    http://www.flightpedia.org/convert/1...-megabits.html

    You might go with at least 393216.


Bookmarks

Posting Permissions

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