PHP创建并将txt文件保存到我的文件夹目录

时间:2015-05-05 20:11:34

标签: php

我正在尝试创建并将文件保存到我的站点的根目录,但文件将转到根目录。我想将文件保存到我的文件夹,其名称为allfiles

以下是代码:

$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);

如何将其设置为保存在allfiles文件夹中?

2 个答案:

答案 0 :(得分:0)

只需在文件

之前添加/ allfiles文件夹即可
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/allfiles/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);

答案 1 :(得分:0)

您可以尝试以下方法:

$fileName = "example.txt";
$contents = "put your file content here";
$fileNameWithPath = "your_path".$fileName;
if(file_put_contents($fileNameWithPath,$contents )){
  echo "File ". basename($fileNameWithPath ) ." was successfully created.";
}
else{
  echo "Failed to create file";
}