Forum


Replies: 1   Views: 2402
Unify path before exploding in createdocxanddownload
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by andrebernemann  · 19-11-2019 - 15:31

Hi,

you have changed the function createDocxAndDownload in CreateDocx.php from:

if (isset($args[0]) && !empty($args[0])) {
        $fileName = $args[0];
        $completeName = explode("/", $args[0]);
        $fileNameDownload = array_pop($completeName);
} else {
        $fileName = 'document';
        $fileNameDownload = 'document';
}

to (in 9.0 or 9.5):

if (isset($args[0]) && !empty($args[0])) {
    $fileName = $args[0];
    $completeName = explode(DIRECTORY_SEPARATOR, $args[0]);
    $fileNameDownload = array_pop($completeName);
} else {
    $fileName = 'document';
    $fileNameDownload = 'document';
}

Although, the first version is not 100% correct ($fileName is not used in the explode() stmt) it worked in my case because i use "/" as path separator. The new code does not work for me for the same reason. In my opinion, you should not rely on the DIRECTORY_SEPARATOR because developers can pass either "/" or "\\" (at least on windows).

Like you did in TransformDoc.php maybe you should better unify the separators to DIRECTORY_SEPARATOR before exploding by it like in the following suggestion:

if (isset($args[0]) && !empty($args[0])) {
        $fileName = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $args[0]);
        $completeName = explode(DIRECTORY_SEPARATOR, $fileName);
        $fileNameDownload = array_pop($completeName);
} else {
        $fileName = 'document';
        $fileNameDownload = 'document';
}

Regards,

André Bernemann