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?";
    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(
      '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;

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.

    打赏 评论
  • duanhuang3074 2011-02-09 15:30

    Big thanks to berkes for helping me solve this problem. Turns out that since the files were already on the drupal webserver and not being uploaded to PHP $_FILES global variable, I was unable to programmatically upload the file correctly.

    This was causing every other way I've tried to fail. I tried using Drupals defualt upload module and I also tried using CCK's fielfield module both were not working. Thanks to berkes suggestion I found a function that comes with CCK's filefield widget to save uploaded files that are already on the server. Hopefully this helps someone else.

    This is the function I found that can save a file thats already on the web-server.

    Here is the working code I used to create the node and attach the file after calling 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)){


    // 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>

    Thanks again berkes

    打赏 评论

相关推荐 更多相似问题