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

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合