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

  • SEARCH
  • Home
  • 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 7867195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:38:49+00:00 2026-06-03T00:38:49+00:00

I am using code that i found on here which works great but the

  • 0

I am using code that i found on here which works great but the moment i try to access a file in a sub-directory it just doesn’t want to work.

It gets a file, creates a temp file to write to, then looks for some text in the file and replaces that text with new text, then saves the updated file, then deletes the temp file.

The below works fine:

        $reading = fopen('links.htm', 'r');
        $writing = fopen('links.tmp', 'w+');

        $replaced = false;

        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line,'::template::')) {
            $line = "replacement line!\n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
          rename('links.tmp', 'links.htm');
        } else {
          unlink('links.tmp');
        }

This doesnt work:

        $reading = fopen('path/to/links.htm', 'r');
        $writing = fopen('path/to/links.tmp', 'w+');

        $replaced = false;

        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line,'::template::')) {
            $line = "replacement line!\n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
          rename('path/to/links.tmp', 'path/to/links.htm');
        } else {
          unlink('path/to/links.tmp');
        }

Any suggestions?

The path is absolute i defined it in the code earlier and use it to create file and write files to but when i want the code above to work in the same way it just doesn’t want to.

Also the folders permissions has been set to write and read which also works fine.

Ran the code in the sub folder and works fine but not from the top level directory.

Error reporting is turned off, turned it on now:

The process cannot access the file because it is being used by another process. (code: 32)

  • 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-06-03T00:38:51+00:00Added an answer on June 3, 2026 at 12:38 am

    So after a week of looking around, testing, breaking and recoding a lot i found a simple way to do this.

    Since most of the content is dynamically created as well as the links.htm file, it was quite hard finding where and when the file is accessed since the code accesses about 300 of my clients sites and updates the links file.

    Simple fix:

    $path = "path/on/server/";
    
    $conn_id = ftp_connect("ftp.testftp.com") or die("Could not connect");
    ftp_login($conn_id,"admin","ert456");
    
    $old_file = $path.'links.htm';
    $new_file = $path.'_template.htm';
    ftp_rename($conn_id, $old_file, $new_file);
    
    $source = fopen($new_file, "w");
    $localTarget = "path/to/links.htm";
    
    ftp_fget($conn,$source,$localTarget,FTP_ASCII);
    
        $reading = fopen('path/to/links.htm', 'r');
        $writing = fopen('path/to/_template.htm', 'w+');
    
        $replaced = false;
    
        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line,'::template::')) {
            $line = "replacement line!\n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced) 
        {
          rename('path/to/_template.htm', 'path/to/links.htm');
        } else {
          unlink('path/to/_template.htm');
        }
    
     $local_file = "path/to/links.htm";
    
     ftp_put($conn_id, $path, $local_file, FTP_BINARY);
    
     ftp_close($conn_id);
    

    There is probably easier ways to do this but this is what worked for me, hope it helps someone.

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

Sidebar

Related Questions

I am using a Java code that I found that generates a public and
I have some code that is using SyncEnumerator. As you can see here ,
Below is the code that I'm using in ms access to list the data
I'm using the code found here to make my gridview have clickable rows. The
Here is the code below I am using the remove cells (which have been
I'm trying to parse some css files using a code project parser found here
i'm using that code for encrypt a string with 3DES in IOS and i
I have some code using Lucene that leaves the default conjunction operator as OR,
This is the code that I'm using to generate a DH keypair: KeyPairGenerator keyGen
I am using this code to check that array is present in the HashMap

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.