Michael Ahrendt
Michael Ahrendt

Reputation: 3

PHP mkdir not working as expected

I'm working on some PHP that will create a folder every time a request is made to my server. I can't seem to get my syntax proper. If I just used the $date variable it works no problem, however when I add the "clean" folder before it, it won't create the folder.

<?php

$time = $_SERVER['REQUEST_TIME'];
$date =  date(Ymd_Hms, $time);
$new =  "clean/".$date;

echo $new;

if(!is_dir ($new))
{
    mkdir($new);
}

?>

Upvotes: 0

Views: 1281

Answers (2)

Exception
Exception

Reputation: 787

This is scenario is happened to me when I recently configure server mkdir() function was working and it was giving permission error constantly. So I found out the solution that folder (in this case clean folder) in which you are creating another folder must have 0777 / 0755 permission and its user:group must be same as that of user and group given in httpd.conf file in apache or httpd folder .

chown -R root:root clean/ when I gave that to that folder it was working like magic. Please check with this , if all above solutions failed then this will definitely help you.

Upvotes: 1

DirtyBit
DirtyBit

Reputation: 16772

Put the in ' quotes and give the mkdir() appropriate params and it should work fine:

<?php
$time = $_SERVER['REQUEST_TIME'];
$date =  date('Ymd_His', $time);
$new =  "clean/".$date;

echo $new;

if(!is_dir ($new))
{
mkdir($new, 0777, true);
}
?>

mode: The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod()

Note: mode is ignored on Windows.

Return Values:

Returns TRUE on success or FALSE on failure.

Note: You don't have to create the folder clean first IF you're using the recursive = true option, Otherwise a folder named clean must be created first.

PHP Manual: mkdir

PS. I've created a directory using this code before posting so it should not have any problem.

Upvotes: 0

Related Questions