form upload with intermittent problems

Discussion in 'PHP' started by Rasputin, Jul 14, 2009.

  1. #1
    Hi

    I have an image upload that works fine for me (FF and IE) - but users often tell me doesn't work for them. I don't think it's filesize related because when I ask them to email the files they are usually pretty small. Am I missing something obvious, or is there a more reliable way to handle file uploads?

    $allowed_pics = array('image/jpeg', 'image/jpg', 'image/pjpeg');
    
    if(is_uploaded_file($_FILES['image1']['tmp_name'])){
    if(in_array($_FILES['image1']['type'], $allowed_pics)) {
    $field1_filename = "file1_".date("sihdmY").".jpg";
    if(!move_uploaded_file($_FILES['image1']['tmp_name'], "./files/".$field1_filename)){
    die("File " .  $_FILES['image1']['name'] . " Image 1 was not uploaded.");
    }
    }
    }
    Code (markup):
    I use the same code 8 times on the page (with different filenames) so that up to 8 pictures can be uploaded at once.

    Thanks for any help or suggestions!
     
    Rasputin, Jul 14, 2009 IP
  2. ashraful88

    ashraful88 Active Member

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    You can add more condition for image size limitation and for upload error.
    add this conditions in ur code....


    //to check the file size is not more then 100000 byte

    if($_FILES["file"]["size"] < 100000))
    {
    }


    //this condition for any error

    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
     
    ashraful88, Jul 14, 2009 IP
    Rasputin likes this.
  3. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Check you have set the form in html as application/multipart instead of post/get and also check you are giving the correct file field name there in your php code
     
    HivelocityDD, Jul 14, 2009 IP
    Rasputin likes this.
  4. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the suggestions...

    Is there any difference between 'application/multipart' and enctype='multipart/form-data' (which I currently use) ?

    I'd ignored error messages because I can't recreate the errors here, but you've got me thinking - perhaps something like

    if ($_FILES["file"]["error"] > 0)
    {mail (.,.,.);}
    with the error being mailed to me, would help me work it out

    cheers
     
    Rasputin, Jul 14, 2009 IP
  5. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    There are differences between application/multipart and multipart/form-data but for this script both should do. So this is not an issue.

    If everything is same for you and your clients' codes, then it must run. However, your server and their server may contain a basic difference. Mime type JPEG may be blocked on their server (for whom, the script is not working), though it's an odd case.
     
    techbongo, Jul 14, 2009 IP
  6. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks techbongo, I had wondered if I should change it to look at mime types with the word 'image' in them instead of specifying jpg etc - problem is I only want jpg files because they then go through a resizing routine etc.
    Shame if people can't upload jpg files...
     
    Rasputin, Jul 14, 2009 IP