-1

これは、php を使用して Mysql に画像をアップロードするための私の xcode コードです。NSString *urlString = @"http://www.mysullurpeta.com/iphone/upload.php";

    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"world1.jpeg"],90);
    NSLog(@"imageView in data %@/n>>>>%d kb",imageData,imageData.length);


    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];
 NSLog(@"post data is %@", contentType);
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    [request setHTTPBody:imageData];
 NSLog(@"post data is %@",request);

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"Data ---- %@", str);

------そして、これは私のPHPスクリプトです

<?php
echo "file from client: " . $_FILES["filename"]["name"]; 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

画像を「upload/」ファイルに移動しようとしています。無効なファイルのエラーが表示されます。どこを間違えたのか、わかりません。お願いします...私を助けてください...ありがとう

4

2 に答える 2

1

この関数を使用して、データベースではなくサーバーのフォルダーに uiimage をアップロードします

+(NSString *)uploadImage:(UIImage *)img :(NSString *)path :(NSString *)name {

NSLog(@"-----------------------image uploadImage uploadImage uploadImage -------------------------");

 /*
 turning the image into a NSData object
 getting the image back out of the UIImageView
 setting the quality to 90
 */
NSData *imageData = UIImageJPEGRepresentation(img, 90);

// setting up the URL to post to

NSString *urlString=WSURL;

// setting up the request object now
NSMutableURLRequest *_request = [[[NSMutableURLRequest alloc] init] autorelease];
[_request setURL:[NSURL URLWithString:urlString]];
[_request setHTTPMethod:@"POST"];

 /*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type
 You might want to generate a random boundary.. this is just the same
 as my output from wireshark on a valid html post
 */

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

 /*
 now lets create the body of the post
 */



NSString *name1= [NSString stringWithFormat:@"%@%@",path,name];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",name1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
//[body appendData:[NSData i:@"path=%@",path]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[_request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:_request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

return(returnString);

}

于 2012-06-04T10:51:31.547 に答える
0

以下を使用して、このコードを使用してmysqlデータベースにレコードを正常にアップロードしました

iPhoneコード

  NSString *post =[[NSString alloc] initWithFormat:@"ProviderNPI=%@&PatientID=%@&FileURL=%@&FileTYPE=%@&DataSynID=%@&AppointmentListingsID=%@",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];



    NSURL *url=[NSURL URLWithString:@"http://www.yourserver.com/Sync.php?"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);

データベースに挿入するための PHP コード

 <?php

 $con = mysql_connect("emriphone.db.6420177.hostedresource.com","emriphone","Light12-");

 if (!$con)


   {

 die('Could not connect: ' . mysql_error());

  }

mysql_select_db("emriphone", $con);

$providernpi=$_POST['ProviderNPI'];
$patientid=$_POST['PatientID'];
$fileurl=$_POST['FileURL'];
$filetype=$_POST['FileTYPE'];
$datasynid=$_POST['DataSynID'];
$appointmentlistingsid=$_POST['AppointmentListingsID'];



  $query=("INSERT INTO AppointmentDataSync   (ProviderNPI,PatientID,FileURL,FileType,DataSyncID,AppointmentListingsID)
VALUES ('$providernpi', '$patientid','$fileurl','$filetype','$datasynid','$appointmentlistingsid')");



mysql_query($query,$con);
printf("Records inserted: %d\n", mysql_affected_rows());


 echo($appointmentlistingsid)


  ?>
于 2012-06-04T11:18:30.387 に答える