Location: PHPKode > projects > phlyMail Lite > phlymail/handlers/files/upload.php
<?php
/**
 * Add a file to the personal storage of a user
 * @package phlyMail Nahariya 4.0+ Default Branch
 * @subpackage Handler Files
 * @copyright 2005-2010 phlyLabs, Berlin (http://phlylabs.de)
 * @version 4.1.2 2010-11-08
 */
// Only valid within phlyMail
if (!defined('_IN_PHM_')) die();

// We got to have the upload_tmp_dir set to something useful
if (!ini_get('upload_tmp_dir')) ini_set('upload_tmp_dir', $_PM_['path']['storage'].'/'.$_SESSION['phM_uid'].'/files/.tmp');
/**
 * Hints about the image types, getimagesize() may return
 * 1 = 'GIF' (supported)
 * 2 = 'JPG' (supported)
 * 3 = 'PNG' (supported)
 * 4 = 'SWF'
 * 5 = 'PSD'
 * 6 = 'BMP'
 * 7 = 'TIFF, intel byte order'
 * 8 = 'TIFF, motorola byte order'
 * 9 = 'JPC'
 * 10 = 'JP2'
 * 11 = 'JPX'
 * 12 = 'JB2'
 * 13 = 'SWC'
 * 14 = 'IFF'
 * 15 = 'WBMP'
 * 16 = 'XBM'
 */
require_once($_PM_['path']['handler'].'/files/fs.php');
$FS = new files_storage($_SESSION['phM_uid']);
require_once($_PM_['path']['lib'].'/phm_mime_handler.php');
$MIME = new phm_mime_handler($_PM_['path']['conf'].'/mime.map.wpop');

$passthru = give_passthrough(1);
$defaultFolder = (isset($_SESSION['files_workfolder']) && $_SESSION['files_workfolder']) ? $_SESSION['files_workfolder'] : false;
$destfolder = (isset($_REQUEST['folder'])) ? intval($_REQUEST['folder']) : $defaultFolder;

if (isset($_FILES) && isset($_FILES['file']) && !empty($_FILES['file'])) {
    foreach ($_FILES['file']['name'] as $k => $v) {
        if (!is_uploaded_file($_FILES['file']['tmp_name'][$k])) continue; // Skip things, being no upload
        switch ($_FILES['file']['error'][$k]) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_INI_SIZE:
            throw new Exception('The uploaded file exceeds the upload_max_filesize directive ('.ini_get('upload_max_filesize').') in php.ini.');
            break;
        case UPLOAD_ERR_FORM_SIZE:
            throw new Exception('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
            break;
        case UPLOAD_ERR_PARTIAL:
            throw new Exception('The uploaded file was only partially uploaded.');
            break;
        case UPLOAD_ERR_NO_FILE:
            throw new Exception('No file was uploaded.');
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            throw new Exception('Missing a temporary folder.');
            break;
        case UPLOAD_ERR_CANT_WRITE:
            throw new Exception('Failed to write file to disk');
            break;
        default:
            throw new Exception('Unknown File Error');
        }
        $uploadname = uniqid(time().'.', true);
        list ($type) = $MIME->get_type_from_name($_FILES['file']['name'][$k], false);
        if (!$type) $type = ($_FILES['file']['type'][$k]) ? $_FILES['file']['type'][$k] : 'application/octet-stream';
        $bicon = $MIME->get_icon_from_type($_PM_['path']['frontend'].'/filetypes/64', $type, array('png', 'jpg', 'jpeg', 'gif'));
        $destinfo = $FS->get_folder_info($destfolder);
        $thumb = basics::create_thumbnail($_FILES['file']['tmp_name'][$k], 190, 190);
        move_uploaded_file
                ($_FILES['file']['tmp_name'][$k]
                ,$_PM_['path']['storage'].'/'.$_SESSION['phM_uid'].'/files/'.$destinfo['folder_path'].'/'.$uploadname
                );
        $friendlyname = basename(phm_stripslashes($_FILES['file']['name'][$k]));
        $exists = $FS->item_exists($friendlyname, $destfolder);
        if ($exists) {
            $expos = strrpos($friendlyname, '.');
            // Yes, we mean "Not found" AND "on position 0"!
            if (!$expos) {
                $basename = $friendlyname;
                $ext = '';
            } else {
                $basename = substr($friendlyname, 0, $expos);
                $ext = substr($friendlyname, $expos);
            }
            $adder = 1;
            while (true) {
                $match = $FS->item_exists($basename.' ('.$adder.')'.$ext, $destfolder);
                if (!$match) {
                    $friendlyname = $basename.' ('.$adder.')'.$ext;
                    break;
                }
                ++$adder;
            }
        }
        $id = $FS->file_item(array
                ('folder_id' => $destfolder
                ,'filed' => true
                ,'filename' => $uploadname
                ,'friendlyname' => $friendlyname
                ,'type' => $type
                ,'size' => $_FILES['file']['size'][$k]
                ,'img_w' => $ii[0]
                ,'img_h' => $ii[1]
                ));
        if (false !== $thumb) {
            $DB->thumb_add('files', $id, 'fdetail', $thumb['mime'], $thumb['size'], $thumb['width'], $thumb['height'], $thumb['stream']);
        }
    }
    header('Location: '.PHP_SELF.'?load=upload&handler=files&'.$passthru);
    exit;
}

$tpl = new fxl_cached_template($_PM_['path']['frontend'].'/templates/files.upload.tpl', $_PM_['path']['tplcache'].'files.upload.tpl');
$FS->init_folders();
$t_inb = $tpl->get_block('destfolder');
foreach ($FS->read_folders_flat() as $id => $data) {
    $lvl_space = ($data['level'] > 0) ? str_repeat('&nbsp;', $data['level'] * 2) : '';
    $t_inb->assign(array
            ('id' => (!$data['has_items']) ? '" style="color:darkgray;" disabled="disabled' : $id.($id == $defaultFolder ? '" selected="selected' : '')
            ,'name' => $lvl_space . phm_entities($data['foldername'])
            ));
    $tpl->assign('destfolder', $t_inb);
    $t_inb->clear();
}
$tpl->assign(array
        ('action' => htmlspecialchars(PHP_SELF.'?load=upload&handler=files&'.$passthru, ENT_COMPAT, 'utf-8')
        ,'leg_choosefile' => $WP_msg['LegRemoteFile']
        ,'leg_localfolder' => $WP_msg['LegLocalFolder']
        ,'about_choosefile' => $WP_msg['UploadSelectFiles']
        ,'msg_upload' => $WP_msg['Upload']
        ,'msg_filetofolder' => $WP_msg['AboutLocalFolder']
        ));
if (false !== ($maxfilesize = ini_get('upload_max_filesize')) && $maxfilesize) {
    $tpl->fill_block('maxfilesize', 'maxfilesize', wash_size_field($maxfilesize));
    $tpl->assign('msg_maxfilesize', $WP_msg['MaxFilesize'].': '.size_format(wash_size_field($maxfilesize), 0, 0, 0));
}
?>
Return current item: phlyMail Lite