rubing Posted August 2, 2008 Share Posted August 2, 2008 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); Quote Link to comment Share on other sites More sharing options...
rubing Posted August 2, 2008 Author Share Posted August 2, 2008 The code above just gives me an empty 250k file Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 are u sure the file is of at least 250000+200000 bytes Quote Link to comment Share on other sites More sharing options...
rubing Posted August 2, 2008 Author Share Posted August 2, 2008 yes the file is at least 584000 bytes Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 Check out fopen() The mode 'w' will truncate the file to zero length. Change the mode to something more appropriate, perhaps 'a'. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 Check out fopen() The mode 'w' will truncate the file to zero length. Change the mode to something more appropriate, perhaps 'a'. exactly testing you gonna point out 'a' or 'w+' are what you are seeking to do this appending. Quote Link to comment Share on other sites More sharing options...
rubing Posted August 2, 2008 Author Share Posted August 2, 2008 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? Quote Link to comment Share on other sites More sharing options...
rubing Posted August 2, 2008 Author Share Posted August 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.