dongzhan5943 2015-01-18 10:50
浏览 52

注意:未定义索引:用PHP文件的用户文件Objective-C

Good morning,

I'm trying to upload an image using Objective-C and PHP file but I'm getting a "Notice: Undefined index: userfile" from my PHP. I have tried a lot of thing and I don't know where is my mistake, that's why I need your help. I'm going to my post my code.

CamViewController.m

- (IBAction)uploadPhoto:(UIButton *)sender {

    NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 90);
    NSString *urlString = @"http://www.mywebsite.com/upload.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"imagen.jpg\"
" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(returnString);

}

CamViewController.h

@interface CamViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:  (UIButton *)sender;

- (IBAction)selectPhoto:(UIButton *)sender;

- (IBAction)uploadPhoto:(UIButton *)sender;

@end

Output error:

2015-01-18 09:54:46.085 App[2213:1205076] Array
(
)

Notice: Undefined index: userfile in /var/www/vhosts/mywebsite.com/httpdocs/upload.php on line 31

Notice: Undefined index: userfile in /var/www/vhosts/mywebsite.com/httpdocs/upload.php on line 34
Temp file not uploaded. 


Notice: Undefined index: userfile in /var/www/vhosts/mywebsite.com/httpdocs/upload.php on line 43

Notice: Undefined index: userfile in /var/www/vhosts/mywebsite.com/httpdocs/upload.php on line 43

Notice: Undefined index: userfile in /var/www/vhosts/mywebsite.com/httpdocs/upload.php on line 45
Incorrect file type.   is the file type you uploaded.

PHP file:

<?php
print_r($_REQUEST);
error_reporting(E_ALL);
ini_set("display_errors", 1);

$uploaddir = '/images/';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
    echo "Temp file uploaded. 
";
}
else
{
    echo "Temp file not uploaded. 
";
}

if ((!($_FILES['userfile']['type'] == "image/jpeg")) && (!($_FILES['userfile']['type']    == "image/png")))           
{
    exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you uploaded."); 
}


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $postsize = ini_get('post_max_size');   
    $canupload = ini_get('file_uploads');    
    $tempdir = ini_get('upload_tmp_dir');   
    $maxsize = ini_get('upload_max_filesize');
    echo "http://www.mywebsite.com/images/{$file}" . "
" . $_FILES['userfile']['size'] . "
" . $_FILES['userfile']['type'] ;
}
?>

Can you help me? I don't know where is the mistake.

More output error:

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

Thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器