-2

I can't seem to find the folder called invoice and not sure whether my code is correct or not

I tried to change the write to write and read but it still hasn't created the file in the folder for me...

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

I expect it to create a folder called invoice in my root directory of my live server

Level 3 Monthly Subscriptionplan Information

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

I have done the following code but can't get it to change to append

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

This is my updated code... Will this work in terms of creating a new file if it doesn't exist and then append it?

$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);
                                  }
piano0011
  • 61
  • 7
  • From my understanding, using the w mode will attempt to create the file it it doesn't exist but will the mode a also create the file? – piano0011 Dec 24 '18 at 04:00
  • It has created the file with the w+ mode but now if I would like to append, would I need to add a a mode instead? I guess, how would I get it to work if first, I would like to create a new file and then 2nd, append to it? – piano0011 Dec 24 '18 at 04:16
  • I got something weird here and should I place the code after my email or before the email? Does it matter with the order? – piano0011 Dec 24 '18 at 04:40
  • Level 3 Monthly Subscriptionplan Information Subscriptionplan: Enrollment Date: Monthly Fees:0 Payment Status: Expiry Date: Payment Due Date: – piano0011 Dec 24 '18 at 04:40
  • Enable error reporting. – Funk Forty Niner Dec 24 '18 at 04:44
  • Is this database related also? Seems to be. Or maybe it's not. – Funk Forty Niner Dec 24 '18 at 04:44

1 Answers1

1

If the directory is not already created, you will need to manually create it or use 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);

You may as well use file_put_contents() as well, it's more straight forward, in my opinion:

$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);
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • thanks and I just updated my code because some of the variables are not getting through... Can I use $row variables in this or should I use normal variables? – piano0011 Dec 24 '18 at 04:42
  • Maybe the $row variables is an array, so I can't use it – piano0011 Dec 24 '18 at 04:42
  • I am also confused here because I have used the w mode to create a new file for a new user but how would I change this to append? Let's say that the user wants to create a new file as shown above but for each additional upgrade, I would like to append to it – piano0011 Dec 24 '18 at 04:43
  • would I need to do something like... if($file_exists($myfile) { file_put_contents($myfile, "a")} – piano0011 Dec 24 '18 at 04:44
  • thanks and is my updated code above correct with the if the file exists statement? – piano0011 Dec 24 '18 at 05:00
  • Also, most of the times, it did write to the file but sometimes it failed to write... Does this happen often? – piano0011 Dec 24 '18 at 05:01
  • I got it to work... I just had to place the $myfile at the top so that the if statement could process it – piano0011 Dec 24 '18 at 05:05
  • How would I attach this in my email? I am getting a syntax error here: – piano0011 Dec 24 '18 at 05:25
  • $file = "invoice/level1monthly'.$_SESSION['u_uid'].'.txt"; – piano0011 Dec 24 '18 at 05:25
  • `$file = "invoice/level1monthly/".$_SESSION['u_uid'].".txt";` – Rasclatt Dec 24 '18 at 05:26
  • Send attachments: https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – Rasclatt Dec 24 '18 at 05:28
  • I am trying to get the following syntax to work but read that I should be using double quotes around the \n\n tag but can't get it to line break.... – piano0011 Dec 24 '18 at 07:40
  • I tried using double quotes and single quotes but none seem to work – piano0011 Dec 24 '18 at 07:40
  • $htmlContent = "

    Activate your Level 3 Monthly Membership Plan!

    Dear ".$_SESSION['u_first']." ".$_SESSION['u_last']."; \n\n Thank you for registering your Level 3 Monthly Membership Plan with PianoCourse101! You are receiving this e-mail because you or someone else claiming to be you has bought a Level 3 Monthly Membership Plan \n\nIf you believe that this is a mistake, please send us a ticket with the subject \"How to cancel my Level 3 Monthly Membership Plan?\" and allow at least 48 hours before receiving a repl

    – piano0011 Dec 24 '18 at 07:40