[英]Drupal 6 Node Creation PHP Script with File upload not working
I've got this PHP script I'm working on to import pay-stubs into Drupal. 我已经有了这个PHP脚本,正在将工资单导入Drupal。 It's doing everything the way I want except the script is not attaching the uploaded PDF file to the node.
除了脚本没有将上传的PDF文件附加到节点之外,它按照我想要的方式进行了所有操作。
A few notes; 一些注意事项; Drupal's filesystem is set to private, not sure if this makes a difference or not.
Drupal的文件系统设置为私有,不确定是否有区别。 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.
其次,pdf文件已经在正确的位置“ paystubs / [uid] /paystub_1.pdf”中,因此我认为我的问题是该文件未正确关联到节点。
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\n";
}
}
//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.
当我使用Devel模块查看节点时,可以看到'field_paystub_upload'数组为空。 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?
最好的回应是免费上网吗?
Drupal's file.inc file_save_upload uses $_FILES
, which is a global, magically set by PHP . Drupal的file.inc file_save_upload使用
$_FILES
,这是一个由PHP神奇设置的全局 变量 。 Drupal expects an uploaded file, not a file that exists locally. Drupal需要一个上传的文件,而不是本地存在的文件。
You best just call a custom file-saver method, to process local files. 最好只调用自定义文件保存方法来处理本地文件。 Make sure its path up in the
files
database-table too. 确保其路径也位于
files
数据库表中。 file_save_upload will be valuable for creating such a helper method. file_save_upload对于创建这样的辅助方法将非常有用。
Big thanks to berkes for helping me solve this problem. 非常感谢berks帮助我解决了这个问题。 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.
事实证明,由于文件已经在drupal Web服务器上并且没有上传到PHP $ _FILES全局变量,所以我无法以编程方式正确地上传文件。
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.
我尝试使用Drupals defualt上传模块,也尝试使用CCK的fielfield模块都无法正常工作。 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.
多亏了berkes的建议,我找到了CCK的filefield小部件随附的函数来保存服务器上已经存在的上载文件。 Hopefully this helps someone else.
希望这可以帮助其他人。
This is the function I found that can save a file thats already on the web-server. 这是我发现的功能 ,可以保存Web服务器上已经存在的文件。
Here is the working code I used to create the node and attach the file after calling field_file_save_file. 这是我用来创建节点并在调用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)){
mkdir($dest_folder);
}
// 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 再次感谢伯克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.