PHP failed to create file

Joined
Aug 21, 2023
Messages
40
Reaction score
0
Hi.
I have a simple php
PHP:
<?php // testfile.php
  $fh = fopen("testfile.txt", 'w') or die("Failed to create file");

  $text = <<<_END
Line 1
Line 2
Line 3
_END;

  fwrite($fh, $text) or die("Could not write to file");
  fclose($fh);
  echo "File 'testfile.txt' written successfully";
?>

When I open in browser I get the message "Failed to create file". How can I fix this? Thanks.
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
What if you just execute the first line?
<?php // testfile.php
$fh = fopen("testfile.txt", 'w') or die("Failed to create file");
?>
 
Joined
Sep 4, 2022
Messages
136
Reaction score
16
Hello !


PHP:
<?php

$fh = fopen("testfile.txt", 'w+') or die("Failed to create file");

// as 'w' is not enough, use 'w+' . it includes the creation of the file if needed.

?>



when you need informations about one function part of php,
reach the 'official website' at : www.php.net
 
Joined
Aug 21, 2023
Messages
40
Reaction score
0
Hello !


PHP:
<?php

$fh = fopen("testfile.txt", 'w+') or die("Failed to create file");

// as 'w' is not enough, use 'w+' . it includes the creation of the file if needed.

?>



when you need informations about one function part of php,
reach the 'official website' at : www.php.net
I still get the message "Failed to create file" . I even tried adding chmod("testfile.txt", 0600); but get the same message.
 
Joined
Dec 10, 2022
Messages
108
Reaction score
26
You could try this. Will create the file in the same directory as executing script.
PHP:
<?php
$fh = fopen(dirname(__FILE__) . "/test.txt","w+") or die('Error');
if($fh) {
    echo 'File Created';
}
else {
    echo 'File not Created';
}

?>
 
Joined
Aug 21, 2023
Messages
40
Reaction score
0
You could try this. Will create the file in the same directory as executing script.
PHP:
<?php
$fh = fopen(dirname(__FILE__) . "/test.txt","w+") or die('Error');
if($fh) {
    echo 'File Created';
}
else {
    echo 'File not Created';
}

?>
Doesn't work; get some error message.
 
Joined
Aug 21, 2023
Messages
40
Reaction score
0
I am running it on apache server(XAMPP) localhost. I tried the code (original) online on a site that I use for practice and it works fine. Also when I enter php -v in Terminal it produces nothing.
 
Joined
Sep 3, 2023
Messages
36
Reaction score
2
I don't know XAMPP, and have done that kinda stuff in a long time, but for setting permissions try:




Note also Mac's come with apache & php
So if run a command from the terminal you'll need to append the /Applications/XAMPP.app/... path on to it, otherwise you'll probably get the one in usr/bin/
 
Joined
Aug 21, 2023
Messages
40
Reaction score
0
I
I don't know XAMPP, and have done that kinda stuff in a long time, but for setting permissions try:




Note also Mac's come with apache & php
So if run a command from the terminal you'll need to append the /Applications/XAMPP.app/... path on to it, otherwise you'll probably get the one in usr/bin/
I switched to AMPPS and it works fine. Something went wrong with the XAMPP installation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top