Beispiel #1
0
function fn_copy_product_files($file_id, $file, $product_id, $var_prefix = 'file')
{
    $revisions = Registry::get('revisions');
    if (!empty($revisions['objects']['product']['tables'])) {
        $revision = true;
    } else {
        $revision = false;
    }
    if ($revision) {
        $filename = $file['name'];
        $i = 1;
        while (is_file(substr(DIR_DOWNLOADS, 0, -1) . ($revision ? '_rev' : '') . '/' . $product_id . '/' . $filename)) {
            $filename = substr_replace($file['name'], sprintf('%03d', $i) . '.', strrpos($file['name'], '.'), 1);
            $i++;
        }
    } else {
        $filename = $file['name'];
    }
    $_data = array();
    $_data[$var_prefix . '_path'] = $filename;
    $_data[$var_prefix . '_size'] = $file['size'];
    list($new_file, $_data[$var_prefix . '_path']) = fn_generate_file_name(substr(DIR_DOWNLOADS, 0, -1) . ($revision ? '_rev' : '') . '/' . $product_id . '/', $_data[$var_prefix . '_path']);
    if (fn_copy($file['path'], $new_file) == false) {
        $_msg = fn_get_lang_var('cannot_write_file');
        $_msg = str_replace('[file]', $new_file, $_msg);
        fn_set_notification('E', fn_get_lang_var('error'), $_msg);
        return false;
    }
    db_query('UPDATE ?:product_files SET ?u WHERE file_id = ?i', $_data, $file_id);
    return true;
}
Beispiel #2
0
 function fn_exim_import_file($product_id, $filename, $path, $delete_files = 'N')
 {
     // Check if directory for storing files is exist
     if (!@is_dir(DIR_DOWNLOADS . $product_id) && fn_mkdir(DIR_DOWNLOADS . $product_id) == false) {
         $msg = str_replace('[directory]', DIR_DOWNLOADS . $product_id, fn_get_lang_var('text_cannot_create_directory'));
         fn_set_notification('E', fn_get_lang_var('error'), $msg);
         return false;
     }
     // Clean up the directory above if flag is set
     if ($delete_files == 'Y') {
         $file_ids = db_get_fields("SELECT file_id FROM ?:product_files WHERE product_id = ?i", $product_id);
         db_query("DELETE FROM ?:product_files WHERE product_id = ?i", $product_id);
         db_query("DELETE FROM ?:product_file_descriptions WHERE file_id IN (?n)", $file_ids);
         fn_rm(DIR_DOWNLOADS . $product_id, false);
     }
     // Check if we have several files
     $files = fn_explode(',', $filename);
     // Copy files
     foreach ($files as $file) {
         if (strpos($file, '#') !== false) {
             list($f, $pr) = fn_explode('#', $file);
         } else {
             $f = $file;
             $pr = '';
         }
         $file = fn_find_file($path, $f);
         if (!empty($file)) {
             $pr_res = false;
             list($_path, $_filename) = fn_generate_file_name(DIR_DOWNLOADS . $product_id . '/', basename($f));
             copy($file, $_path);
             if (!empty($pr)) {
                 $file = fn_find_file($path, $pr);
                 if (!empty($file)) {
                     list($_prpath, $_prfilename) = fn_generate_file_name(DIR_DOWNLOADS . $product_id . '/', basename($pr));
                     $pr_res = copy($file, $_prpath);
                 }
             }
             $_data = array('file_path' => basename($_filename), 'file_size' => filesize(DIR_DOWNLOADS . $product_id . '/' . basename($_filename)), 'preview_path' => !empty($pr_res) ? basename($_prfilename) : '', 'preview_size' => !empty($pr_res) ? filesize(DIR_DOWNLOADS . $product_id . '/' . basename($_prfilename)) : 0, 'product_id' => $product_id);
             $file_id = db_get_field("SELECT file_id FROM ?:product_files WHERE product_id = ?i AND file_path = ?s", $product_id, $_data['file_path']);
             if (!empty($file_id)) {
                 db_query("UPDATE ?:product_files SET ?u WHERE file_id = ?i", $_data, $file_id);
             } else {
                 $file_id = db_query("INSERT INTO ?:product_files ?e", $_data, $file_id);
                 $_data = array('file_id' => $file_id, 'file_name' => basename($_filename));
                 foreach ((array) Registry::get('languages') as $_data['lang_code'] => $v) {
                     db_query("INSERT INTO ?:product_file_descriptions ?e", $_data);
                 }
             }
         }
     }
 }