2

Is it possible to write data to CSV while the file is opened from desktop? Because I am trying to write some input data to CSV when user submit the form, but I am getting above error when the file is opened from my desktop. My system will only write to csv when I close the file.

Error when meeting the line $fp = fopen("C:\Users\lenovo\Desktop\sample.csv", "w");

Leon
  • 329
  • 2
  • 3
  • 15

2 Answers2

0
<?php       
       $myfile = fopen("C:\Users\lenovo\Desktop\sample.csv", "a+");
        // the variable holding the values
        $numbers;
        // write the variable in the text file created 
        fwrite($myfile, $numbers);
        // close the file when done
        fclose($myfile);
?>

W = Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file

a+ = Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist

I hope it helps

0

Change the permission of the file sample.csv by using the following command on the terminal in the folder where the file is-

chmod 777 sample.csv
  • 1
    `fopen("C:\Users\lenovo\Desktop\sample.csv", "w")` shows that user is using Windows where you don't have `chmod`. – SaschaM78 Sep 17 '20 at 13:20
  • 1
    Whatever you are hoping to accomplish, **`chmod 777` is *wrong* and *dangerous.*** You absolutely do not want to grant write access to executable or system files to all users under any circumstances. You will want to revert to sane permissions ASAP (for your use case, probably `chmod 755`) and learn about the Unix permissions model before you try to use it again. If this happened on a system with Internet access, check whether an intruder could have exploited this to escalate their privileges. – tripleee Oct 28 '22 at 12:01