upload_file ()

function upload_file ($field, $target, $allow_overwrite = FALSE)

This is an easier & safer way to accept user files upload. Easier because it can accept multiple files upload easily, and safer because it denies unsafe file types (configurable in ACP > Configuration).

$field = file form field ID, can be array for multiple uploads.

$target = target folder OR folder with file name, can be array for multiple locations.

$allow_overwrite = if set true, will overwrite existing files. If set false, will rename the file if the same name exists.

This function will return an array of information, and uploaded file will be rename for safe file name.

The returned array:

  • success = false if file upload fails. If you are using multiple file uploads, even if one file is failed, it will returned false. True if all file uploaded.
  • demo_mode = false if qE is not in demo mode, true is it is.
  • size = total succesfully uploaded file size.
  • count = total succesfully uploaded file number.
  • Then there is an array for each uploaded file:
    • source = original file.
    • path = stored location & file name.
    • filename = stored file name only.
    • err = error message, can be: (empty) if no problem; 'NotWriteable' if the target location is not writeable. 'Disallow' if file type is not allowed.

Example 1. test.tpl

<h1>Welcome to test page!</h1>
<form name="myform" enctype="multipart/form-data" action="test.php"
method="POST">
    <input type="hidden" name="cmd" value="upload" />
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <p>Send these files:</p>
    <ul>
        <li><input name="userfile1" type="file" /></li>
        <li><input name="userfile2" type="file" /></li>
        <li><input name="userfile3" type="file" /></li>
    </ul>
    <p><input type="submit" value="Send File" /></p>
</form>

test.php

<?php
require './includes/user_init.php';

$cmd = post_param ('cmd');

switch ($cmd)
{
    case 'upload':
        $fields = array ('userfile1', 'userfile2', 'userfile3');
        $status = upload_file ($fields, './public/file/', true);
        print_r ($status);
    break;
   
    default:
        $tpl = load_tpl ('test.tpl');
        $txt['main_body'] = quick_tpl ($tpl, $txt);
        flush_tpl ();
    break;
}
?>

Example Output

(
[success] =>
[demo_mode] =>
[size] => 24510
[count] => 2
[0] => Array
(
[source] => a.php
[path] => ./public/file/a.php
[filename] => a.php
[err] => Disallow
)

[1] => Array
(
[source] => Winter.jpg
[path] => ./public/file/winter.jpg
[filename] => winter.jpg
[err] =>
)

[2] => Array
(
[source] => Blue-hills.jpg
[path] => ./public/file/blue_hills.jpg
[filename] => blue_hills.jpg
[err] =>
)
)

 


Posted On: Aug-25-2010 @ 07:00am
Last Updated: Jan-01-1970 @ 07:00am

There is no comment. Why not be the first?

More Comments/Post Your Own

Advertisement

Online Users

There are 115 users online.

Connect

Connect with us on Facebook

Newsletter

Subscribe to our newsletter for the latest updates and exciting promotions!