Creating a folder when I run file_put_contents()

后端 未结 4 604
情歌与酒
情歌与酒 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:39

    file_put_contents() does not create the directory structure. Only the file.

    You will need to add logic to your script to test if the month directory exists. If not, use mkdir() first.

    if (!is_dir('upload/promotions/' . $month)) {
      // dir doesn't exist, make it
      mkdir('upload/promotions/' . $month);
    }
    
    file_put_contents('upload/promotions/' . $month . '/' . $image, $contents_data);
    

    Update: mkdir() accepts a third parameter of $recursive which will create any missing directory structure. Might be useful if you need to create multiple directories.

    Example with recursive and directory permissions set to 777:

    mkdir('upload/promotions/' . $month, 0777, true);
    
    0 讨论(0)
  • 2020-12-02 18:40

    I wrote a function you might like. It is called forceDir(). It basicaly checks whether the dir you want exists. If so, it does nothing. If not, it will create the directory. A reason to use this function, instead of just mkdir, is that this function can create nexted folders as well.. For example ('upload/promotions/januari/firstHalfOfTheMonth'). Just add the path to the desired dir_path.

    function forceDir($dir){
        if(!is_dir($dir)){
            $dir_p = explode('/',$dir);
            for($a = 1 ; $a <= count($dir_p) ; $a++){
                @mkdir(implode('/',array_slice($dir_p,0,$a)));  
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 18:43

    modification of above answer to make it a bit more generic, (automatically detects and creates folder from arbitrary filename on system slashes)

    ps previous answer is awesome

    /**
     * create file with content, and create folder structure if doesn't exist 
     * @param String $filepath
     * @param String $message
     */
    function forceFilePutContents ($filepath, $message){
        try {
            $isInFolder = preg_match("/^(.*)\/([^\/]+)$/", $filepath, $filepathMatches);
            if($isInFolder) {
                $folderName = $filepathMatches[1];
                $fileName = $filepathMatches[2];
                if (!is_dir($folderName)) {
                    mkdir($folderName, 0777, true);
                }
            }
            file_put_contents($filepath, $message);
        } catch (Exception $e) {
            echo "ERR: error writing '$message' to '$filepath', ". $e->getMessage();
        }
    }
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题