Search code examples
phpxampp

Warning: file_put_contents(./aa.txt): failed to open stream: Permission denied - in XAMPP


Im getting this warning when trying to append data to a text file - I've looked around a lot and tried many things, changing permissions (eg. chmod'd to 777 and given www permissions) etc., but nothing I've tried seems to work. I'm using XAMPP 7.1.7, on MacOS.

Wondering if I'm just doing something obviously wrong or haven't setup a part of xampp.

$url = "text";
$filename = "./data.txt" ;
file_put_contents($filename, $url, FILE_APPEND);

Solution

  • Looks like I've found your problem - you don't need to use terminal actually, problem is you need to give read/write access to everyone - I had a similar problem like that and it can be fixed like so.

    If you go to you your directory containing your htdocs, xamppfiles etc. (this is normally by default a folder called 'XAMPP' in Applications), right click on the folder and select 'Get Info'. Another window will pop up and here at the bottom will be a sharing and permissions drop down - from there you can see a table, and next to 'Everyone' select 'Read and Write' in the Privilege dropdown. You can then go ahead an below that select "Apply to all subfolders".

    This should fix it!