图像上传但不到指定目录

时间:2014-12-04 05:59:38

标签: php mysql xcode image

所以在Xcode中我有以下代码将图像上传到服务器,工作正常。

NSData *imageData = UIImageJPEGRepresentation(imageChosen.image, 90);
NSString *urlString = @"http://serveraddress/Picture.php";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);

Picture.php文件如下所示:

<?php
$uploaddir = './';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999); 
$newName = $uploadDir . $randomNumber . $uploadFile;


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
echo "http://server_address/{$file}"
} else {
echo "Temp file not uploaded. \r\n";
}

if ($_FILES['userfile']['size']> 300000) {
exit("Your file is too large."); 
}



if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size');   //Not necessary, I was using these
$canupload = ini_get('file_uploads');    //server variables to see what was 
$tempdir = ini_get('upload_tmp_dir');   //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "http://server_address/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" .     $_FILES['userfile']['type'] ;
}
?>

我总是收到&#34; Temp文件上传。&#34;然后从is_uploaded_file()的echo语句中"http://serveraddress/.jpg"。但是,move_uploaded_file()方法始终返回false,因此永远不会分配新文件名。这张照片去了哪里?这可能是权限问题吗?

0 个答案:

没有答案