Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:14:02+00:00 2026-05-23T21:14:02+00:00

When a user upload a file(users can upload multiple files) exec(‘nohup php /main/apache2/work/upload/run.php &’);

  • 0

When a user upload a file(users can upload multiple files)

exec('nohup php /main/apache2/work/upload/run.php &');

I am using nohup as the it needs to be executed in the back end.

In my original design run.php scans the directory using scandir everytime it’s executed. Get an exclusive lock LOCK_EX on the file using flock and use LOCK_NB to skip the file if it has a lock and go the next one. If a file has a lock //Do logic. The problem is that the server is missing fcntl() library and since flock uses that library to execute the locking mechanism, flock won’t work at the moment. It’s going to take a month or two to get that installed(I have no control over that).

So my work around for that is have a temporary file lock.txt that acts a lock. If the filename exists in lock.txt skip the file and go to the next one.

$dir = "/main/apache2/work/upload/files/";
$files = scandir($dir);
$fileName = "lock.txt";

for($i=0; $i<count($files); $i++) 
{   
    if(substr(strrchr($files[$i],'.csv'),-4) == '.csv')
    {           
        if($file_handle = fopen("$fileName", "rb"))
        {
            while(!feof($file_handle))
            {
                $line = fgets($file_handle);
                $line = rtrim($line);
                if($line == "")
                {
                    break;
                }
                else
                {
                    if($files[$i] == $line)
                    {
                        echo "Reading from lock: ".$line."</br>";
                        $i++; //Go to next file
                    }
                }

            }
            fclose($file_handle);
        }

        if($i >= count($files))
        {
            die("$i End of file");
        }

        if($file_handle = fopen("$fileName", "a+"))
        {
            if(is_writable($fileName))
            {               
                $write = fputs($file_handle, "$files[$i]"."\n");
                //Do logic

                //Delete the file name - Stuck here

                fclose($file_handle);
            }

        }

    }
    else
    {
        //Do nothing
    }
}

How can I delete the filename from lock.txt?

More importantly, is there a better way to lock a file in php without using flock?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:14:03+00:00Added an answer on May 23, 2026 at 9:14 pm

    Having a shared lock database simply moves the locking problem to that file; it doesn’t solve it.

    A much better solution is to use one lock file per real file. If you want to lock access to myFile.csv then you check file_exists('myFile.csv.lock') and touch('myFile.csv.lock') if it doesn’t exist. And unlink('myFile.csv.lock') when done.

    Now, there is a possible race-condition between file_exists() and touch(), which can be mitigated by storing the PID in the file and checking if getmypid() is indeed the process holding the lock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form where users can enter multiple images to upload along with
I have implemented a simple file upload-download mechanism. When a user clicks a file
I'm creating an online game in PHP where users can create playable characters. Each
So I am using the Uplodify plugin to allow users to upload multiple images
I am looking a easy way for the user to upload multiple images to
Where can I find a silverlight 3 or 4 user control that contains multiple
I'm trying to do the following: User goes to web page, uploads XLS file
I have an application where I need the user to upload a photo. After
How to make the browser upload and display image after the user puts it
I'm developing a webapp where the user is given the chance to upload his

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.