Creating a folder when I run file_put_contents()

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  = date('Yd')
file_put_contents("upload/promotions/".$month."/".$image, $contents_data);

after I tried this one, I get error result.

Message: file_put_contents(upload/promotions/201211/ang232.png): failed to open stream: No such file or directory

If I tried to put only file in exist folder, it worked. However, it failed to create a new folder.

Is there a way to solve this problem?


Solution 1:

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);

Solution 2:

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();
    }
}

Solution 3:

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);
    }