无法打开文件或找不到文件夹

时间:2018-12-24 04:00:02

标签: php file session directory

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

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

$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的文件夹

Level 3 Monthly Subscriptionplan Information

Subscriptionplan:
Enrollment Date:
Monthly Fees:0
Payment Status:
Expiry Date:
Payment Due Date:

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

$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);
                                  }

1 个答案:

答案 0 :(得分: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);