SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue With Image Upload Using $_FILES

    I'm hoping someone can give me some insights on this, because I'm a little puzzled. I have a script that allows a user to upload product information, including an image. (I'm still in the coding/testing stage.) But, I keep running into a weird issue: when I browse and find the file I want to upload, it won't always upload it. I've put in echo statements to check values, for instance, the "tmp_name" value of the $_FILES array. When it comes back empty, I know it's not being uploaded. But, this is the weird part: it doesn't follow any pattern. I'll choose an image, hit upload, and it doesn't take. I'll choose another, same thing. I may do that three, four, or however many times, and then one will upload. (I also have a copy statement so I know if the image is being sent from the temp folder to its new destination.)

    For a while I thought it might be a cache issue, but adding the cache settings for the header didn't help. I even took the code out of the page I was working on and inserted it into a bare-bones page, but still no success. Does anyone know of any factors that may be causing this?

    (Tested in FF3 and IE7. Running Apache 2.2.)

    Since I was still having the same problem with the "bare-bones" code, I figured I'd post that, so it'll be less to look through. Thanks in advance! If you need more info, let me know.

    (Also, I apologize in advance if there is a really simple answer. I've tested it myself, looked around online, and can't seem to find one.)

    -------------------------------------------------------------------

    <?php
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', FALSE);
    header('Pragma: no-cache');
    ?>
    <html>
    <head><title>test</title></head>
    <body>
    <form id="frmProductAdd" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"
    onsubmit="javascript:return validateProductAdd();">
    <label for="flImg" id="flImg">Product Image: </label>
    <input type="file" class="txtMed" id="flImg" name="flImg" maxlength="300" />

    <input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" />

    file uploaded?:
    <?php

    echo "File name: ".$_FILES['flImg']['tmp_name'];

    if (is_uploaded_file($_FILES['flImg']['tmp_name']) and copy($_FILES['flImg']['tmp_name'],"photos/newpic.jpg")){
    echo "<p>File stored</p>";
    }else{
    echo "<p>Not stored</p>";
    }
    ?>
    </form>

    </body>
    </html>
    Last edited by katendarcy; Dec 1, 2008 at 14:43.

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php
    header
    'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
    header'Cache-Control: no-store, no-cache, must-revalidate' );
    header'Cache-Control: post-check=0, pre-check=0'FALSE );
    header'Pragma: no-cache' );
    echo 
    "<pre>";
    print_r$_FILES );
    if ( isset(
    $_POST['submit']) )
    {
        if ( 
    is_uploaded_file($_FILES['flImg']['tmp_name']) and copy($_FILES['flImg']['tmp_name'],
            
    "photos/newpic.jpg") )
        {
            echo 
    "<p>File stored</p>";
        }
        else
        {
            echo 
    "<p>Not stored</p>";
        }
    }
    ?>
    <html>
    <head><title>test</title></head>
    <body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <label for="flImg" id="flImg">Product Image</label>
    <input type="file" id="flImg" name="flImg" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ernie1, thanks for the info. I'm still having the same issue, though. I considered the possibility that it could be some issue with using a pic I had tried to upload before, (though I don't know why it would), but that doesn't seem to be the case. It still happens on new pictures. Again, the frustrating part is that it doesn't always happen, just sporadically. But, obviously, something working most of the time isn't good enough!

    Also, in reality, after posting back to itself, the upload page will, if there are no errors, re-direct to another page that contains the actual confirmation. This confirmation was just for my testing purposes.

    Thanks again.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would smaller files succeeding, and larger files failing be a pattern to the behavior you're observing? php has configuration limits which can come into play here. You should read the manual here http://www.php.net/features.file-upload and check upload_max_filesize and post_max_size

    The manual shows you how to check the upload error, if any,

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe they're all around the same size, but I'll definitely take a look at the manual you suggested. Hopefully I'll be able to try some things out within the next couple of days. Thanks!

  6. #6
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    crmalibu, thanks for the advice and the link. It got me on the right track. The file size was indeed the pattern. Some of the images were topping off just over 2mb, though some from the same set were just under 2mb. (Not sure why I didn't catch onto that one! : ) A quick look online found the solution to be changing the default "2m" for the "upload_max_filesize" setting in the php.ini file. (The images were hi-res, thus why they were a larger size.) Of course, I may end up wanting to limit the file size for this project anyway, but knowing what's wrong and how to fix it is a good feeling! I wasn't real familiar with the upload settings, so it's good for me to get into them. Thanks again!

    Thanks again Ernie1 for your suggestion, too.


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
  •