print

upload_file ()

You are here: C97net5.05 File Functions

Safer way to receive file uploads

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:

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 at 07:00am
Last Updated: Jan-01-1970 at 07:00am

There is no comment. Why not be the first?

read more More Comments/Post Your Own

Account

Search

Tags

acp add all also any create default display don download easy echo etc features field file files folder form function includes information init kemana link list may module name need number own page php please qengine read require script set site skin time tpl url use user using version welcome