我在尝试将上传的文件放到正确的目录中时遇到了很多麻烦,非常类似于: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');
}
?>
答案 0 :(得分: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' ;
}