File Upload

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

    #1

    File Upload

    I'm trying to get a file upload to work and it's not happening. Here's my
    form code...

    echo '<form enctype="multip art/form-data" action="catalog ue.php"
    method="post">
    <input type="hidden" name="MAX_FILE_ SIZE" value="204800">
    <table border="0" width="100%">
    <tr>
    <td width=100%>
    <center>
    <table border="0">
    <tr>
    <td>
    <font face="Tahoma">A dd Subcategory</font>
    </td>
    <td>
    <input name="subcat" type="text" style="width:22 7px">
    </td>
    </tr>
    <tr>
    <td>
    <font face="Tahoma">t o Category</font>
    </td>
    <td>';
    $result=mysql_q uery("select * from category");
    echo '<select name="category" style="width:22 7px" size="1">';
    for($i=0;$i<mys ql_num_rows($re sult);$i++)
    {
    $row=mysql_fetc h_array($result );
    echo '<option
    value="'.$row["category"].'">'.$row["descriptio n"].'</option>';
    }
    echo '</select>
    </td>
    </tr>
    <tr>
    <td>
    <font face="Tahoma">w ith picture</font>
    </td>
    <td>
    <input type="file" name="userfile" >
    </td>
    </tr>
    </table>
    </center>
    </td>
    </tr>
    </table>
    <input type="hidden" name="action" value="2">
    <p align="center"> <input type="submit" value="Add
    Subcategory"></p></form>';


    Which displayes the form perfectly well. However, the script which captures
    the form always seems to have a completly empty userfile variable. Here's my
    processing script:

    $userfile=$_FIL E['userfile']['tmp_name'];
    $userfile_name= $_FILE['userfile']['name'];
    $userfile_size= $_FILE['userfile']['size'];
    $userfile_type= $_FILE['userfile']['type'];
    $userfile_error =$_FILE['userfile']['error'];

    if(!$_REQUEST['subcat'])
    {
    echo '<p align="center"> <font face="Tahoma" color="#FF0000" ><b>
    No Category was Entered</b></font></p>';
    }
    else if($userfile_er ror > 0)
    {
    switch($userfil e_error)
    {
    case 1: echo "Picture too large, max size is 200kb"; break;
    case 2: echo "Picture too large, max size is 200kb"; break;
    case 3: echo "Picture failed to upload"; break;
    case 4: echo "No Picture"; break;
    }
    }
    else
    {
    $categoryid = $_REQUEST['category'];
    $subcat = $_REQUEST['subcat'];
    $result=mysql_q uery("select * from category where
    category=$categ oryid");
    $row=mysql_fetc h_array($result );
    echo '<p align="center"> <font face="Tahoma">' ;
    echo $subcat.' was added to '.$row["descriptio n"];
    echo '</font></p>';
    $result=mysql_q uery("insert into subcategory (subcategory, category,
    description) values (null, $categoryid, '$subcat')");
    }

    The script never processes the error section, the file doesn't get uploaded
    but the rest of the form works fine. If a file is specified or not in the
    other script the rest always works fine. Any help would be greatly
    appreciated!

    Phil





  • John Dunlop

    #2
    Re: File Upload

    Phillip Parr wrote:
    [color=blue]
    > $userfile_name= $_FILE['userfile']['name'];[/color]

    The autoglobal is $_FILES, not $_FILE.

    --
    Jock

    Comment

    • Pedro Graca

      #3
      Re: File Upload

      Phillip Parr wrote:
      (snip)[color=blue]
      > $userfile=$_FIL E['userfile']['tmp_name'];[/color]
      (snip)

      The PHP variable that holds information about uploaded files is not
      called $_FILE

      Try changing all your $_FILE[...] to $_FILES[...]
      -------------------------------------------^-----


      Also, when debugging I like to do a

      <?php
      echo '<pre>'; print_r($_FILE) ; echo '</pre>';
      ## or, better :-)
      ## echo '<pre>'; print_r($_FILES ); echo '</pre>';
      ?>

      and make sure I'm not messing up the indexes or something.


      Happy Coding :)
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Phillip Parr

        #4
        Re: File Upload

        I love you! It's wrongly printed in every line in a very good book I'm
        using... And because it was just one letter I guess I missed it when looking
        at php.net. Thank you :D

        Phil

        "John Dunlop" <john+usenet@jo hndunlop.info> wrote in message
        news:MPG.1a906d 1a12e4d15e98968 9@News.Individu al.NET...[color=blue]
        > Phillip Parr wrote:
        >[color=green]
        > > $userfile_name= $_FILE['userfile']['name'];[/color]
        >
        > The autoglobal is $_FILES, not $_FILE.
        >
        > --
        > Jock[/color]


        Comment

        Working...