views:

85

answers:

4

I try to send a photo and GPS location to server via PHP

here is the PHP part:Copy from here Saving the Uploaded File

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.

The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  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";
  }
?>

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

Note: This example saves the file to a new folder called "upload"

now back to iOS part ,I only can send GPS location to server ,it's easy because just a string.

and you can download sample code here

But an image is not,I do some search and found this discussion

I directly put the code in my send GPS&image button

the best thing is it can feedback I send the file success or failed

this is the most message I got after I send photo to server

1.Invalid file

2.Return Code:
Upload:
Type:
Size: 0 Kb
Temp file:
Stored in: upload/

I follow some guide to save image to NSData

NSData *photoData= UIImageJPEGRepresentation(self.theimageView.image, 1.0);

and I also check there is data inside or not

so simply add this code into Button action

if (photoData == nil) {
    NSLog(@"The photo is nothing !!!");
}
else {
    NSLog(@"Photo inside !!!!");

It works fine.......

But How To Upload An Image To Server ???

maybe I need to give a file name then upload it ???

but how to...

I never use camera function and upload data before

Hope someone can figure out this problem

Many Great Thanks !!!

+1  A: 

use HTTP multipart POST or ASIHTTPRequest

dhii
How do I know where I save the data and the what's the file name ?or path ...I have know idea anything about the data info ,but thanks anyway
WebberLai
I think I need to include included ASIFormDataRequest subclass???
WebberLai
http://allseeing-i.com/ASIHTTPRequest/How-to-use
WebberLai
+1  A: 

@WebberLai

Save the image to home directory: NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"]; UIImage *img = pickerImage;

// Write a UIImage to JPEG with minimum compression (best quality) [UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

request = [ASIFormDataRequest requestWithURL:webServiceUrl];
[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];
[request startSynchronous];
dhii
THANK YOU SO MUCH , I WILL TRY THIS SOON !
WebberLai
I got some error and warning ...I post the result back soon
WebberLai
A: 
WebberLai
I "#import "ASIHTTPRequest.h"and #import "ASIFormDataRequest.h" Still got a warning at ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:UploadPhotoAddress];//warning: incompatible Objective-C types 'struct NSString * const', expected 'struct NSURL *' when passing argument 1 of 'requestWithURL:' from distinct Objective-C type
WebberLai
A: 

OK,Here is a small bug between iPhone3 & iPhone4/touch4

@ dhil's Answer is almost right , You just need to give a right key (depends on Server)

[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];

Ok...now if you use iPhone3 , and you don't give requestString alloc

It runs fine ~

But if you run on iPhone4/touch4 ,It will cause EXC_BAD_ACCESS

even both device is iOS 4.1

So...Hope this answer will help you to save time about upload image to server.

WebberLai