欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal7 从一个已经存在的文件 保存文件对象 Creating a file object from a file already in public:// (drupal 7)有大用

1 Answer 正确答案

I think the easiest way would be to create a file object manually, populate it with the file details, and save it using file_save():

global $user;$uri = 'public://path/to/file.txt';$filename = 'file.txt';$file = new stdClass;$file->uid = $user->uid;$file->filename = $filename;$file->uri = $uri;$file->filemime = 'text/plain';$file->filesize = filesize($uri);$file->status = 1;file_save($file);

After you've done that you'll probably also want to use the file_usage_add() function to declare a vested interest in the file, so that it doesn't get deleted by any other system process:

$type = 'some_type'; // A string used by your module to identify this type of file entry$id = 'some_id'; // An identifier used by your module to identify this particular file    file_usage_add($file, 'MYMODULE', $type, $id);
shareimprove this answeranswered Jun 16 '12 at 0:00Clive♦135k11224271

来自 https://drupal.stackexchange.com/questions/34244/creating-a-file-object-from-a-file-already-in-publi...

普通分类: