我在ios中使用这些代码上传我的图片
与上的另一个代码几乎相同Internet
NSData *uploadImage = UIImageJPEGRepresentation(self.image, 1) ;
NSString *uploadURL = @"http://...../jeff.php" ;
NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] init];
[uploadRequest setURL:[NSURL URLWithString:uploadURL]] ;
[uploadRequest setHTTPMethod:@"POST"] ;
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
[uploadRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition:form-data; name=\"userfile\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:uploadImage]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[uploadRequest setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"echo:%@",returnString);
我的 PHP 是
$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "yes \r\n";
}
else
{
echo "NO \r\n";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "http://...../uploads/{$file}";
}
但它不工作
我的上传文件夹是 /var/www/uploads