使用PHP函数创建新的txt文件

Geo*_*lov 3 php file function

我正在尝试编写一个函数,它接受两个参数(文件名和放入内部的sting),创建一个包含字符串的新文件.

<?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!";

?>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止,函数回声创建文件,但是当我运行IF条件来检查文件是否已创建时,它返回它不是.

Sol*_*ake 5

如何使用file_put_contents呢?

$current = "John Smith";
file_put_contents("blabla.txt", $current);
Run Code Online (Sandbox Code Playgroud)