0

我想将我在 temp 目录中创建的文件保存到 drupal。但是 file_save 请求一个文件对象,但我只有真实的路径。

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

2 回答 2

1

我认为您正在寻找file_save_data 函数,或者可能是file_unmanaged_save_data,而不是 file_save()。

于 2012-10-15T14:07:13.983 回答
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);

我希望这能帮到您。

于 2012-10-18T06:16:42.387 回答