我在IOS和PHP上的上传图像代码不起作用

时间:2013-12-23 13:41:23

标签: php ios image upload uiimagepickercontroller

我有问题。我一直在寻找其他答案,但无法得到解决方案。我的代码似乎工作,(日志给我的图像已经上传到服务器)但是当我打开服务器没有获得任何东西。可以帮助我?我疯了!谢谢

这是我的ios代码.m:

- (IBAction)pushUpload:(id)sender {
    NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90);
    NSString *urlString= @"http://www.myserver/test/upload.php";
    NSMutableURLRequest *request =[[[NSMutableURLRequest alloc]init]autorelease];
    [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:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\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]autorelease];
    NSLog(returnString);
}
@end

这是我的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";
} else {
    echo "Temp file not uploaded. \r\n";
}

if ($_FILES['userfile']['size']> 300000000000) {
    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://www.myweb.com/dir/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}
?>

1 个答案:

答案 0 :(得分:0)

好的,这是一个愚蠢的权限问题服务器文件夹。 谢谢@AustinAllover!