Php Drupal:从文件系统保存文件
我想将我在temp目录中创建的文件保存到drupal中。但是file_save请求一个file对象,但我只有真正的路径Php Drupal:从文件系统保存文件,php,drupal,Php,Drupal,我想将我在temp目录中创建的文件保存到drupal中。但是file_save请求一个file对象,但我只有真正的路径 $imageId =file_save('/tmp/proj/media/cover.jpg']); 我认为您正在寻找,或者可能是,而不是file_save()。file_save(stdClass$file)保存一个file对象。您正在尝试下载一个文件 你可以随心所欲 $file = '/tmp/proj/media/cover.jpg'; // Get the file
$imageId =file_save('/tmp/proj/media/cover.jpg']);
我认为您正在寻找,或者可能是,而不是file_save()。file_save(stdClass$file)保存一个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);
我希望这能对你有所帮助。('/tmp/proj/media/cover.jpg'
]
);为什么会这样。如果你做一个简单的谷歌搜索,你就会知道这个论点是stdClass