File Upload Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Muttly

    File Upload Problem

    Hey all. I'm trying to upload multiple files. I'm using the chm file from
    php.net to help me figure it out. I also checked online and say theres a
    problem with the file. Anyway My html looks like this:

    <form enctype="multip art/form-data" action="file-upload.php" method="post">
    Send these file:<br>
    <input name="userfile[0]" type="file"><br >
    <input name="userfile[1]" type="file"><br >
    <input name="userfile[2]" type="file"><br >
    <input type="submit" value="Send File">
    </form>
    <form enctype="multip art/form-data" action="file-upload.php" method="post\">
    <input type="hidden" name="MAX_FILE_ SIZE" value="1500000" >
    Send this file:<br>
    <input name="userfile" type="file"><br >
    <input type="submit" value="Send File">
    </form>

    My file-upload.php is:

    <?php
    $uploaddir = "files/";
    print "<pre>";
    if (move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploaddir .
    $_FILES['userfile']['name'])) {
    print "File is valid, and was successfully uploaded. Here's some more
    debugging info:\n";
    print_r($_FILES );
    }
    else{
    print "Possible file upload attack! Here's some debugging info:\n";
    print_r($_FILES );
    }:
    ?>

    This works for a single file and I've tried putting the <input type="hidden"
    name="MAX_FILE_ SIZE" value="1500000" > in the multiple file aswell and it
    doesn't give me an error it just comes back with:

    Possible file upload attack! Here's some debugging info:
    Array
    (
    [userfile] => Array
    (
    [name] => Array
    (
    [0] => 1_1024x768.jpg
    [1] => 2_1024x768.jpg
    [2] => 3_1024x768.jpg
    )
    [type] => Array
    (
    [0] => image/pjpeg
    [1] => image/pjpeg
    [2] => image/pjpeg
    )
    [tmp_name] => Array
    (
    [0] => C:\WINDOWS\TEMP \php1A5.tmp
    [1] => C:\WINDOWS\TEMP \php1A6.tmp
    [2] => C:\WINDOWS\TEMP \php1A7.tmp
    )
    [error] => Array
    (
    [0] => 0
    [1] => 0
    [2] => 0
    )
    [size] => Array
    (
    [0] => 461796
    [1] => 576646
    [2] => 640622
    )
    )
    )
    Can someone help me please. I don't know what I'm doing wrong and I can't
    figure it out.Thanks in advance.



  • Ian.H

    #2
    Re: File Upload Problem

    On Mon, 16 Aug 2004 01:09:28 +0100, Muttly wrote:
    [color=blue]
    > Hey all. I'm trying to upload multiple files. I'm using the chm file from
    > php.net to help me figure it out. I also checked online and say theres a
    > problem with the file. Anyway My html looks like this:
    >
    > <form enctype="multip art/form-data" action="file-upload.php" method="post">
    > Send these file:<br>
    > <input name="userfile[0]" type="file"><br >
    > <input name="userfile[1]" type="file"><br >
    > <input name="userfile[2]" type="file"><br >
    > <input type="submit" value="Send File">
    > </form>
    > <form enctype="multip art/form-data" action="file-upload.php" method="post\">
    > <input type="hidden" name="MAX_FILE_ SIZE" value="1500000" >
    > Send this file:<br>
    > <input name="userfile" type="file"><br >
    > <input type="submit" value="Send File">
    > </form>
    >
    > My file-upload.php is:
    >
    > <?php
    > $uploaddir = "files/";
    > print "<pre>";
    > if (move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploaddir .
    > $_FILES['userfile']['name'])) {
    > print "File is valid, and was successfully uploaded. Here's some more
    > debugging info:\n";
    > print_r($_FILES );
    > }
    > else{
    > print "Possible file upload attack! Here's some debugging info:\n";
    > print_r($_FILES );
    > }:
    > ?>[/color]


    [ snip ]


    You need to perform a loop.. you're trying to handle an array as a string
    / file. Try something like:


    $errors = array();

    for ($i = 0; $i < sizeof($_FILES['userfile']); $i++) {
    $src = $_FILES['userfile']['tmp_name'][$i];
    $dest = $uploaddir . $_FILES['userfile']['name'][$i];

    if (!move_uploaded _file($src, $dest)) {
    array_push($err ors, $_FILES['userfile']['name'][$i];
    }
    }

    if (sizeof($errors ) > 0) {
    foreach ($errors as $error) {
    echo 'Failed to upload: ' . htmlentities($e rror) . "<br />\n";
    }
    }


    HTH.



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    Working...