psr7 icon indicating copy to clipboard operation
psr7 copied to clipboard

cant use tryFopen with Swoole + AwsS3sdk and freebsd

Open tiagocomti opened this issue 2 years ago • 0 comments

PHP version: x.y.z (hint: php --version) PHP 7.3.18 FreeBSD 12.2-STABLE FreeBSD 12.2-STABLE r370102 PROAPPS amd64 Description Guys, I have a problem using the tryFopen function to create a temporary file on my system.

Basically, I use amazons3 framework and when I download a file from my bucket it creates a temporary file in the system's /tmp, but it calls the function like this: tryFopen("_MY_BUCKET_KEY", "w+") (whitout "/tmp" before my path) and I always get an error saying, ( Could not create file with w+ permission.)

By default, /tmp has permission of 777 (open for anyone to do what they want) but still my PHP + swoole was not letting me save there. I was debugging and I understood the reason, Swoole in freebsd ignores the default tmp path, for security reasons and when you pass the path without the "/" it will save in the location that is the root of the class, right in the vendors folder. That way, it will NEVER be saved because it doesn't have write permission there, only read. The solution would be: in the tryFopen function try to validate if it is a real path, if yes, see if it has write permission otherwise, send it by default to /tmp/$filename. example:

 public static function tryFopen($filename, $mode)
    {
        if(substr($filename, 0, 1) !== "/" && substr($filename, 0, 3) !== "php"){
            $filename = "/tmp/".$filename;
        }

I sent this suggestion to the amazon SDK about 2 months ago but I think they ignored it, because nobody uses the sdk with swoole + php and freebsd....

How to reproduce Using AWss3 + swoole and freebsd try to get any bucket file. with you guys want i can show in live.

Possible Solution swoole-foundation/yii2-swoole-extension/src/web/Request.php

 public static function tryFopen($filename, $mode)
    {
        if(substr($filename, 0, 1) !== "/" && substr($filename, 0, 3) !== "php"){
            $filename = "/tmp/".$filename;
        }

Additional context Sorry about my english, I'm little rusty

tiagocomti avatar Jun 24 '22 15:06 tiagocomti