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条)

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题