Upload large video from iphone to web server

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

+2
source share
1 answer

I got a problem guys. In fact, I was sending different pieces of video at the same time. And the problem arose because later video fragments reached the server before the first video fragment.

I solved the problem by sending the second video fragment only after the first piece reached the web server and the response was received on the client side.

0
source

Source: https://habr.com/ru/post/944873/


All Articles