0
我試圖將上傳的文件放到正確的目錄中時遇到了一些麻煩,非常類似於:PHP move_uploaded_file() error?然而,對於代碼和權限來說,一切似乎都很好。我正在使用Objective-C部分的本教程:http://www.michaelroling.com/post/upload-an-image-using-objective-c以及下面的應用程序和PHP腳本的完整實現。但是,問題可能在於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');
}
?>
我發現了一些調試後,但是,它不會寫的圖像即使它說它是成功的。此外,該目錄不會寫任何東西,除非我這樣做:$ directory = $ _SERVER ['DOCUMENT_ROOT']。 '/ appphotos';而不是$ directory = $ _SERVER ['DOCUMENT_ROOT']。 'appphotos /'; – user2793987
這是因爲'$ _SERVER ['DOCUMENT_ROOT']'不會以目錄分隔符結束,所以請使用'/ appphotos /'來代替。另外,是目錄777?如果沒有,從命令行運行'chmod 777/path/to/dir/here/appphotos'。 – ollieread
如果我用/後appphotos文件夾它reutrns錯誤: 警告:move_uploaded_file()以[function.move-uploaded-file]:無法 '的/ tmp/php0dqQSS' 在移動到一個 '/ home/shipstud /的public_html/appphotos /' /home/shipstud/public_html/animal/pictureuploads.php on line
– user2793987