dseomy1964 2011-02-07 18:54
浏览 43
已采纳

Drupal 6节点创建PHP脚本,文件上传不起作用

I've got this PHP script I'm working on to import pay-stubs into Drupal. It's doing everything the way I want except the script is not attaching the uploaded PDF file to the node.

A few notes; Drupal's filesystem is set to private, not sure if this makes a difference or not. Second, the pdf files are already in the correct location 'paystubs/[uid]/paystub_1.pdf' so I think my problem is that the file is not being associated to the node correctly.

Here is the code

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
";
    } 
  }

  //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); 
}

The node is created and the title and body of the node have the right values. When I look at the node using Devel module I can see that the 'field_paystub_upload' array is null. So for some reason its doing everything right except attaching the file to the node and that is what I've been banging my head on for days. Best response gets on free internet?

  • 写回答

2条回答 默认 最新

  • duanchuang1935 2011-02-07 20:05
    关注

    Drupal's file.inc file_save_upload uses $_FILES, which is a global, magically set by PHP. Drupal expects an uploaded file, not a file that exists locally.

    You best just call a custom file-saver method, to process local files. Make sure its path up in the files database-table too. file_save_upload will be valuable for creating such a helper method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!