Iphone SDK: Upload image from iphone to php server, send an empty file? (sample code code inside)

I am trying to send a photo and GPS location to a server via PHP

here is the part of PHP: copy here Saving the downloaded file

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

Temporary copied files disappear when the script ends. To save the downloaded file, we need to copy it to another 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 above script checks if the file exists, if it does not exist, it copies the file to the specified folder.

Note. In this example, the file is saved in a new folder named "upload"

Now back to the iOS part, I can send the GPS location to the server, it's just because it's just a string.

and you can download the sample code here

But there is no image, I do a search and found this discussion

I directly put the code in my GPS & Image Submit button

Best of all, it could be feedback. I'm sending a file or failing

this is the biggest message I received after sending the photo to the server

1. Invalid file

2.Back Code:
Download:
A type:
Size: 0 Kb
Temp file:
Saved in: upload /

The following guide to saving an image to NSData p>

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

and I also check if there is data inside or not

so just add this code to the button action

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

It works great .......

But how to upload the image to the server ???

Maybe I need to specify a file name and then upload it.

but how...

I never use the camera function or upload data before

Hope someone can understand this problem

Thank you very much!

+4
source share
3 answers

use HTTP multipart POST or ASIHTTPRequest


 @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]; 
+2
source

It's great to add this.

 NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"]; UIImage *img = pickerImage; [UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES]; 

But when I try to apply a button action

 NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"]; request = [ASIFormDataRequest requestWithURL:webServiceUrl]; [request setPostValue:requestString forKey:@"data"]; [request setFile:photoPath forKey:@"apiupload"]; [request startSynchronous]; 

He got a lot of error messages, I think this is a delegation problem

the first compiler says: "request undeclared" and "ASICormDataRequest uneclared"

and "requestString uneclared"

I am trying to provide a request and requestString declared

Therefore, I use ASIFormDataRequest *request; and NSString *requestString;

Clarify the error "equestString undeclared"

I do not know how to declare ASIFormDataRequest, so this is still a mistake ...

Did I write this correctly in IBAction ???

I look at an example here

Thanks again ~ you're a really nice guy!

More Edit:

OK, here is another step in how to import the necessary .hm and frames

download here

and import this header:

 ASIHTTPRequestConfig.h ASIHTTPRequestDelegate.h ASIProgressDelegate.h ASICacheDelegate.h ASIInputStream.h ASIInputStream.m ASIHTTPRequest.h ASIHTTPRequest.m ASIFormDataRequest.h ASIFormDataRequest.m ASINetworkQueue.h ASINetworkQueue.m ASIDownloadCache.h ASIDownloadCache.m 

IPhone designs should also include:

 ASIAuthenticationDialog.h ASIAuthenticationDialog.m Reachability.h (in the External/Reachability folder) Reachability.m (in the External/Reachability folder) 

than import Framework:

CFNetwork

SystemConfiguration.framework

MobileCoreServices.framework

CoreGraphics.framework

libz.1.2.3.dylib

+1
source

OK, here is a small error between iPhone3 and iPhone4 / touch4

@dhil The answer is almost right, you just need to give the correct key (server dependent)

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

Ok ... now if you are using iPhone3 and you are not giving requestString alloc

It works fine ~

But if you start iPhone4 / touch4, it will call EXC_BAD_ACCESS

even both devices - iOS 4.1

So ... I hope this answer helps you save time uploading images to the server.

0
source

All Articles