Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Xcode 6和PHP文件将图像上载到服务器_Php_Ios_Xcode - Fatal编程技术网

使用Xcode 6和PHP文件将图像上载到服务器

使用Xcode 6和PHP文件将图像上载到服务器,php,ios,xcode,Php,Ios,Xcode,我正试图根据用户的选择将图像发送到我的网站,每次应用程序都应该给出用户选择的图像名称 我使用的是Xcode 6,代码如下: NSString *urlString = @"http://MyWebSite.com/sendImages.php"; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]];

我正试图根据用户的选择将图像发送到我的网站,每次应用程序都应该给出用户选择的图像名称

我使用的是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];
她是我的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!"; } 

    ?>
我试着找到任何对我有帮助的单薄,但我找不到,所以请找出我的展位代码中的错误。

你应该试试这个

uname,datestring(我在这里用来定义上传的用户和他上传的时间)

我希望这会对你有所帮助,
uname和datestring都是字符串

你找到你的解决方案了吗?还有什么疑问吗?
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@receipt%@.jpg\"\r\n", uname,datestring]] dataUsingEncoding:NSUTF8StringEncoding]];