以编程方式将文件上传到Drupal,文件系统设置为私有

时间:2011-01-26 16:16:54

标签: drupal file upload

我正在尝试使用此脚本将文件上传到Drupal。我在将文件从A移动到B时遇到问题.Dupupal的文件系统设置为私有

节点创建正常,一切正常,除了某些原因我无法下载附件。它只是让我找不到页面。如果我在drupal中创建节点并上传文件,它可以正常工作。

我的代码必须在这里做错了。我正在关注this guide上传文件,但我似乎无法找到任何关于将Drupal的文件系统设置为私有的内容以及必须做些什么才能使其以编程方式正常工作。

任何人都有这方面的经验吗?

//prep file source and destination vars
$withoutExt = substr($file2, 0, -7);
$sourcePDF = "/var/www/html/pay.***.com/burst_pdfs/pdfs/" . $withoutExt . ".pdf";
$destinationPDF = '/var/paystubs/' . $drupalUid . '/' . $withoutExt . '.pdf';
$destination = '/var/paystubs/' . $drupalUid . '/';

//if the file does not exist, create it
if (!file_check_directory($destination, TRUE)){
    echo "Fail";    
}

// Copy the file to the Drupal files directory if it exists
if (file_exists($sourcePDF)) {

    if(!file_move($sourcePDF, $destination, FILE_EXISTS_RENAME)) {
        echo "Failed to move file: $sourcePDF.\n";
    } 
} else {

    echo "File Moved to " . $destination . "<br/>"; }


$mime = 'application/pdf'; // Importing PDF files
$file = new stdClass();
$file->filename = $withoutExt;
$file->filepath = $destinationPDF;
$file->filemime = $mime;
$file->filesize = filesize($destinationPDF);

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



$node = new stdClass();
$node->title = $employeeDate;
$node->body = $employeeID;
$node->type = 'estub';
$node->uid = 1;

//$field = field_file_save_file($file_drupal_path, file_directory_path() .'/'.    $directory);
//$node->$field_paystub_upload[] = $field;

$node->field_estub_upload = array(
    array(
      'fid' => $file->fid,
      'title' => basename($file->filename),
      'filename' => $file->filename,
      'filepath' => $file->filepath,
      'filesize' => $file->filesize,
      'mimetype' => $mime,
      'data' => array(
        'description' => $withoutExt,
      ),
      'list' => 1,
    ),
  );

  $node->status = 1;
  $node->active = 1;
  $node->promote = 1;
  node_save($node);

1 个答案:

答案 0 :(得分:0)

我的第一个猜测是文件需要位于“文件”目录中,以便drupal能够访问它。 /var/paystubs/$uid是什么设置为Drupal中的“files”目录?当您使用界面上传文件时,它们最终会在/var/paystubs/$uid

相关问题