Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 80,001 times

Contents

Related Categories

Uploading files in PHP - The Code

S.S. Ahmed

The Code

upload.htm

<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>

myupload.php

<html>
<?
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES.
if(!empty($_FILES["userfile"])) {
    $uploaddir = "/upload/" // set this to wherever
    //copy the file to some permanent location
    if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $_FILES["userfile"]["name"])) {
        echo("file uploaded");
    } else {
        echo ("error!");
    }
}
?>
</html>

myupload.php handles the uploaded file. In PHP, posted files can be accessed via the $_FILES array. This array in turn has an array with the following entries:

  • $_FILES["userfile"]["name"]
    The original name of the file on the client machine.
  • $_FILES['userfile']['type']
    The mime type of the file, if the browser provided this information. An example would be "image/gif".
  • $_FILES['userfile']['size']
    The size, in bytes, of the uploaded file.
  • $_FILES['userfile']['tmp_name']
    The temporary filename of the file in which the uploaded file was stored on the server.

When a posted file is retrieved, PHP automatically saves the file in a temporary directory. We copy the file to a permanent location using the move_uploaded_file() function, which simply takes the temporary file name (accessed by 'tmp_name') and the location to move it to.

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 web sites. He has been working with SharePoint for the last 3-4 years. He develops customized SharePoint solutions. Ahmed likes to hop into other tools as well. Ahmed has used Project Server, InfoPath and BizTalk. Ahmed enjoys travelling and has been to many parts of the world. Web: www.walisystems.com Blog: www.sharepointblogs.com/ssa

Comments

  • Re: [2892] Uploading files in PHP

    Posted by CarlBe on 28 Sep 2006

    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 erro...

  • can the form change the filename as it is uploaded

    Posted by tpwalker1980 on 12 Jun 2005

    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.jp...

  • permission change of uploaded file

    Posted by lenilachica on 04 Feb 2005

    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.

    it...

  • change/set permissions on upload

    Posted by mike00 on 29 Jan 2005

    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 pr...

  • root address

    Posted by lenilachica on 28 Jan 2005

    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 ...