[FX.php List] Upload script in PHP?

Dale Bengston dbengston at preservationstudio.com
Mon Mar 13 10:11:03 MST 2006


Hi Bob,

I took mine right from the php.net's examples about uploading files:

<http://us2.php.net/manual/en/features.file-upload.php>

Here is their upload HTML form:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
     <!-- MAX_FILE_SIZE must precede the file input field -->
     <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
     <!-- Name of input element determines name in $_FILES array -->
     Send this file: <input name="userfile" type="file" />
     <input type="submit" value="Send File" />
</form>

The three comment lines identify the big differences in this form and  
more traditional html forms. Note that the MAX_FILE_SIZE value is  
largely ignored by the browser, so you'll need to evaluate that after  
the file is uploaded (file size is part of the $_FILES array... see  
immediately below).

Once uploaded, PHP stores info about the file in the $_FILES array.  
You can find the details of the elements of $_FILES on the page  
linked above, but the elements for the uploaded 'userfile' above are:

$_FILES['userfile']['name'] The original name of the uploaded 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". This mime  
type is however not checked on the PHP side and therefore don't take  
its value for granted.

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

$_FILES['userfile']['error'] The error code associated with this file  
upload. This element was added in PHP 4.2.0

The uploaded file lands in a temp directory, and you use php's  
move_uploaded_file() to relocate it to your appropriate web  
directory. You can also rename it and use the values in $_FILES check  
for different file types and file sizes (although the mime type thing  
isn't bulletproof).

Things to watch out for: file and folder permissions on the final  
resting place for your uploads, since the www user has pretty limited  
access. Also, your php.ini file probably has a upload_max_filesize  
set to 2MB. If the PDFs being uploade are larger than 2MB, you'll  
need to up this value. If you're changing upload_max_filesize, you'll  
need to look at post_max_size too.

Hope this helps,
Dale


On Mar 13, 2006, at 9:59 AM, Bob Patin wrote:

> Does anyone have any code for writing a simple upload script in  
> PHP? I tried some code that I found online, but have been unable to  
> get it to work.
>
> I have a client who needs to put a form on their site so that  
> clients can upload PDF files directly into their web folder on the  
> web server.
>
> Any help would be greatly appreciated.
>
> Thanks,
>
> Bob Patin
> Longterm Solutions
> bob at longtermsolutions.com
> 615-333-6858
> http://www.longtermsolutions.com
>
>   CONTACT US VIA SKYPE:
>      USERNAME: longtermsolutions
>
>   CONTACT US VIA INSTANT MESSAGING:
>      AIM or iChat: longterm1954
>      Yahoo: longterm_solutions
>      MSN: bob at patin.com
>      ICQ: 159333060
>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list



More information about the FX.php_List mailing list