为什么我不能用PHP写文件?
Hi i have a client connecting to a socket server in php.
I want to empty a particular file after connecting to the server, however, the codes is not giving me any error but it is not writing to the file?
<?php
set_time_limit(0);
//parameters to connect to server
$ip = "127.0.0.1";
$port = "1245";
$data = "";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $ip, $port);
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "");
while (true)
{
$data = "some value here"
socket_write($socket, $data, strlen($data));
$line =@socket_read($socket,2048, PHP_NORMAL_READ);
if($line != "")
{
echo $line."
";
}
}
socket_close($socket);
?>
您好我有一个客户端连接到php中的套接字服务器。 p>
我想在连接到服务器后清空一个特定的文件,但是,代码没有给我任何错误,但它没有写入文件? p> \ n
&lt;?php
set_time_limit(0);
//连接服务器的参数
$ ip =“127.0.0.1”;
$ port =“1245”;
$ data =“”;
$ socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$ result = socket_connect($ socket,$ ip,$ port);
$ myFile =“test.txt “;
$ fh = fopen($ myFile,'w')或死(”无法打开文件“);
fwrite($ fh,”“);
而(true)
{
$ data =“这里有些值”
socket_write($ socket,$ data,strlen($ data));
$ line = @socket_read($ socket,2048,PHP_NORMAL_READ);
if($ line!=“”)
{
echo $ line。“
”;
}
}
socket_close($ socket);
? &gt;
code> pre>
div>
I'd use an absolute path $myFile = "/tmp/test.txt";
to make sure I knew where the file was to be written. Right now with just text.txt it could be written in a variety of places but most likely where the web servers binary is located.
If you'd rather not use an absolute path you can use http://php.net/manual/en/function.getcwd.php and http://www.php.net/manual/en/function.chdir.php to control the current working directory.
I would check the file is writable before trying to open it:
if(is_writable($myFile)) {
$fh = fopen($myFile, 'w');
} else {
die('unable to write to '.$myFile);
}