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);

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.