2012-10-15 63 views
0

我想將臨時目錄中創建的文件保存到drupal中。但file_save請求一個文件對象,但我只是真正的路徑。Drupal:保存文件系統中的文件

$imageId =file_save('/tmp/proj/media/cover.jpg']); 
+0

('/ tmp/proj/media/cover.jpg']');爲什麼那裏。如果你做一個簡單的谷歌搜索,你會知道這個爭論是'stdClass'的一個對象http://api.drupal.org/api/drupal/includes!file.inc/function/file_save/7 – wesside

回答

0

file_save(stdClass $ file)保存文件對象。您正在嘗試下載文件。

你可以做

$file = '/tmp/proj/media/cover.jpg'; 
// Get the file size 
$details = stat($file); 
$filesize = $details['size']; 

// Get the path to your Drupal site's files directory 
$dest = file_directory_path(); 

// Copy the file to the Drupal files directory 
if(!file_copy($file, $dest)) { 
    echo "Failed to move file: $file.\n"; 
    return; 
} else { 
    // file_move might change the name of the file 
    $name = basename($file); 
} 

// Build the file object 
$file_obj = new stdClass(); 
$file_obj->filename = $name; 
$file_obj->filepath = $file; 
$file_obj->filemime = file_get_mimetype($name); 
$file_obj->filesize = $filesize; 
$file_obj->filesource = $name; 
// You can change this to the UID you want 
$file_obj->uid = 1; 
$file_obj->status = FILE_STATUS_TEMPORARY; 
$file_obj->timestamp = time(); 
$file_obj->list = 1; 
$file_obj->new = true; 

// Save file to files table 
drupal_write_record('files', $file_obj); 

我希望這會幫助你。

相關問題