Library tutorials & articles

Uploading files in PHP

Introduction

Think of an application where you want your users to upload files to your machine. Uploading files in PHP is very easy. You can upload any type of file from client to server.

Files are uploaded from the browser using an <input> tag, with the type parameter in the <input> tag set to "File". This is supported by all popular browsers currently available on the market. The important thing is to set the ENCTYPE attribute of the form to "multipart/form-data" and set the form's action element to the file upload page. The file upload page will handle the file uploading.

The code is on the next page

Comments

  1. 28 Sep 2006 at 11:08

    I am trying to allow people to upload image files and post them to me.

     

    I have copied your code exactly but get the following;

    Parse error: parse error, unexpected T_IF in /nfs1/expatriatespain.com/www/test site/myupload.php on line 16

    Any ideas?

     

  2. 12 Jun 2005 at 15:58

    Is there anyway to change the filename as it is uploaded?   So  myhouse.jpg   is auto changed to file1.jpg


    For instance, anytime a file is uploaded via the form... Have it name the file   MYFILE.jpg


    so that when a new file is uploaded it overwrites the old file without having to have the user specify the correct filename before uploading it.

  3. 04 Feb 2005 at 05:37

    gud day!
    i have problems regarding the file i uploaded on the server.


    the file is pic.bmp


    i uploaded it in "subs/images"
    then i am about to create an exec statement to convert this logo.


    its permission is "-rw-------" and i would like to change it.
    i typed "chmod 777 pic.bmp"


    it says "operation not permitted"
    what can i do?

  4. 29 Jan 2005 at 21:16

    Hi,


    I'm relatively new to php. I'm using a code simliar to the one posted for file uploads. I was wondering if there was any way to set the permisson to "read only" upon upload. I've been having problems with uploading images that will only display if I manually set the permission in my site admin tool. Any help would be muck appreciated.
    Thank you,
    Mike

  5. 28 Jan 2005 at 08:32

    how can i get the exact address of the uploaded file i browsed.


    like when i browsed it from "C:/picture.bmp"


    how can i get this address? i mean how could i get these exact words?


    and when i upload this picture to the server, i declared the address of the server.
    but then it says, "permission denied"


    what does it mean?

  6. 17 Jan 2005 at 08:21
    You should add quotation marks to the upload.htm script as follows:

    method=post change to method="post"
    type=file change to type="file"
    type=submit change to type="submit"

    Also note - The php manual  recommends you include max file size, which appearently warns the user before the upload begins.

    btw the script worked fine for me after these corrections

    my site: carterettechservice.com
  7. 10 Jan 2005 at 08:30

    The T_IF error is the missing ";" at the end of the $uploaddir = "/path" statement in his code.  In my case the code simply isn't working and I'm not sure why.  The file just doesn't upload.

  8. 03 Jan 2005 at 09:14

    The problem is a missing ";"


    add a semi colon after:


    $uploaddir = "/upload/"


  9. 02 Jan 2005 at 05:40

    i have done everything you have said and this error comes up and cant figure out what this means right now


    Parse error: parse error, unexpected T_IF in c:\appserv\www\upload.php on line 7


    Thanks for any reply

  10. 03 Dec 2004 at 13:39

    Are there C functions equivalent to those in php which will allow
    me to write the upload.php in C? I have a lot of form processing
    that is all written in C, and I would like to integrate the uploading
    of files right into other procedures.
    Thanks for you help.
    John

  11. 05 Sep 2004 at 20:48

     This error message is common when the upload directory does not have the right permissions for the web server to write to it.


     You must change the write permissions of the upload directory allowing the web server to write to it.

  12. 29 Jul 2004 at 03:40
    You might want to replace the double quotes in the second [] too ...

    Steven
  13. 29 Jul 2004 at 03:39
    Try $_FILES['userfile']["tmp_name"] ... I even had to 'waste' the single quote in a script where the var userfile was variable itself.

    Steven
  14. 18 May 2004 at 16:18
    Hello, I am new to PHP and have done a little bit of research into uploading files. I believe you need to have a 'hidden' field in the form called 'MAX_FILE_SIZE' with a value no greater than 2000000(that's the default in most PHP configs). I hope this helps!

    Nick
  15. 13 May 2004 at 18:31
    I seem to have the same problem as someone in an earlier post.  I don't seem to have my directory correct.  Here is the portion of my code that specifies the directory:

    move_uploaded_file($_FILES['userfile']['tmp_name'], "home/successa/public_html/".$_FILES['userfile']['name']);

    When I run it, I get:

    Warning: move_uploaded_file(home/successa/public_html/NTUSER.DAT.LOG): failed to open stream: No such file or directory in /home/successa/public_html/processfile.php on line 15

    Warning: move_uploaded_file(): Unable to move '/tmp/php6fQXnx' to 'home/successa/public_html/NTUSER.DAT.LOG' in /home/successa/public_html/processfile.php on line 15

    I don't know how to specify the directory.  When I log into my control panel, whatever root directory I am in, I see a public_html folder there.  I've tried just specifying "/public_html" as the directory but that doesn't work either!  I read someplace that it should be the "absolute location" but in this case I'm not sure what that means.  I've checked for write permission and I do have that...???  I feel so dumb!    
  16. 10 Oct 2003 at 18:32

    i


    when execute that script the value of $FILES["userfile"]["tmpname"], $FILES["userfile"]["type"], and $FILES["userfile"]["size"] are empty,
    the value of $_FILES["userfile"]["name"], yes, take the name of the file to upload, but, don't make nothing, why ?




    thank's in advance

  17. 19 Jun 2003 at 01:02
    Sorry about the delayed reply! Try this:

    <?php

    if(!empty($userfile))
    {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    elseif (empty($userfile))
    {
    echo("FAILED!");
    }

    ?>
  18. 10 Jun 2003 at 14:40

    This is due to a change in the way PHP lets you access POST'ed variables. I've updated the article to reflect these changes.

  19. 10 Jun 2003 at 12:20

    Hello, I have tried using your script and I have checked over my code for syntax and it appears to be the exact same as yours. When I test it, it appears to not upload anything and skips right to the else statement "FAILED". It appears as username is not being passed properly to the php page. Also  copy($userfile, "kuva.gif"); that line what should the value in " " be. THe path where the uploaded file is going to be stored? I dont understand how someone could have a .gif. Plz help
    HTML PAGE


    <html>
    <form action="myupload.php" method=post enctype="multipart/form-data">
    submit this file: <input type=file name="userfile"><br>
     <input type=submit>
     <br>
    </form>
    </html>


    PHP


    <html>
    <?php
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>

  20. 09 Jun 2003 at 20:05
  21. 09 Jun 2003 at 20:02

    Perhaps it's more likely that the data getting put into $userfile is not working properly leaving it empty.


    I'd also rather have "<?php" instead of just "<?" when starting to write php code.


    <?php


    if(!empty($userfile))
    {
     copy($userfile, "kuva.gif");
     echo("file uploaded");
    }
    else
    {
     echo("FAILED!");
    }


    ?>

  22. 09 Jun 2003 at 19:59

    "./phpIV1As" sounds like a directory name, probably the directory of your php folder or where your files are stored on your webspace, which may be inaccessable to you.


    With your "c:pippo.ttt" file name, have you tried putting in the "\" backspace after C: in this text? If you don't, which is what you have done here, then PHP will look for the pippo.ttt file in the present working directory of the C: Drive, which is unnecessarily the root folder of the C: drive.

  23. 09 Jun 2003 at 19:54

    Quote:
    ...I am new php programmer but when I test your upload php. I got a message


    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upl



    bonk on head


    Perhaps you don't have a www/phptest/ folder


    The text "'/home/www/phptest/test.txt" was just an example, you can make it anything you want, as long as its a valid place in your webspace.

  24. 08 Apr 2003 at 05:39

    I have had this problem before.  Your code appears to be ok so it seems odd that it is not working.  Try using the same code but without the braces {} - this often helps me in PHP when for some reason the second branch of the conditional is never reached.




    Quote:
    [1]Posted by meissi on 26 Feb 2003 06:32 AM[/1]
    Hi!


    I used this code for my upload form and worked ok...there's just something i can't get to work.
    I placed a check for the "empty input field" and I can't get it to work.


    <html>
    <?
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>


    No matter if I turn the code "upside down", it only does the "if" part..can you help me?


    -meissi-

  25. 06 Apr 2003 at 13:25
    I have a little prob
    I am new php programmer but when I test your upload php. I got a message

    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
    file up...


    You need to make sure the directory you are uploading to has write permissions. If it doesn't you will get that error!
  26. 24 Mar 2003 at 23:42

    That's so easy in my self server, but I am using free hosting like iwebland.com.
    May I change temporary directory? no I think so.
    I tried do a copy inside my root, and I discover that I haven't permision to do that.
    I don't know java.
    I can't use ftp because I can't give the access to my customer, but he need put images in the database.
    May redirect the upload trought "<input type=file..." to the directory in my root?

  27. 26 Feb 2003 at 06:32

    Hi!


    I used this code for my upload form and worked ok...there's just something i can't get to work.
    I placed a check for the "empty input field" and I can't get it to work.


    <html>
    <?
    if(!empty($userfile)) {
    copy($userfile, "kuva.gif");
    echo("file uploaded");
    }
    else {
    echo("FAILED!");
    }
    ?>
    </html>


    No matter if I turn the code "upside down", it only does the "if" part..can you help me?


    -meissi-

  28. 18 Feb 2003 at 02:59

    are you talking to me?

  29. 06 Feb 2003 at 02:13

    Hi


    Run your application from the "public" folder under your server's root directory.


    S.S

  30. 05 Feb 2003 at 03:22

    Try on a php supported server (i.e. sign up on www.free.fr)  
    bye

  31. 04 Feb 2003 at 21:20

    I am new php programmer but when I test your upload php. I got a message


    Warning: Unable to create '/home/www/phptest/test.txt': Permission denied in /home/www/phptest/upload.php on line 6
    file uploaded


    instead. what should I do to solve this prob?

  32. 03 Feb 2003 at 04:25
    Hi,
    I'm a new inexpert php programmer.

    I've tested your script upload.html and upload.php and it is all  ok.

    Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.


    test 1)
    I try to substitute in upload.htm the line:
    :---------
      submit this file: <input type=file name="userfile"><br>
    :---------
    with this line :
    :---------
      submit this file: <input type="hidden" name="userfile" value="c:pippo.ttt"><br>
    :---------
    ... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
    :---------
    Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 12
    :---------



    --------------------------------------------------------------
    test 2)
    to debug I add a line in upload.php
    :---------
       echo "$userfile";
       echo "<br>-xxx-<br>";
    :---------
    ... but the server replay:

    :---------
    ./phpIV1Asq
    -xxx-

    Warning: Unable to open 'c:pippo.ttt' for reading: No such file or directory in your script on line 9
    :---------


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


    The question:

    Why the string c:pippo.ttt is echoed as ./phpIV1As ?

    Thank you for your help.

    Best regards
  33. 03 Feb 2003 at 04:24
    Hi,
    I'm a new inexpert php programmer.

    I've tested your script upload.html and upload.php and it is all  ok.

    Now, I like modify script to upload a specified file (i.e c:\pippo.ttt) from pc to server, without insert manually the file name in the input box, but I have some problems.


    test 1)
    I try to substitute in upload.htm the line:
    :---------
      submit this file: <input type=file name="userfile"><br>
    :---------
    with this line :
    :---------
      submit this file: <input type="hidden" name="userfile" value="cippo.ttt"><br>
    :---------
    ... but the server ( http://infrarossi.free.fr/test/upload5.htm ) replay :
    :---------
    Warning: Unable to open 'cippo.ttt' for reading: No such file or directory in your script on line 12
    :---------



    --------------------------------------------------------------
    test 2)
    to debug I add a line in upload.php
    :---------
       echo "$userfile";
       echo "<br>-xxx-<br>";
    :---------
    ... but the server replay:

    :---------
    ./phpIV1Asq
    -xxx-

    Warning: Unable to open 'cippo.ttt' for reading: No such file or directory in your script on line 9
    :---------


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


    The question:

    Why the string cippo.ttt is echoed as ./phpIV1As ?

    Thank you for your help.

    Best regards
  34. 12 Sep 2002 at 05:48

    when a partner wrote a simple script to receive uploaded files, the owner of the file was set to nobody, which is the apache webserver, I believe.
    In this case, we can't access or manipulate the uploaded files via ftp or the shell.  Chmod doesn't seem to work.


    Are we doing something wrong or is this always the case?


    Thanks,
    Collin

  35. 01 Jan 1999 at 00:00

    This thread is for discussions of Uploading files in PHP.

Leave a comment

Sign in or Join us (it's free).

S.S. Ahmed S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic we...

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Apr 17

    WebTech Conference 2010 - Bulgaria

    Veliko Turnovo, Bulgaria

    6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business

We'd love to hear what you think! Submit ideas or give us feedback