点击按钮上传视频

时间:2011-12-05 09:56:05

标签: php objective-c

我的应用需要点击按钮上传视频。但是,我不能用我的代码来做。我的代码如下:

- (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, @"\r\n"] 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""\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"--\r\n"] 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, @"--\r\n"] 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);

}

我使用的PHP如下:

<?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;
}
?>

有人可以告诉我,我在哪里弄错了吗?

如果我想流式传输视频并将其上传到服务器上,我还需要做什么?

1 个答案:

答案 0 :(得分:0)

以下是我用来将文件上传到服务器的代码:

http://pastebin.com/bk7M0rr5

就php而言,代码看起来很好。您可以看到/使用上面的代码或与您的代码进行比较以找到问题

<强>更新

//declaring vars
$username = $_GET['user'];
$status = $_GET['tweet'];
$uploaddir = '/var/www/sounds/';      //Uploading to sounds dir
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$uName = "i-".$username."_".time(); 
$newName = $uName; // . $uploadFile;


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

} else {
    echo "error:100";
}

$caf = $uploaddir . $newName.'.caf';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $caf)) {
//here I added a few lines to convert the audio file to mp3 etc and move it around

//    echo $rvalue;
//this is where you can return something
}

请注意,为了安全起见,我已经更改了一些代码。