Jump to content

ftruncate() trying to trim begining and end of file


rubing

Recommended Posts

I am trying to take a file that is 600000 bytes and truncate it so that i only have the middle 250000bytes (trimming begining and end off).  The following code using the ftruncate() function is not working   ???

 

 

$handle=fopen($mp3,'w');
fseek($handle,200000);
ftruncate($handle,250000);

Link to comment
Share on other sites

OK, so I changed my code to the following:

 

$handle=fopen($mp3,'a');
fseek($handle,200000);
ftruncate($handle,260000);

 

Yet this still truncates the file from the beginning till the 260000 byte.  I thought specifying the file pointer with fseek would start the truncate in the middle of the file.  Is there a way to do this in php without using some foreign class?

Link to comment
Share on other sites

Ok I figured out a way to do what i wanted as follows:

 

$mp3string=file_get_contents($mp3,FILE_BINARY,$context=NULL,$offset=200000,470000);
file_put_contents($mp3,$mp3string);
[/code

1.  I read the mp3 file in as a string with my desired offset
2.  Save this new (truncated;offset) file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.