1

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 = \PhpOffice\PhpWord\IOFactory::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 = \PhpOffice\PhpWord\IOFactory::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'));
7
  • Does storage/references.docx exist? Commented Jul 25, 2023 at 13:33
  • Yes it already exists and also the random folder is also being created. Commented Jul 25, 2023 at 13:35
  • The code is running on my local machine but when i upload it on hosting server which is also centos 7, an exception is thrown Commented Jul 25, 2023 at 13:37
  • 1
    check permissions for folders on the remote serever Commented Jul 25, 2023 at 13:37
  • 1
    The references permissions are owner: read/write/execute, group: read/execute-setuid, everyone: read/execute. Make sure all of the directories are owned by the webserver user (apache, www-data) Commented Jul 25, 2023 at 13:57

2 Answers 2

1

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

sys_temp_dir = "/temp"
Sign up to request clarification or add additional context in comments.

Comments

0

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 = \PhpOffice\PhpWord\IOFactory::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 = \PhpOffice\PhpWord\IOFactory::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.

6 Comments

The bug still persist. I Have tried all the above but the error still can be resolved
I guess you mean can't. So even with the second alternative, the code that does not save a file nor creates a directory, you still get the mkdir() error, right? Which line of it does it cause?
This line is the one that through error: $objWriter->save($absoluteRefPath . '/references.docx');
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError vendor/phpoffice/phpword/src/PhpWord/Writer/AbstractWriter.php:203
Can Selinux be the cause of the error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.