My app requires to upload video on click of a button. However, I can't do it with my code. My code for this is as follows:
- (IBAction)uploadVideo {
/* setting up the URL to post to */
NSString *urlString = @"http://172.19.128.170/UploadFile.php";
/* setting up the request object */
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/* setting up the request body*/
NSString *path = [[NSString alloc]init];
path = @"Users/msat/Library/ApplicationSupport/iPhoneSimulator/4.3.2/Media/DCIM/100APPLE/";
NSString *boundary = [NSString stringWithString:@"*****"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@", boundary, @"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"uploadeFfile\"; filename=\"Users/msat/Library/ApplicationSupport/iPhoneSimulator/4.3.2/Media/DCIM/100APPLE/IMG_0001.png""
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"--
"] dataUsingEncoding:NSUTF8StringEncoding]];
// setting up the buffer size
int buffer[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = read([fileHandle fileDescriptor], buffer, BUFFER_SIZE) > 0)) {
[body appendBytes:buffer length:(NSUInteger)bytesRead];
}
[body appendData:[[NSString stringWithFormat:@"buffer"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type:"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@", boundary, @"--
"] dataUsingEncoding:NSUTF8StringEncoding]];
/* setting the body of the post to the reqeust */
[request setHTTPBody:body];
/* setting up the connection to the web*/
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"...this is returned %@", returnData);
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"...this is uploaded %@", returnString);
}
The PHP I am using is as follows:
<?php
// Where the file is going to be placed
$target_path = "./Videos/Unapproved/";
//$description = $_FILES['description']['name'];
//echo "Entered the PHP file!!" . basename( $_FILES['description']['name']);
/* Add the original filename to our target path.
Result is "/tmp/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo "before checking the file!". basename( $_FILES['uploadedfile']['name']);
//echo $_FILES['uploadedfile']['tmp_name'];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
//$conn = mysql_connect("localhost", "root", "demo123") or die(mysql_error());
//mysql_select_db("Upload") or die(mysql_error());
//mysql_query("INSERT INTO User_Upload_Table(video_Name, description, video_Path, duration, status) VALUES('', '', '', '', '')");
//mysql_close($conn);
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo "target_path: " . $target_path;
}
?>
Can somebody tell me where am I making mistake?
What else do I need to do if I want to stream a video and upload it on the server?