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 !!!
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];
It's fine 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 put in my 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];
It got many error message ,I think is delegate problem
first complier says "request undeclared" and "ASIFormDataRequest undeclared"
and "requestString undeclared"
I try to give request and requestString declared
So I use ASIFormDataRequest *request;
and NSString *requestString;
It clear an error about "equestString undeclared"
I don't know how to declared ASIFormDataRequest so it still a error...
Did I right to put this in IBAction ???
I look some example here
Thanks again~you are a really nice guy !
More Edit :
OK, Here is more step about how to import necessary .h .m and framework
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 projects must 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
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.