function asset_file_insert in Asset 5.2
Same name and namespace in other branches
- 6 asset.types.inc \asset_file_insert()
Implementation of hook_asset_type('insert') for file asset type
Related topics
File
- ./
asset.types.inc, line 109 - This file is included by asset_asset_type() and includes all the file and directory specific functions
Code
function asset_file_insert(&$asset) {
$file = file_check_upload('upload');
$asset_path = variable_get('asset_file_directory_path', '');
$dest = $asset_path ? $asset_path . '/' . $file->filename : $file->filename;
if ($file = file_save_upload($file, $dest)) {
$file->fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
db_query("INSERT INTO {asset_files} (aid, fid, label) VALUES (%d, %d, '%s')", $asset->aid, $file->fid, 'default');
}
elseif ($asset->localfile) {
$path = variable_get('asset_ftp_dir', '') . $asset->localfile;
$asset_path = variable_get('asset_file_directory_path', '');
$dest = $asset_path ? $asset_path . '/' . basename($path) : basename($path);
// prepare the file object. based on file_check_upload
// Begin building file object.
$file = new stdClass();
$file->filename = trim(basename($path), '.');
$file->filepath = $path;
$file->filemime = mime_content_type($path);
$file->filesize = filesize($path);
// Rename potentially executable files, to help prevent exploits.
if (preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && substr($file->filename, -4) != '.txt') {
$file->filemime = 'text/plain';
$file->filepath .= '.txt';
$file->filename .= '.txt';
}
if (file_move($file, $dest)) {
$file->fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
db_query("INSERT INTO {asset_files} (aid, fid, label) VALUES (%d, %d, '%s')", $asset->aid, $file->fid, 'default');
}
}
$asset->file = db_fetch_array(db_query('SELECT f.* FROM {files} f INNER JOIN {asset_files} a ON f.fid=a.fid WHERE a.aid=%d', $asset->aid));
}