Why do I get an Error when Creating .text file using php in xampp macOS

Viewed 29

I am a Beginner in PHP and want to create a text file in my Directory that PHP files are in it but it gives me this Error:

Warning: fopen(numbers.dat): Failed to open stream: 
Permission denied in /opt/lampp/htdocs/Project1/15.filehandling.php on line 13
Error creating the file numbers.dat

and my code is =>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File handling</title>
</head>
<body>
    <?php
    $numbers = [1, 2, 3, 4, 5];
    $filename = 'numbers.dat';
    
    $f = fopen($filename, 'wb');
    if (!$f) {
        die('Error creating the file ' . $filename);
    }
    
    foreach ($numbers as $number) {
        fputs($f, $number);
    }
    fclose($f);
    ?>
</body>
</html>
0 Answers
Related