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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错