bob don
bob don

Reputation: 19

Creating a directory using PHP

I am trying to create a directory with that will create a folder for each month. I would like my directory to be setup like this. Root\subfolder\month . Inside the month folder will be a daily report that will get generated. The problem that I have when i code this is i receive a syntax error saying that there is an uexpected '\' (T_NS_SEPARATOR). The code that i have for this looks like this.

$month = date('M');
file('Driver Check In\Void\'.$month. '\Void_'.date('m-d-y').".csv");

I also have code that will create the month folder if it does not exist.

How can I resolve this issue?

Full Code

if(isset($_POST['Void']))
{
    $month6 = date('M');
    $fp6 = file('Driver Check In\Void\\'.$month6. '\Void_'.date('m-d-y'). '.csv');
    $header6 = array("Date", "Customer", "Location/City", "Driver", "Cases", "Bottles", "Reason", "Comment");
    $dates6 = $_POST['dates6'];
    $customer2 = $_POST['customer2'];
    $location2 = $_POST['location2'];
    $driver6 = $_POST['drivers6'];
    $cases6 = $_POST['cases6'];
    $bottles6 = $_POST['bottles6'];
    $reason2 = $_POST['reason2'];
    $comment2 = $_POST['comment2'];
    $result6 = '';
    $search6 = "Date";
    $line_number6 = false;

    while(list($key6, $line6) = each ($fp6) and !$line_number6)
    {
        $line_number6 = (strpos($line6, $search6) !== FALSE);
    }
    if($line_number6)
    {
        $result6 .=
                    $dates6. " ,". $customer2. " ,". $location2. " ,". $driver6. " ,". $cases6. " ,". $bottles6. " ,". $reason2. " ,". $comment2. "\r\n";
    }
    else
    {
        $result6 .= implode(",", $header6). "\r\n".
                    $dates6. " ,". $customer2. " ,". $location2. " ,". $driver6. " ,". $cases6. " ,". $bottles6. " ,". $reason2. " ,". $comment2. "\r\n";
    }
    if(!is_dir('Driver Check In\Void\\'.$month6))
    {
        mkdir('\Driver Check In\Void\\'.$month6);
    }
    file_put_contents('Driver Check In\Void\\'.$month6. '\Void_'.date('m-d-y'). ".csv", $result6, FILE_APPEND);
    echo "data added6";


}

Upvotes: 0

Views: 89

Answers (2)

Allen Butler
Allen Butler

Reputation: 312

Well, for one it looks like you have some syntax problems in your ' and " placements. Its also important to know that \ is considered an escape sequence which will escape the strings following the mark. Try doing the following:

file('Driver Check In\\Void\\'.$month. '\\Void_'.date('m-d-y').'.csv');

Have you also considered the mkdir function?

Upvotes: 1

Jimmmy
Jimmmy

Reputation: 579

you have to escape your backslashes so them do not consume your quotes. i would suggest using php constant DIRECTORY_SEPARATOR (listed in here: http://php.net/manual/en/dir.constants.php) instead of backslashes..

'Driver Check In\Void\'.$month. '\Void_'.date('m-d-y').".csv"

backslash after Void\ consumes the quote and interpret it as part of string, not closing char of the string.

Upvotes: 0

Related Questions