好吧,所以继承我的问题,我需要我的用户能够上传视频,视频可以是任何大小,所以我写了一个PHP脚本来处理将文件放在正确的位置..这是我的代码:
-(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 :(得分:1)
听起来您要达到文件上传大小限制,执行时间限制,内存限制或磁盘空间。您可以在PHP.ini中更改这些内容。
此外,如果您没有学习使用PDO进行准备好的查询,或者至少逃避您的注意,那么对SQL注入将