duannong1801 2017-12-17 08:55
浏览 29
已采纳

在一个总和中添加一个字符串和一个数字

I want to be able to +1 to $i every page reload. I have come across a very simple issue, that I am struggling to find a solution online.

Heres my code:

$backupNumber = fopen("$v", "r+") or die("Unable to open file!");
$i = fread($backupNumber,filesize("invoices/invoice1/backupN.txt"));

$i = intval($i);
$i = $i + 1;
echo $i;
fwrite($backupNumber,$i);
$a = "invoices/" . $invoiceN . "/backup" . $i;
fclose($backupNumber);

and in the txt file is simply the number '1' to start off with.

The issue occurs when reloading the page when I echo $i it outputs:
2 then 13 then 1214 then 12131215 then 2147483648 etc.
I want it to simple output
2 then 3 then 4 etc

  • 写回答

4条回答 默认 最新

  • 「已注销」 2017-12-17 09:02
    关注

    You append the text file, that is why this is happening.

    My advice is to use file_get_contents and file_put_contents.

    $i = file_get_contents("invoices/invoice1/backupN.txt");
    $i++;
    Echo $i;
    file_put_contents("invoices/invoice1/backupN.txt" $i);
    

    File get and put contents always reads the whole text file.
    I don't think you need to intcast the string, it should work without it.

    The code can be a one liner too. It's messy but compact.

    file_put_contents("invoices/invoice1/backupN.txt", file_get_contents("invoices/invoice1/backupN.txt")+1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图