0

http サーバーへのファイルのアップロード中にエラーが発生しました。私が使用しているコードは次のとおりです。

    UIImage * ourImage = [UIImage imageNamed:@"tux.png"];
    NSData * imageData = UIImageJPEGRepresentation(ourImage, 1.0);  

    NSString *urlString = @"http://www.site.com/imageupload.php";      
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];


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



    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"userfile\"; filename=\".png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Text parameter1
    NSString *param1 = @"parameter text";
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Another text parameter
    NSString *param2 = @"Parameter 2 text";
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body
    [request setHTTPBody:body];

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

    NSLog(@"%@", returnString);    

これは、サーバーの public_html/ ディレクトリにある PHP ファイルです。ファイル名は imageupload.php です。ログイン名やパスワードを使用せずに、HTTP 経由で画像ファイルをアップロードしようとしています。私はPHPに慣れていないので、本当に助けが必要です。ありがとう。

    <?php
     $uploaddir = '/public_html/upload';
     $file = basename($_FILES['userfile']['name']);
     $uploadFile = $file;
     $randomNumber = rand(0, 99999);
     $newName = $uploadDir . $uploadFile;
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "Temp file uploaded. \r\n";
    } else {
    echo "Temp file not uploaded. \r\n";
   }

   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.iroboticshowoff.com/dir/{$file}" . "\r\n" .  $_FILES['userfile']
         ['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }
    ?>

これは、コードを実行したときにサーバーから返されるエラーです。

2012-07-31 10:04:24.576 FTP6[3600:f803]
PHP エラー メッセージ
解析エラー: 構文エラー、予期しない T_STRING in /home/a5054180/public_html/imageupload.php2

無料 Web ホスティング

さらに、その他のエラー。誰でも試してみて、それを修正するために何ができるか教えてください。どうもありがとうございました。

4

1 に答える 1

0

スクリプトをテストしていませんが、Content-Disposition と Content-Type を次のように変更する必要があると思います。

Content-Disposition:フォームデータ; 名前="ユーザーファイル"; filename="front.png" Content-Type:画像/png

チャールズという番組を知っていますか?驚くほど役に立ちます。これはプロキシであり、リクエストとサーバーの応答を分析するのに役立ちます。

http://www.charlesproxy.com/

于 2013-01-11T17:58:18.627 に答える