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"
    <input type="hidden" name="cmd" value="upload" />
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <p>Send these files:</p>
        <li><input name="userfile1" type="file" /></li>
        <li><input name="userfile2" type="file" /></li>
        <li><input name="userfile3" type="file" /></li>
    <p><input type="submit" value="Send File" /></p>


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);
        $tpl = load_tpl ('test.tpl');
        $txt['main_body'] = quick_tpl ($tpl, $txt);
        flush_tpl ();

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