I am trying to upload a large video from iphone to a web server with php script.
I use NSInputStream to retrieve fragments of video files, and I create a request (POST) on each crawl
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
with read data passed as a parameter.
Here is the code I use to get chunks of data
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch(eventCode) { case NSStreamEventHasBytesAvailable: { NSMutableData *dataSlice; uint8_t buf[1048576]; unsigned int len = 0; len = [(NSInputStream *)stream read:buf maxLength:1048576]; if(len) { dataSlice = [NSMutableData dataWithBytes:(const void *)buf length:len]; NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:folderNameForUpload, kFolderName, @"abcd.MOV", kFileName, @"MOV", kFileType, nil]; MKNetworkOperation *op = [self.networkEngine operationWithPath:@"upload.php" params:params httpMethod:@"POST"]; [op addData:dataSlice forKey: @"file" mimeType: @"image/mov" fileName: @"abcd"]; [op onCompletion:^(MKNetworkOperation *completedOperation) { } onError:^(NSError *error) { }]; [[WebRequest sharedInstance].networkEngine enqueueOperation: op]; } else { NSLog(@"NO MORE BUFFER!"); } break; } case NSStreamEventEndEncountered: { [stream close]; [stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [stream release]; stream = nil; break; } } }
It sends data to the server, and I can write the pieces to a file. But the problem is that if there is more than one fragment, the file will be damaged and I will not be able to open the video file.
I checked the file size on both the server and the client, and both are exactly the same.
Below is the php script I use to combine fragments of video files.
$tmp_file = $_FILES['file']['tmp_name']; $write_handle = fopen($fileURL, "ab+"); $read_handle = fopen($tmp_file, "rb"); $contents = fread($read_handle, filesize($tmp_file)); fwrite($write_handle, $contents); fclose($write_handle); fclose($read_handle);
What am I doing wrong here ?, Please help!
I am stuck on this problem!
Thanks Advance,
Suraj