【问题标题】:My upload image code in IOS and PHP doesnt work我在 IOS 和 PHP 中的上传图片代码不起作用
【发布时间】:2014-01-11 17:52:10
【问题描述】:

我有问题。我一直在这里查看其他答案,但无法获得解决方案。我的代码似乎工作,(日志给我我的图像已上传到服务器)但是当我打开服务器时没有任何收获。你能帮帮我吗?我要疯了!谢谢

这是我的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'] ;
}
?>

【问题讨论】:

  • 你为此做了哪些调试?你的move_uploaded_file()返回是真的吗?
  • 文件是否对上传目录有写权限?

标签: php ios image upload uiimagepickercontroller


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2015-10-14
    • 2017-05-05
    • 2015-07-27
    • 1970-01-01
    • 2023-03-08
    • 2023-01-02
    相关资源
    最近更新 更多