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 7087397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:40:09+00:00 2026-05-28T07:40:09+00:00

I’m a beginner at PHP, and I’m still trying to work out proper file

  • 0

I’m a beginner at PHP, and I’m still trying to work out proper file handling techniques. I’m usually alright with trial and error, but when it comes to deleting and modifying data, I always like to be on the safe side.

I wrote the code below to delete a certain section of a file, but I’m not sure if it will work with larger files or under unforeseen conditions which require experience to code for.

I tested this just now and it did work, but I would like to run it by the more experienced programmers first:

function deletesection($start,$len){
    $pos=0;
    $tmpname=$this->name."tmp.tmp";
    $tmpf=fopen($tmpname,"wb+");
    rewind($tmpf);
    $h=fopen($this->name,"rb");
    rewind($h);
    while(!feof($h)){
        $this->xseek($h,$pos);
        $endpos = $pos+1000;
        if($endpos>$start && $pos<$start+$len){
            $readlen=$start-$pos;
            $nextpos=$start+$len;
        }
        else{
            $readlen=1000;
            $nextpos=$pos+1000;
        }
        fwrite($tmpf,fread($h,$readlen));
        $pos=$nextpos;
    }
    fclose($h);
    unlink($this->name);
    rename($tmpname,$this->name);
}

This is inside a class where the property “name” is the file path.

I’m writing the file 1000 bytes at a time because I was getting errors about the maximum amount of memory being exceeded when testing with files over 30mb.

  • 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-28T07:40:10+00:00Added an answer on May 28, 2026 at 7:40 am

    I had a quick look at your code – seems a bit complicated, also copying the entire file will be less efficient if the section to delete is small in relation to the total filesize…

    function deletesection($filename, $start, $len)
    {
     $chunk=49128;
     if (!is_readable($filename) || !is_writeable($filename) || !is_file($filename)) {
       return false;
     }
     $tfile=tempnam(); // used to hold stuff after the section to delete
     $oh=fopen($tfile, 'wb');
     $ih=fopen($filename, 'rb');
     if (fseek($ih, $start+$len)) {
      while ($data=fgets($ih, $chunk) && !feof($ih) {
         fputs($oh,$data);
      }
      fclose($oh); $oh=fopen($tfile, 'rb'); 
           // or could just have opened it r+b to begin with
      fseek($ih, $start, SEEK_SET);
      while ($data=fgets($oh, $chunk) && !feof($oh) {
         fputs($ih, $data);
      }
     }
     fclose($oh);
     fclose($ih);
     unlink($tfile);
     return true;  
    }
    

    I believe it would also be possible to do this modifying the file in place (i.e. not using a second file) using a single file handle – but the code would get a bit messy and would require lots of seeks (then an ftruncate).

    NB using files for managing data with PHP (and most other languages in a multi-user context) is not a good idea.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.