$name = basename($file); // Build the file object $file_obj = new stdClass(); $file_obj->filename = $name; $file_obj->filepath = $file; $file_obj->filemime = 'text/plain'; $file_obj->filesize = $filesize; // You can change this to the UID you want $file_obj->uid = $uid; $file_obj->status = FILE_STATUS_TEMPORARY; $file_obj->timestamp = time(); $file_obj->list = 1; $file_obj->new = true; // Save file to files table drupal_write_record('files', $file_obj); // change file status to permanent file_set_status($file_obj, 1); // Attach the file object to your node XXX create/attach to comment? $issue->files[$file_obj->fid] = $file_obj; } } upload_save($issue); // Mark it private. It'd be nice if this could be wrapped in a transaction // with above node_save. //if ($private) { //db_query("UPDATE {node_access},{private} SET gid='$uid', realm='private_author', grant_view='1', grant_update='1', grant_delete='1', private='1' WHERE {node_access}.nid='$nid' AND {private}.nid={node_access}.nid"); //db_query("UPDATE {private} SET private='1' WHERE nid='$nid')"); //} // Now that everything is privatized and such, send notifications project_mail_notify($nid); print "OK {$nid} http://www.virtualmin.com/node/{$nid}\n";
/** * Moves a PDF file to the appropriate location * * @param string $filename */ function move_pdf($filename) { global $mniblogpub_props; // Get all our names and directories $full_src = $mniblogpub_props["mniblogpub.shared"]["bloghome"] . "/pdfs/" . $filename; $orig_src = $full_src; $dst_dir = file_directory_path() . "/" . $mniblogpub_props["mniblogpub.php"]["dir.pdf.drupal"]; $full_dst = $dst_dir . "/" . $filename; $dir_perms = !is_dir($dst_dir); // Check our path $p = file_create_path($dst_dir); if (!file_check_directory($p, FILE_CREATE_DIRECTORY)) { if (!file_check_directory($p, FILE_MODIFY_PERMISSIONS)) { print "ERROR: Could create pdfs directory at: " . $dst_dir . "\n"; exit(5); } } if ($dir_perms) { if (!chgrp($dst_dir, "www-data")) { print "WARN: Could not change dir to www-data group. " . $dst_dir . "\n"; } if (!chmod($dst_dir, 0775)) { print "WARN: Could not change permissions for dir. " . $dst_dir . "\n"; } } // Error Checking if (!file_copy($full_src, $full_dst, FILE_EXISTS_REPLACE)) { print "ERROR: Could not move file " . $full_src . "\n"; exit(5); } if (!file_exists($full_dst)) { print "ERROR: File not actually moved. " . $full_src . "\n"; exit(5); } if (!chgrp($full_dst, "www-data")) { print "WARN: Could not change file to www-data group. " . $full_dst . "\n"; } if (!chmod($full_dst, 0664)) { print "WARN: Could not change permissions for file. " . $full_dst . "\n"; } $details = stat($full_dst); $file = new stdClass(); $file->filename = basename($full_dst); $file->filepath = $full_dst; $file->filemime = file_get_mimetype($full_dst); $file->filesize = $details['size']; $file->filesource = basename($full_dst); $file->uid = $mniblogpub_props["mniblogpub.php"]["user.use"] == 1 ? $mniblogpub_props["mniblogpub.php"]["user.id"] : 1; $file->status = FILE_STATUS_PERMANENT; $file->timestamp = time(); $file->list = 1; $file->new = true; drupal_write_record('files', $file); if ($file != null && $file) { if (!unlink($orig_src)) { // !!! NOTE: // I don't think it's necessary to exit out here. I'm // commenting this out for now. If it causes errors later, // come back here. -Gerg // print "ERROR: Could not remove src file at: " . $orig_src; // exit(5); print "WARN: Could not remove src file at: " . $orig_src . "\n"; } } else { print "ERROR: Error saving file object in Drupal. " . $full_src . "\n"; exit(5); } file_set_status($file, 1); return $file; }
function budgets_supplier_form_submit($form_id, &$form_values) { $supplier =& $form_values['values']; guifi_log(GUIFILOG_TRACE, 'function budgets_supplier_submit()', $supplier); /* * Delete logo */ if (isset($supplier['Delete'])) { $result = file_delete($supplier['logo']); guifi_log(GUIFILOG_TRACE, 'function budgets_supplier_submit(DELETE)', $result); $supplier['logo'] = null; $form_values['#redirect'] = FALSE; $form_values['rebuild'] = TRUE; return; } #this leads us to sites/mysite.example.com/files/ $dir = file_directory_path(); $dir .= '/suppliers'; guifi_log(GUIFILOG_TRACE, 'function budgets_supplier_submit(DIR)', $dir); # unlike form submissions, multipart form submissions are not in # $form_state, but rather in $FILES, which requires more checking if (isset($_FILES) && !empty($_FILES) && $_FILES['files']['size']['logo'] != 0) { #this structure is kind of wacky $name = $_FILES['files']['name']['logo']; $size = $_FILES['files']['size']['logo']; $type = $_FILES['files']['type']['logo']; #this is the actual place where we store the file $file = file_save_upload('logo', array(), $dir); if ($file) { drupal_set_message(t('You uploaded %name', array('%name' => $name))); guifi_log(GUIFILOG_TRACE, 'file', $file->filepath); $supplier['logo'] = $file->filepath; file_set_status($file, FILE_STATUS_PERMANENT); } else { drupal_set_message("Something went wrong saving your file."); } } else { drupal_set_message("Your file doesn't appear to be here."); } $form_values['#redirect'] = FALSE; $form_values['rebuild'] = TRUE; }
/** * Attaches a file to the image node * * @param int $nid ["path","0"] * The nid of the image to update * @param object $file ["data"] * The file object * @return object * * @Access(callback='DocuWalkPictureResource::access', args={'update'}, appendArgs=true) * @RESTRequestParser(mime='image/*', parser='RESTServer::fileRecieve') */ public static function uploadFile($nid, $file) { global $user; $node = node_load($nid); // Mark old file as temporary, then it'll be removed on next cron run, and we don't have to bother // ourselves with the business of deleting the file and the database entry for it if (!empty($node->field_picture) && $node->field_picture[0]['fid']) { $old_file = (object) $node->field_picture[0]; file_set_status($old_file, FILE_STATUS_TEMPORARY); } $dir = file_directory_path() . '/pictures'; if (!file_exists($dir)) { mkdir($dir); } $file->uid = $user->uid; $file->status = FILE_STATUS_PERMANENT; $destination = file_destination(file_create_path($dir . '/' . $file->filename)); rename($file->filepath, $destination); $file->filepath = $destination; drupal_write_record('files', $file, array('fid')); $node->field_picture = array((array) $file); node_save($node); return $file; }