You are here

function _asset_file_insert_localfile in Asset 5.2

Same name and namespace in other branches
  1. 6 asset.types.inc \_asset_file_insert_localfile()

Related topics

File

./asset.types.inc, line 147
This file is included by asset_asset_type() and includes all the file and directory specific functions

Code

function _asset_file_insert_localfile($localfile) {
  $path = variable_get('asset_ftp_dir', '') . $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) VALUES (%d, %d)", $asset->aid, $file->fid);
    return $file;
  }
  else {
    return false;
  }
}