使用 Xcode 6 和 PHP 文件将图像上传到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26358962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload image to server using Xcode 6 and PHP file
提问by Ahmed Alkaabi
I am trying to send image to my web site depending on user choice, every time the app should give the image's name which has been chosen by the user.
我试图根据用户的选择将图像发送到我的网站,每次应用程序都应该提供用户选择的图像名称。
I am using Xcode 6 and the code below:
我正在使用 Xcode 6 和以下代码:
NSString *urlString = @"http://MyWebSite.com/sendImages.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image,90);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:returnString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
And her is my PHP file:
她是我的 PHP 文件:
<?
$myparam = $_FILES['userfile']; //getting image Here //getting textLabe Here
$target_path = "khadamati/";
if(move_uploaded_file($myparam['tmp_name'], $target_path . basename( $myparam['name'])))
{ echo "The file ". basename( $myparam['name']). " has been uploaded"; }
else
{ echo "There was an error uploading the file, please try again!"; }
?>
How can I change the myImage name in this line:
如何更改此行中的 myImage 名称:
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
I tried to find any thin that would help me, but I could not, so please find the mistake in my booth code.
我试图找到任何对我有帮助的薄,但我找不到,所以请在我的展位代码中找到错误。
采纳答案by Devang Goswami
You should try this ,
你应该试试这个
uname,datestring ( i used here to define the user who has uploaded and the time when he uploaded ).
uname,datestring(我在这里用来定义上传的用户和他上传的时间)。
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@receipt%@.jpg\"\r\n", uname,datestring]] dataUsingEncoding:NSUTF8StringEncoding]];
i hope it will be helpful to you , uname and datestring both are strings !
我希望它对你有帮助,uname 和 datestring 都是字符串!