早上好,
我尝试使用Objective-C和PHP文件上传图像,但我得到了一个"注意:未定义的索引:userfile"来自我的PHP。我尝试了很多事情,但我不知道我的错误在哪里,这就是我需要你帮助的原因。我将发布我的代码。
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:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"imagen.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:[[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];
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
输出错误:
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文件:
<?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. \r\n";
}
else
{
echo "Temp file not uploaded. \r\n";
}
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}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
?>
你能帮助我吗?我不知道错误在哪里。
更多输出错误:
快照未呈现的视图会导致空快照。确保在屏幕更新后快照或快照之前,您的视图至少呈现过一次。
提前致谢。