Creating a folder when I run file_put_contents()

后端 未结 4 650
情歌与酒
情歌与酒 2020-12-02 18:06

I have uploaded a lot of images from the website, and need to organize files in a better way. Therefore, I decide to create a folder by months.

$month  = dat         


        
4条回答
  •  悲&欢浪女
    2020-12-02 18:45

    i have Been Working on the laravel Project With the Crud Generator and this Method is not Working

    @aqm so i have created my own function

    PHP Way

    function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
        {
            $exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
    
            array_pop($exploded);
    
            $directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
    
            if (!file_exists($directoryPathOnly)) 
            {
                mkdir($directoryPathOnly,0775,true);
            }
            file_put_contents($fullPathWithFileName, $fileContents);    
        }
    

    LARAVEL WAY

    Don't forget to add at top of the file

    use Illuminate\Support\Facades\File;

    function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
        {
            $exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
    
            array_pop($exploded);
    
            $directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
    
            if (!File::exists($directoryPathOnly)) 
            {
                File::makeDirectory($directoryPathOnly,0775,true,false);
            }
            File::put($fullPathWithFileName,$fileContents);
        }
    

提交回复
热议问题