duangang1991 2016-05-16 14:30
浏览 21
已采纳

使用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. "
";
    }
    else
    {
        $result6 .= implode(",", $header6). "
".
                    $dates6. " ,". $customer2. " ,". $location2. " ,". $driver6. " ,". $cases6. " ,". $bottles6. " ,". $reason2. " ,". $comment2. "
";
    }
    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";


}
  • 写回答

2条回答 默认 最新

  • dragonmeng2002 2016-05-16 14:38
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗