0
votes

I'm trying to use this script to upload files into Drupal. I'm having problems moving the file(s) from A to B. Drupal's filesystem is set to private

The nodes are created fine and everything works except for some reason I cannot download the attached file. It just takes me to page not found. If I create the node within drupal and upload the file it works fine.

I must be doing something wrong with my code here. I was following this guide for uploading files but I can't seem to find anything about setting Drupal's file-system to private and what someone would have to do to make it work properly programmatically.

Anyone have any experience with this?

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

1 Answers

0
votes

My first guess is that the file needs to be in the "files" directory for drupal to have access to it. Is /var/paystubs/$uid what the is set as the "files" directory in Drupal? When you upload files using the interface do they end up in /var/paystubs/$uid