【问题标题】:can't open file or find the folder无法打开文件或找不到文件夹
【发布时间】:2019-05-23 08:36:23
【问题描述】:

我似乎找不到名为 invoice 的文件夹,并且不确定我的代码是否正确

我尝试将写入更改为写入和读取,但它仍然没有为我在文件夹中创建文件...

$invoice = "------------------------------------\n"; 
$invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
$invoice .=  $level1."\n"; 
$invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
$invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
$invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
$invoice .= "Payment Status:".$row['paid']."\n"; 
$invoice .= "Expiry Date:".$row['expirydate']."\n"; 
$invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 

$myfile='invoice/level1monthly/'.$_SESSION['u_uid'].'.txt';
$fh = fopen($myfile, 'w+') or die("can't open file");
fwrite($fh, $invoice);
fclose($fh);

我希望它在我的实时服务器的根目录中创建一个名为 invoice 的文件夹

3 级每月订阅计划信息 订阅计划: 注册日期: 月费:0 支付状态: 到期日: 付款截止日期:

我已完成以下代码,但无法将其更改为追加

$invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
                                $invoice .=  $level1."\n"; 
                                $invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
                                $invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
                                $invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
                                $invoice .= "Payment Status:".$row['paid']."\n"; 
                                $invoice .= "Expiry Date:".$row['expirydate']."\n"; 
                                $invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 
                                $myfile='invoice/level1monthly'.$_SESSION['u_uid'].'.txt';
                                $fh = fopen($myfile, 'a+') or die("can't open file");
                                fwrite($fh, $invoice);
                                fclose($fh);
                                  } else {

                                  $invoice = "------------------------------------\n"; 
                                $invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
                                $invoice .=  $level1."\n"; 
                                $invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
                                $invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
                                $invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
                                $invoice .= "Payment Status:".$row['paid']."\n"; 
                                $invoice .= "Expiry Date:".$row['expirydate']."\n"; 
                                $invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 
                                $myfile='invoice/level1monthly'.$_SESSION['u_uid'].'.txt';
                                $fh = fopen($myfile, 'w+') or die("can't open file");
                                fwrite($fh, $invoice);
                                fclose($fh);
                                  }

这是我更新的代码...如果新文件不存在然后附加它,这是否可以创建一个新文件?

$myfile='invoice/level1monthly'.$_SESSION['u_uid'].'.txt';

                                  if(file_exists($myfile)) {
                                     $invoice = "------------------------------------\n"; 
                                $invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
                                $invoice .=  $level1."\n"; 
                                $invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
                                $invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
                                $invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
                                $invoice .= "Payment Status:".$row['paid']."\n"; 
                                $invoice .= "Expiry Date:".$row['expirydate']."\n"; 
                                $invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 

                                $fh = fopen($myfile, 'a+') or die("can't open file");
                                fwrite($fh, $invoice);
                                fclose($fh);
                                  } else {

                                  $invoice = "------------------------------------\n"; 
                                $invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
                                $invoice .=  $level1."\n"; 
                                $invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
                                $invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
                                $invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
                                $invoice .= "Payment Status:".$row['paid']."\n"; 
                                $invoice .= "Expiry Date:".$row['expirydate']."\n"; 
                                $invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 
                                $myfile='invoice/level1monthly'.$_SESSION['u_uid'].'.txt';
                                $fh = fopen($myfile, 'w+') or die("can't open file");
                                fwrite($fh, $invoice);
                                fclose($fh);
                                  }

【问题讨论】:

  • 据我了解,使用 w 模式会尝试创建它不存在的文件,但模式 a 也会创建文件吗?
  • 它已经用 w+ 模式创建了文件,但是现在如果我想追加,我需要添加一个模式来代替吗?我想,如果首先,我想创建一个新文件,然后第二个,附加到它,我将如何让它工作?
  • 我在这里遇到了一些奇怪的事情,我应该将代码放在电子邮件之后还是电子邮件之前?顺序有关系吗?
  • Level 3 每月订阅计划信息订阅计划: 注册日期: 月费:0 付款状态: 到期日: 付款到期日:
  • 启用错误报告。

标签: php file session directory


【解决方案1】:

如果目录尚未创建,则需要手动创建或使用mkdir()

$dir = __DIR__.'/invoice/level1monthly/';
# If directory doesn't exist
if(!is_dir($dir))
    # Create it recursively and use folder permission 0755
    mkdir($dir, 1, 0755);

你也可以使用file_put_contents(),在我看来它更直接:

$invoice = "------------------------------------\n"; 
$invoice .= "Level 1 Monthly Subscriptionplan Information\n"; 
$invoice .=  $level1."\n"; 
$invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
$invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
$invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
$invoice .= "Payment Status:".$row['paid']."\n"; 
$invoice .= "Expiry Date:".$row['expirydate']."\n"; 
$invoice .= "Payment Due Date:".$row['paidbydate']."\n";
# I am assuming this script is happening in the root.
$dir = __DIR__.'/invoice/level1monthly/';
if(!is_dir($dir))
    mkdir($dir, 1, 0755);
# Append
$myfile = $dir.$_SESSION['u_uid'].'.txt';
# Put contents
file_put_contents($myfile, $invoice);

echo is_file($myfile);

【讨论】:

  • 谢谢,我刚刚更新了我的代码,因为有些变量没有通过...我可以在其中使用 $row 变量还是应该使用普通变量?
  • 可能$row变量是一个数组,所以不能用
  • 我在这里也很困惑,因为我使用 w 模式为新用户创建了一个新文件,但是我该如何将其更改为追加?假设用户想要创建一个如上所示的新文件,但对于每个额外的升级,我想追加到它
  • 我需要做类似... if($file_exists($myfile) { file_put_contents($myfile, "a")}
  • 谢谢,如果文件存在语句,我上面更新的代码是否正确?
猜你喜欢
  • 1970-01-01
  • 2015-08-20
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 2015-07-07
相关资源
最近更新 更多