Php 网络上传图像

Php 网络上传图像,php,ios,objective-c,afnetworking,Php,Ios,Objective C,Afnetworking,我试图从我的iOS应用程序上传一个图像到我的服务器,但是php脚本中的“不成功”一直在欺骗我。我做错了什么 日志返回“文件=不成功”,然后返回二进制文件号 iOS(上传图像): -(iAction)上传照片:(id)发件人{ AFHTTPRequestOperationManager*manager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@]http://SERVERURL"]]; m

我试图从我的iOS应用程序上传一个图像到我的服务器,但是php脚本中的“不成功”一直在欺骗我。我做错了什么

日志
返回“
文件=不成功
”,然后返回二进制文件号

iOS(上传图像):

-(iAction)上传照片:(id)发件人{
AFHTTPRequestOperationManager*manager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@]http://SERVERURL"]];
manager.responseSerializer=[AFHTTPResponseSerializer序列化程序];
NSData*imageData=UIImageJPEG表示法(PickeImage,0.5);
NSDictionary*参数=@{@“消息”:self.descriptionView.text};
AFHTTPRequestOperation*op=[manager POST:@“rest.of.url”参数:参数constructingBodyWithBlock:^(id formData){
//不要像我一样将图像放在参数字典中,而是附加它!
[formData appendPartWithFileData:imageData名称:@“file”文件名:@“upload.jpg”mimeType:@“image/jpeg”];
}成功:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“成功:%@******%@”,operation.responseString,responseObject);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“Error:%@******%@”,operation.responseString,Error);
}];
[行动开始];
[自我解除视图控制器激活:是完成:无];
} 
}
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');
}
?>

不要忘记目录末尾的/,因为您将其与文件名连接起来:

$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';
}

你能试着对目录路径进行硬编码吗?我已经对它进行了硬编码,在目录路径中发现了一个小错误,我确信它现在是正确的,但不能解决问题。服务器是一个ubuntu服务器,不知道这是否是一个问题。@user3423384那么脚本响应什么呢?您没有提到partvar/www/User/core/ios/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';
}