【问题标题】:Drupal 6 Node Creation PHP Script with File upload not workingDrupal 6节点创建PHP脚本文件上传不起作用
【发布时间】:2013-05-08 10:59:12
【问题描述】:

我正在编写这个 PHP 脚本,用于将工资存根导入 Drupal。它按照我想要的方式做所有事情,除了脚本没有将上传的 PDF 文件附加到节点。

一些笔记; Drupal 的文件系统设置为私有,不确定这是否有所不同。其次,pdf文件已经在正确的位置'paystubs/[uid]/paystub_1.pdf'所以我认为我的问题是文件没有正确关联到节点。

这里是代码

function create_drupal_node($employeeID, $employeeDate, $drupalUid, $file2) {
  $sourcePDF = "/var/www/html/mgldev.************.com/burst_pdfs/pdfs/" . $file2;
  $destinationPDF = '/paystubs/' . $drupalUid . '/' . $file2;
  $destination = '/paystubs/' . $drupalUid . '/';

  if (!file_check_directory($destination, TRUE)){
    echo "Failed to check dir, does it exist?";
    mkdir($destination);    
    echo "trying to drupal mkdir...";
  }

  // Copy the file to the Drupal files directory 
  if (file_exists($sourcePDF)) {
    if(!rename($sourcePDF, $destinationPDF)) {
        echo "Failed to move file\n";
    } 
  }

  //Create node and attach file uplaod
  $file_drupal_path = "paystubs/" . $drupalUid . "/" . $file2;
  $mime = 'pdf/application';

  $file = new stdClass();
  $file->filename = $file2;
  $file->filepath = $file_drupal_path;
  $file->filemime = $mime;
  $file->filesize = filesize($file_drupal_path);

  $file->uid = $drupalUid;
  $file->status = FILE_STATUS_PERMANENT;
  $file->timestamp = time();
  drupal_write_record('files', $file);

  $node = new StdClass();
  $node->type = 'paystub';
  $node->body = $employeeID;
  $node->title = $employeeDate;
  $node->field_paystub_upload = array(
    array(
      'fid' => $file->fid,
      'title' => $file2,
      'filename' => $file->filename,
      'filepath' => $file->filepath,
      'filesize' => $file->filesize,
      'mimetype' => $mime,
      'data' => array(
        'description' => $file2,
      ),
      'list' => 1,
    ),
  );
  $node->uid = $drupalUid;
  $node->status = 1;
  $node->active = 1;
  $node->promote = 1;
  node_save($node); 
}

节点已创建并且节点的标题和正文具有正确的值。当我使用 Devel 模块查看节点时,我可以看到“field_paystub_upload”数组为空。因此,出于某种原因,除了将文件附加到节点之外,它所做的一切都是正确的,这就是我几天来一直在努力的事情。最好的反应是免费上网?

【问题讨论】:

    标签: php drupal drupal-6 file upload


    【解决方案1】:

    Drupal 的 file.inc file_save_upload 使用 $_FILES,这是一个 global, magically set by PHP。 Drupal 需要上传的文件,而不是本地存在的文件。

    您最好只调用自定义文件保护程序方法来处理本地文件。确保它的路径也在files 数据库表中。 file_save_upload 对于创建这样的辅助方法很有价值。

    【讨论】:

    • gah,您能否详细说明/让我开始调用 file_save_upload。我试图调用 file_save_upload 文件在原始代码中被重命名但它不能正常工作。
    • 好吧,我找到了这个链接,似乎它可能接近我的解决方案,但我仍然对它应该如何插入我的节点保存功能感到困惑。 stackoverflow.com/questions/1796948/…
    • 对不起,我在您提出问题后重新阅读了我的答案,发现我没有明确指出 File_save_upload 本身无法使用,您应该自己制作 my_file_save_upload( ),而不是使用 $_FILES,而是使用磁盘上的文件。
    【解决方案2】:

    非常感谢berkes 帮助我解决了这个问题。事实证明,由于文件已经在 drupal 网络服务器上并且没有上传到 PHP $_FILES 全局变量,我无法以编程方式正确上传文件。

    这导致我尝试过的所有其他方式都失败了。我尝试使用 Drupals 默认上传模块,也尝试使用 CCK 的字段模块,但都无法正常工作。感谢 berkes 的建议,我找到了 CCK 的文件字段小部件附带的一个功能,用于保存已经在服务器上的上传文件。希望这对其他人有帮助。

    This is the function我发现可以保存一个已经在网络服务器上的文件。

    这是我用来创建节点并在调用 field_file_save_file 后附加文件的工作代码。

    function create_drupal_node($employeeID, $employeeDate, $drupalUid, $file2){
        $file_remove_html_extention = substr($file2, 0, -7);
        $file_pdf = $file_remove_html_extention . '.pdf';
    
    
    $node = new stdClass();
    $node->type = 'paystub';
    $node->status = 1;
    $node->uid = $drupalUid;
    $node->title = $employeeDate . ' - eStub';
    $node->body = $employeeID;
    $node->created = time();
    $node->changed = $node->created;
    $node->promote = 1;
    $node->sticky = 0;
    $node->format = 1;
    $node->language = 'en';
    
    $file = '/var/www/html/mgldev.foobar.com/burst_pdfs/pdfs/' . $file_pdf;
    
    // Get the path to your Drupal site's files directory 
    $dest_folder = '/paystubs/' . $drupalUid;
    $dest = 'paystubs/' . $drupalUid . '/' . $file_pdf;
    
    if (!file_check_directory($dest_folder, TRUE)){
    mkdir($dest_folder);    
    

    }

    // Load the CCK field $field = content_fields('field_paystub_upload', 'paystub'); // Load the appropriate validators $validators = array_merge(filefield_widget_upload_validators($field)); // Create the file object $file = field_file_save_file($file, $validators, $dest_folder); // Apply the file to the field, this sets the first file only, could be looped // if there were more files $node->field_paystub_upload = array(0 => $file); // The file has been copied in the appropriate directory, so it can be // removed from the import directory unlink($file); // change file status to permanent file_set_status($file,1); node_save($node); } </pre></code>

    再次感谢伯克斯

    【讨论】:

    • 嗨,我能问一下您是如何调用该函数的吗?您是否创建了一个模块来处理这个问题,或者这是一个在某些时候调用 Drupal Bootstrap 的自定义脚本? (我正在尝试做类似的事情,找出来会很有用)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多