-4

I have a drupal 9 site. There is a zip file in public folder(sites/default/files/), I have extracted the zip and kept it inside public folder(sites/default/files/). This extracted folder contains a .docx file, which I have moved to encrypt folder(sites/default/files-encrypted/). I have copied its uri to file-managed table to get the file id of this docx (fid), in order to insert the file into drupal's encrypt filesystem. I have attached this file id with a particular file field of a content type. I am able to successfully upload that file into that field programmatically but on clicking the uploaded document link, I get access denied error. Here I am attaching the code :

function upload_form_submit(array $form, FormStateInterface $form_state){   
$values = $form_state->getValues();
    $fid = $values['field_bulk_zip'][0]['fids'][0];
    $formatted_docs_name_array = array();
    $database = \Drupal::database(); 
    $get_zip_uri_query = $database->query("select uri, filename from file_managed where fid = '$fid' ");
    $get_zip_array = ($get_zip_uri_query->fetchAll())[0];
    $get_zip_uri = $get_zip_array->uri;
    $get_zip_filename = $get_zip_array->filename;
    $extracted_folder_name = str_replace('.zip', '', $get_zip_filename);
    $file_system = \Drupal::service('file_system');
    $zippedpath = ($file_system->realPath('public://')) ;
    $zip_folder_path = str_replace('public://', ($zippedpath . '/'), $get_zip_uri);
    $extracted_folder_path = ($file_system->realPath('public://')) . '/' . $extracted_folder_name;
    $zip = new \ZipArchive;
    
    if ($zip->open($zip_folder_path) === TRUE) {
        $zip->extractTo($extracted_folder_path . '/');
        $zip->close();
    }
    
    $extracted_docs_name_array = array_diff(scandir($extracted_folder_path), array('.', '..'));

    $count = 2 + sizeOf($extracted_docs_name_array);
    for($i = 2 ; $i < $count; $i++){
        $doc_name = $extracted_docs_name_array[$i];
        $doc_uri = 'public://' . $extracted_folder_name . '/' . $doc_name;
        $oauth_token = get_oauth_token();
        $doc_url = file_create_url($doc_uri);
        $file_data = file_get_contents($doc_url, $oauth_token);
        $src = ($file_system->realPath('public://')) . '/' . $extracted_folder_name . '/' . $doc_name;
        $dest = ($file_system->realPath('encrypt://file_encryption')) . '/2022-09/' . $doc_name;
        copy($src, $dest);
        $dest1 = 'encrypt://file_encryption'  . '/2022-09/' . $doc_name;
        $fileRepository = \Drupal::service('file.repository');
        $fileRepository->writeData($file_data, $dest1, FileSystemInterface::EXISTS_REPLACE);
        $mak_node_id = intval(str_replace(".docx", "", $doc_name));
        $get_fid_query = $database->query("select fid from file_managed where uri = '$dest1' ");
        $fid = ($get_fid_query->fetchAll())[0]->fid;
        $file = File::load($fid);
        $node = Node::load($mak_node_id);
        $node->set('field_mak_detail_file', ['target_id' => $fid]);
        $node->save();
    
    }
New contributor
Dipayan Roy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • What is "Drupal's encrypt filesystem"? Sep 22 at 13:34
  • 1
    The code could never work, there's no $form_state in scope in your function.
    – Clive
    Sep 22 at 13:41
  • Welcome to Drupal Answers! When asking a question about code, the code should not contain obvious PHP errors, like the one spotted by @Clive, or the fact the line defining the function doesn't start with function. (Yes, PHP isn't Bash.)
    – apaderno
    Sep 22 at 16:25
  • @Clive I have edited the code. Sep 23 at 6:58

0

Your Answer

Dipayan Roy is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.