2011-11-17 103 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