skip to Main Content

Am trying to save a word document using PhpWord 1.0 object Writer within the storage folder in laravel 10 but am getting an exception "mkdir(): No such file or directory". The logic is as follows:

 // Set MS Word compatibility to MS Office 2007
        $phpWord->getCompatibility()->setOoxmlVersion(12);

        $filesBasePath = storage_path('app/references/');

        $referenceDir = session()->get('random_files_dir');

        if (!empty($referenceDir) and is_dir($filesBasePath . $referenceDir)) {

            $absoluteRefPath = $filesBasePath . $referenSceDir;
            // Saving the document as OOXML file.
            $objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
            $objWriter->save($absoluteRefpath . '/references.docx');
        } else {
            $referenceDir = Str::random(8);
            $absoluteRefPath = $filesBasePath . $referenceDir;

            if (!File::isDirectory($absoluteRefPath)) {
                if (File::makeDirectory($absoluteRefPath, 0755, true, true)) {
                    // Saving the document as OOXML file.
                    $objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
                    $objWriter->save($absoluteRefPath . '/references.docx');

                    session(['random_files_dir' => $referenceDir]);
                }
            }
        }

        return response()->download($absoluteRefPath . '/references.docx')

What might be the issue? Am running on centos 7

I was expecting the word document to be save inside storage/app/references/random folder. The random folder is being created but the the Phpword Object writer throws an exception. the below Line is the one that throws an error;

$objWriter->save(storage_path('references.docx'));

2

Answers


  1. Chosen as BEST ANSWER

    I have managed to solve the issue by setting the value for temp directory in php.ini file as follows:

    sys_temp_dir = "/temp"

    Thank you to everyone for your ideas on how to try and solve the problem.


  2. It looks like you are having difficulties with the file system. First consider making the processing simpler. This should not solve your problem, as mkdir() is still called on this path under the cover, but who knows:

    // Set MS Word compatibility to MS Office 2007
    $phpWord->getCompatibility()->setOoxmlVersion(12);
    
    $filesBasePath = storage_path('app/references/');
    
    $referenceDir = session()->get('random_files_dir');
    if (empty($referenceDir)) {
        $referenceDir = Str::random(8);
        session(['random_files_dir' => $referenceDir]);
    }
    
    $absoluteRefPath = $filesBasePath . $referenceDir;
    File::ensureDirectoryExists($absoluteRefPath, 0755, true, true);
    
    // Saving the document as OOXML file.
    $objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save($absoluteRefPath . '/references.docx');
    
    return response()->download($absoluteRefPath . '/references.docx')
    

    Now, if I don’t mistake your code, the part that is causing the issue is only to create the temporary directory for the file to save temporarily for the download.

    If so, this is not necessary at all. It’s perhaps a bit cheap as that is what you’re worried about in your question, but if you could just output the docx file without the round-trip on the file-system first:

    // Set MS Word compatibility to MS Office 2007
    $phpWord->getCompatibility()->setOoxmlVersion(12);
    $objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
    
    return response()->streamDownload(function () use ($objWriter) {    
        $objWriter->save('php://output');
    }, 'references.docx')
    

    This reduces the code down to five lines and has no mkdir() call any longer, also session is not needed. YMMV.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search