PHP file_get_contents代码不工作,只是创建一个空白文件

时间:2013-11-13 16:41:03

标签: file-get-contents

我正在构建一个php文件,它将文件写入服务器但它无法正常工作,它只是创建文件,有什么帮助吗?

$htaccess = "http://www.yellowpages.com.lb/UserFiles/File/tls/htaccess.txt";
$file = file_get_contents($htaccess);
$open = fopen("tools/.htaccess" , 'w');
fwrite($open,$file);
fclose($open);
 if($open) {
     echo "<br> [htaccess] => Has Been Created !";
 } else {
     echo "<br>[-] Error !";
 }

1 个答案:

答案 0 :(得分:0)

您尝试写入服务器的文件有多大?我有同样的问题,它只会写文件而不是写数据,因为数据非常大。

我通过在新行上爆炸file_get_contents返回的变量来解决这个问题。循环遍历数组并将每一行写入文件。

所以:

//Get the contents of the file
$string = file_get_contents($localDirectory . $fileName);
//Create the file on the server as appendable type
$stream = fopen("ssh2.sftp://{$this->subCon}/." . $remoteDirectory . $fileName, 'a+');
//Split the file by new line characters
$stringArray = explode("\n", $string);
//Loop through the array and write each line to the file
foreach ($stringArray as $line)
{
  fwrite($stream, $line);
}
//Close the file
fclose($stream);