2011-11-17 107 views
0

好吧,所以继承我的问题,我需要我的用户能够上传视频,视频可以是任何大小,所以我写了一个PHP脚本来处理放置在正确的place..Here文件是我的代码:视频上传,只上传20秒的视频

-(IBAction)upload:(id)sender{ 
[self post:webdata]; 
} 

的Webdata是视频数据,这里是我设置了代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
[self dismissModalViewControllerAnimated:YES]; 
//assign the mediatype to a string 
//check the media type string so we can determine if its a video 
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    webData = [[NSData alloc]init]; 
    webData = [NSData dataWithContentsOfURL:videoURL]; 
    //[self post:webData]; 
    video = 1; 
    upload.hidden = NO; 

    ImagesCopy = [[NSMutableArray alloc]initWithObjects:@"1", nil]; 
[tableview reloadData]; 


} 

好了,所以现在我有所有的Webdata完成,现在我需要通过调用[self post:webdata上传数据上面张贴

- (void)post:(NSData *)fileData 

{

NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:[self saveUserLogin]]; 

int test; 
NSString *string = [array objectAtIndex:3]; 
test = [string intValue]; 
test++; 
NSData *videoData = fileData; 
NSString *urlString = [[NSString alloc]initWithFormat:@"http://www.site.com/members/uploadMovie.php?&username=%@", [array objectAtIndex:0]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100000000000]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
NSString *postName = [[NSString alloc]initWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vid%i.mov\"\r\n", test]; 
[body appendData:[[NSString stringWithString:postName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:videoData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 


//NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
//NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES]; 
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
if (connection) { 
    responceData = [NSMutableData data]; 

} 
else{ 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Well something went wrong, make sure you have a valid internet connection and try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 
// NSLog(@"%@", returnString); 

NSArray *values = [[NSArray alloc] initWithObjects: [array objectAtIndex:0],[array objectAtIndex:1], [array objectAtIndex:2], [NSString stringWithFormat:@"%i", test], nil]; 
[values writeToFile:[self saveUserLogin] atomically:YES]; 

}

好了,现在我需要一些方法来调用:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[responceData appendData:data]; 

} 

    -(void)connection:(NSURLConnection *)connection didSendBodyData (NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten 
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ 

if([connecting isAnimating]){ 
    [connecting stopAnimating]; 
[connectingImage removeFromSuperview]; 
[connecting removeFromSuperview]; 
} 
float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; 
float total = [[NSNumber numberWithInteger: totalBytesExpectedToWrite] floatValue]; 
progressForUpload.progress = progress/total; 

} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 

NSString* responseString = [[NSString alloc] initWithData:responceData  encoding:NSUTF8StringEncoding]; 
NSLog(responseString); 
progressForUpload.progress = 0; 

[ImagesCopy removeObjectAtIndex:0]; 
[tableview reloadData]; 

if (ImagesCopy.count == 0) { 
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; 

    [self.delegate didFinishController:self]; 
    } 
} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; 
UIAlertView *erroragain = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Something went wrong, this might be a server problem, log out and try again, if its still not working, wait a hour and try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
[erroragain show]; 
} 

所以这段代码运行正常,该视频被上传(如果它不到20秒),现在看看我的php代码:

<?php 


$dbc = mysql_connect('hosting', 'user', 'pass'); 
if(!$dbc){ 
    die('not connected : ' . mysql_error()); 
} 

$db_selected = mysql_select_db("db", $dbc); 
if(!$db_selected){ 
     die("could not connect to DB : " . mysql_error()); 
} 

$username = $_GET['username']; 

$check = mysql_query("SELECT video FROM members WHERE username='$username'"); 

$findMem = mysql_num_rows($check); 

if($findMem > 0){ 
while($row = mysql_fetch_array($check) or die(mysql_error())){ 

$vid = $row['video']; 

$vid = $vid + 1; 

mysql_query("UPDATE members SET video='$vid' 
WHERE username='$username'"); 

$uploaddir = './'.$username.'/default/'; 
$file = basename($_FILES['userfile']['name']); 
$uploadfile = $uploaddir . $file; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo "done"; 


    } 
    else{ 
     echo "error"; 
    } 
} 
} 
?> 

所以我真的没有问题,如果它在20秒内上传视频,它应该能够做更长的权利?

如果有人可以请帮我弄清楚这有什么问题,我需要能够上传更大的视频,我已经尝试在php代码中调用sleep(5)来查看该文件是否需要一些时间来处理,没有工作。

感谢, 雅各

+0

“能够做更长的时间”?请解释 。 – Raptor

回答

1

听起来像是你打任何文件上传大小限制,执行时间限制,内存限制,或磁盘空间。你可以在PHP.ini中改变它们。

而且,你是敞开 SQL注入,你将被黑客入侵,如果你不学习做准备的查询与PDO,或至少逃避你的数据。

+0

是的,你是对的。你可以指出我在正确的方向来确保我的SQL? – Jacob

+0

我已经做了。使用PDO准备好的查询。 – Brad

+0

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html – Brad