I am trying to write a function that takes two parameters (the filename and a sting to put inside) that creates a new file, containing the string.
<?php
function writeFile($name, $string) {
$text = $string;
$fh = fopen($name + ".txt", 'w') or die("Could not create the file.");
fwrite($fh, $text) or die("Could not write to the file.");
fclose($fh);
echo "File " . $name . ".txt created!";
}
writeFile("testovFail", "Lorem ipsum dolor sit amet");
if(file_exists("testovFail.txt")) echo "<br>File exists!";
?>
This is what I have so far, the function echos that the file was created, but when I run the IF conditional to check whether the file was created, it returns that it wasn't.
how about using file_put_contents instead?
$current = "John Smith";
file_put_contents("blabla.txt", $current);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With