如何在使用“file_scan_directory”后将文件对象保存到字段

发布于 2024-12-12 05:25:08 字数 410 浏览 0 评论 0原文

我尝试使用 file_scan_directory 扫描一些文件并获取一些本地路径,我希望它们成为一个对象并保存到数据库中,所以我需要 file_save 来这样做。 file_save 将文件对象作为参数,我怎样才能成功这个过程?

我尝试过file_save_upload,API文档提到参数“$source --- 指定要保存的上传文件的文件路径或URI的字符串。”,但是它似乎不读取路径并且总是返回null。

我还尝试自定义创建一个文件对象供 file_save 运行,它确实有效,但不认为它是正确的 drupal 方式,是否有任何解决方案:

file_scan_directory-> ???? ->文件保存-> field_attach_update(* 文件保存后用新的 fid 更新字段)

请帮忙,非常感谢!

I try to use file_scan_directory to scan some files and get some local path, and I want them to become an object and save into db, so I need file_save to do so.
file_save take an file object as parameter, and how can I suppose to success this procedure?

I have try file_save_upload, API doc mentioned the parameter "$source --- A string specifying the filepath or URI of the uploaded file to save.", however it seems not to read the path and always return null.

I also try to custom create a file object for file_save to run, it does work but don't think it's in correct drupal way, would there be any solution for:

file_scan_directory-> ???? -> file_save -> field_attach_update (* update a field with new fid once the file is save)

please help for this, thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2024-12-19 05:25:08

此代码片段将扫描一些 jpg 文件,保存到数据库中,并将文件附加到字段。

function test_form(){
  $node = node_load('61');
  unset($node->field_image[$node->language]);

  $files = file_scan_directory('public://testimport', '/^.*\.(jpg|JPG)$/');

  //dpm($files);

  //http://drupal.org/node/889058
  foreach($files as $fileobj){

    $query = new EntityFieldQuery;
    $result = $query
      ->entityCondition('entity_type', 'file')
      ->propertyCondition('uri', $fileobj->uri)
      //$query->propertyCondition('uri', 'public://%', 'LIKE');
      ->execute();

    if(isset($result['file'])){
      dpm($result['file']);
      $fid = reset($result['file'])->fid;
      $fileobj = file_load($fid);
    }else{
      $fileobj->filemine = file_get_mimetype($fileobj->uri);
      $fileobj = file_save($fileobj);
    }

    $node->field_image[$node->language][] = array(
      'fid' => $fileobj->fid,
      //'alt' => $node->title,
      //'title' => $node->title,
      'uid' => '1',
      'filename' => $fileobj->filename,
      'uri' => $fileobj->uri,
      'filemime' => $fileobj->filemime,
      'filesize'=> $fileobj->filesize,
      'status' => '1',
    );

  }

  if($node = node_submit($node)) { // Prepare node for saving
    node_save($node);
    echo "Node with nid " . $node->nid . " updated!\n";
  }
}

This snippet will scan some jpg files, save into db, and attach the files to a field.

function test_form(){
  $node = node_load('61');
  unset($node->field_image[$node->language]);

  $files = file_scan_directory('public://testimport', '/^.*\.(jpg|JPG)$/');

  //dpm($files);

  //http://drupal.org/node/889058
  foreach($files as $fileobj){

    $query = new EntityFieldQuery;
    $result = $query
      ->entityCondition('entity_type', 'file')
      ->propertyCondition('uri', $fileobj->uri)
      //$query->propertyCondition('uri', 'public://%', 'LIKE');
      ->execute();

    if(isset($result['file'])){
      dpm($result['file']);
      $fid = reset($result['file'])->fid;
      $fileobj = file_load($fid);
    }else{
      $fileobj->filemine = file_get_mimetype($fileobj->uri);
      $fileobj = file_save($fileobj);
    }

    $node->field_image[$node->language][] = array(
      'fid' => $fileobj->fid,
      //'alt' => $node->title,
      //'title' => $node->title,
      'uid' => '1',
      'filename' => $fileobj->filename,
      'uri' => $fileobj->uri,
      'filemime' => $fileobj->filemime,
      'filesize'=> $fileobj->filesize,
      'status' => '1',
    );

  }

  if($node = node_submit($node)) { // Prepare node for saving
    node_save($node);
    echo "Node with nid " . $node->nid . " updated!\n";
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文