dqfr90979 2014-11-20 11:28
浏览 30
已采纳

在文本中删除条目的Flock没有工作我

I create one function for delete entry in little file in txt format , the problem it´s when i go delete entry show me this message :

Warning: flock() expects parameter 1 to be resource, boolean given in

The Script Function :

<?php
function delete_entry($name_file_db,$id_entry)
{
$fil_del=file("".$name_file_db."");
$fd=fopen("".$name_file_db."","w");

if (flock($fd,LOCK_EX)) 
{
ftruncate($fd,0); 
fputs($fd,"".$fil_del[0]."");
for($de=1;$de<sizeof($fil_del);$de++)
{   
if($de=="".$id_entry."")
{
fputs($fd,"");  
}
else
{
fputs($fd,"".$fil_del[$de].""); 
}   
}
fflush($fd); 
flock($fd, LOCK_UN);
fclose($fd);
}
else
{
if($db_activate_msg_bugs=="si")
{
print "Busy File";
}
}

}
?>

I don´t know why no works , i try differents combinations but continue fail

Thank´s Regards

  • 写回答

1条回答 默认 最新

  • dongyan3018 2014-11-20 12:44
    关注

    $fd=fopen("".$name_file_db."","w") probably didn't open a file. fopen() returns false if it fails:

    Returns a file pointer resource on success, or FALSE on error.


    BTW ftruncate() is needless, fopen() in mode w implicitly truncates the file. This does also break your code. The blocked process is truncating your file! Consider using a dedicated lock file or open the file nondeconstructive (e.g. mode c).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?