加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:05:21 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试编写一个函数,它接受两个参数(文件名和放入内部的sting),创建一个包含字符串的新文件. ?phpfunction writeFile($name,$string) { $text = $string; $fh = fopen($name + ".txt",'w') or die("Could not create the file."); fwrite($fh,$text) or
我正在尝试编写一个函数,它接受两个参数(文件名和放入内部的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!";

?>

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

解决方法

如何使用 file_put_contents呢?

$current = "John Smith";
file_put_contents("blabla.txt",$current);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读