Simple PHP File Upload


Simple PHP File Upload



About

This is a very simple, yet fully functional PHP script/function to upload files to your website. Usage and integration is simple, file uploads can be as simple as adding one line to your PHP form processing scripts. All you do is call a function. UPDATED: Now supports uploading multiple files.

Requirements

  • Tested on PHP 5.2.x
  • Tested on Apache 2.x
  • You must have write permissions to the upload directory

How it Works

After uploading a file to the web server, this function will move the file referenced by "$reference" From the PHP temporary file name & path to the "$target" path With the file name of basename($_FILES[reference][name]) or $fileName is it is provided. Note: With multiple files upload, the $fileName parameter is ignored, the files names will be the same as the original.

mixed <strong>uploadFile</strong> (string $target, string $reference [, string $fileName])
  • $target: The directory name under the current directory in the form of "name" or "subdir/name"
  • $refence: The form's upload field name
  • $fileName: Optional value to specify the desired name of the uploaded file, uses the original file name if not given. Ignored with multiple file uploads

Returns the full path of the uploaded file on success
Returns FALSE on failure
With multiple file uploads, it returns: array of "array('file', 'result')"

WARNING: This function will always replace files.
ADDITIONAL: This function will attempt to create subdirectores if they do not exists

How to use (Example)

Copy "fileFunctions.php" to your web server and include the "fileFunctions.php" script at the top of your form processor script:

<?phpinclude('fileFunctions.php');?>

Assuming you have the following form:

<form name="form1" enctype="multipart/form-data" method="post">
    Upload File: <input type="file" name="upload" id="upload" />
    <input type="submit" name="submit" value="Submit" />
</form>

Just add the following line to your form processing script:

<?php$result uploadFile('files''upload');?>

If you'd like to enable upload of multiple files, just add "[]" to the end of the file field form element name. So, above, you would simply create multiple file input fields and name them all name="upload[]". For a complete example, see the file named "uploadSampleCode.php" that is included in the archive.

Where to get it

Download it at: http://www.unibia.com/unibia/dev/downloads/simplePHPUpload.tar

Comments

Tue, 04/23/2013 - 07:10

as file uploader script needs a password, here is a sample one - http://stackoverflow.com/questions/9207379/simple-file-upload-script