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); Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/ 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 Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-605863 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 Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-605871 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 Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-605887 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'. Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-605890 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. Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-605893 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? Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-606237 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. Link to comment https://forums.phpfreaks.com/topic/117796-ftruncate-trying-to-trim-begining-and-end-of-file/#findComment-606274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.