我试图通过我的iOS应用程序将图像上传到我的服务器,但是"不成功"在PHP脚本中继续欺骗。我做错了什么?
Log
返回" File = Unsuccessful
"然后是二进制文件号。
iOS(上传图片):
-(IBAction)uploadPhoto:(id)sender{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://SERVERURL"]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSData *imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
NSDictionary *parameters = @{@"message": self.descriptionView.text};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:@"file" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
PHP代码:
<?
if(!empty($_POST))
{
$message = $_POST['message'];
$directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures';
$file = basename($_FILES['userfile']['upload.jpg']);
$uploadfile = $directory . $file;
var_dump($_FILES);
$randomPhotoID = md5(rand() * time());
echo 'file='.$file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo 'successful';
}
else
{
echo 'unsuccessful';
}
}
else
{
echo('Empty post data');
}
?>
答案 0 :(得分:0)
不要忘记目录末尾的 / 因为你用文件名连接它:
$directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures/';
如果您仍然遇到问题,请尝试添加更多支票:
<?php
if(isset($_POST)) {
$message = $_POST['message'];
if(is_uploaded_file($_FILES['userfile']['tmp_name']){
//we got something, set it up
$directory = $_SERVER['DOCUMENT_ROOT'] . '/pictures/';
$file = basename( $_FILES['userfile']['name']);
$uploadfile = $directory . $file;
$randomPhotoID = md5(rand() * time());
//perform the upload
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo 'successful';
} else {
echo 'unsuccessful';
}
}else{
echo "Nothing was uploaded";
}
}else{
echo 'POST is not set cannot proceed to upload';
}