0

我正在构建一个 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 !";
 }
4

1 回答 1

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);
于 2014-04-23T10:40:49.827 回答