douxi3404 2015-01-29 17:42
浏览 29

too long

I am working on a PHP web application, my first web application. One of the processes needs to copy some text files to a destination directory. In the destination directory it will open the text files and show the output in the web browser. I am wondering what happens if two users at the same time initiate the process? Since the files MUST be copied to destination directory and read from there. Do I create a unique destination directory each time? How is this properly done?

  • 写回答

1条回答 默认 最新

  • dongzhan2461 2015-01-29 17:55
    关注

    You could create a unique directory, it all depends on what your goal is. Do you want to allow two different users to execute it at the same time?

    Databases are usually useful in these situations because they are made for concurrent activity and feature table or even row locking.

    In this case if you wanted to prevent a second user from running the process at the same time, you could use a lock file:

    1. Create a lock file if it doesn't exist
    2. Run the processes
    3. Unlink the lock file

    If the lock file exists, wait until it is unlinked (deleted), an example:

    while (file_exists('file.lock')) {
       usleep(100000); // sleep 100ms
    }
    touch('file.lock');
    // Execute processes here
    unlink('file.lock');
    

    Lock files are commonly used in filesystems and applications to prevent users from modifying a file at the same time or running more than one instance.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line