0

我在尝试将上传的文件放入正确的目录时遇到了很多麻烦,非常类似于:PHP move_uploaded_file() error? 但是,代码和权限似乎一切都很好。我将本教程用于 Objective-C 部分:http ://www.michaelroling.com/post/upload-an-image-using-objective-c ,下面编写了应用程序和 PHP 脚本的完整实现。然而,问题可能是 Objective-C 从未真正传输图像。如果您需要更多信息,请与我们联系。PHP 返回:

文件上传报告为成功,但实际上并未上传 jpeg。

- (IBAction)shareWithFriends:(id)sender
{

if (imageView.image != nil)
{
    NSData *image = UIImageJPEGRepresentation(imageView.image, 0.1);

    self.fileUploadClient = [[fileUploadClient alloc] initWithHostName:@"shipstudent.com" customHeaderFields:nil];

    NSMutableDictionary *postParameters = [NSMutableDictionary dictionaryWithObject:@"Animal" forKey:@"appID"];

    self.fileOperation = [self.fileUploadClient postData:postParameters path:@"/animal/pictureuploads.php"];

    [self.fileOperation addData:image forKey:@"userfl" mimeType:@"image/jpeg" fileName:@"upload.jpg"];

    [self.fileOperation addCompletionHandler:^(MKNetworkOperation *operation)
    {

        NSLog(@"%@",[operation responseString]);
    }

     errorHandler:^(MKNetworkOperation *errorOperation, NSError *error)
     {

         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];

         [alert show];

     }];

    [self.fileUploadClient enqueueOperation:self.fileOperation];

}
else
{

}

}

PHP:

<?php


if(!empty($_POST)) 
{

    $directory = $_SERVER['DOCUMENT_ROOT'] . '/appphotos';

$file = basename($_FILES['userfl']['upload.jpg']);

$uploadfile = $directory . $file;

    $randomPhotoID = md5(rand() * time());

echo "file=".$file;

//where the error resides
if (move_uploaded_file($_FILES['userfl']['tmp_name'], $uploadfile)) {
       echo 'successful';
    }
    else
    {
    echo 'unsuccessful';

}

}
else
{
    echo('Empty post data');
}

?>
4

1 回答 1

2

从错误中可以看出,您正试图无处写入。你的$uploaddir变量永远不会被设置,并且$file永远是空的,所以你告诉它写到$uploadfile = null . null,试试这个:

if(!empty($_POST)) {

    $directory = $_SERVER['DOCUMENT_ROOT'] . '/appphotos/';
    // $file = basename($_FILES['userfl']['upload.jpg']);
    $file = basename($_FILES['userfl']['name']);
    // $uploadfile = $uploaddir . $file;
    $uploadfile = $directory . $file;
    $randomPhotoID = md5(rand() * time());

    echo 'file=' . $file;

    //where the error resides
    if (move_uploaded_file($_FILES['userfl']['tmp_name'], $uploadfile)) {
        echo 'successful';
    } else {
        echo 'unsuccessful';
    }

} else {
    echo 'empty post data' ;
}
于 2013-12-20T23:08:45.103 回答