doupuzhimuhan9216 2011-08-03 03:13
浏览 14
已采纳

为什么我不能用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);
?>
  • 写回答

2条回答 默认 最新

  • douang1243 2011-08-03 03:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分