How to Upload a Image using POST - Beginner

640 views
Skip to first unread message

shamm

unread,
Feb 13, 2012, 11:42:20 AM2/13/12
to ASIHTTPRequest
Hello everyone,

I have googled this for days, but i am still unable to find a good
example that would be able to send a POST request to a PHP file.

I am using ASIHTTPRequest, and i know how to send the data from the
iPhone end;

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

But i am unable to receive this from the PHP side.

Can someone point me show me a good working example, to send POST from
iPhone and receive the information (Including an image) from the PHP
end

G S

unread,
Feb 13, 2012, 4:20:06 PM2/13/12
to asihttp...@googlegroups.com
Here you go.  This one's designed to accept image files and limit their size, but you get the idea.  It also uses one of the query parameters to tell the server where to put the uploaded file, depending on whether it's a thumbnail or full-sized image.

<?php
$file = basename($_FILES['userfile']['name']);
$payloadType = $_POST['type'];
if($payloadType == "item")
{
$destDir = $itemDirectory;
}
else if($payloadType == "thumb")
{
$destDir = $thumbnailDirectory;
}

$uploadFile = $destDir . "/" . $file;

if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "Temp file {$_FILES['userfile']['tmp_name']} uploaded.\r\n";
}
else
{
echo "Temp file not uploaded.\r\n";
}

if ($_FILES['userfile']['size'] > 1000000)     //Limiting image to 1 MB.
{
exit("Your file is too large."); 
}

if(!($_FILES['userfile']['type'] == "image/jpeg") &&
!($_FILES['userfile']['type'] == "image/png"))
{
exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you uploaded."); 
}

printf("Copying to {$uploadfile}\n");

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile);

?>

Hal Mueller

unread,
Feb 13, 2012, 6:14:11 PM2/13/12
to asihttp...@googlegroups.com
Thanks for that, GS! I need something similar to support some student projects--you just saved me an afternoon.

Hal

G S

unread,
Feb 13, 2012, 6:20:23 PM2/13/12
to asihttp...@googlegroups.com
On Mon, Feb 13, 2012 at 3:14 PM, Hal Mueller <halmu...@gmail.com> wrote:
Thanks for that, GS! I need something similar to support some student projects--you just saved me an afternoon.


You're welcome!  Just double-check all the permissions on your directories; I wasted boatloads of time trying to figure out what was wrong, only to find that weird permissions were inherited from the PHP user on the server or something.  I have a version of that script that tries to create the destination directory if it doesn't exist, and chmod its permissions, and I still don't know if it works right.

Gavin

Shamil

unread,
Feb 13, 2012, 6:28:34 PM2/13/12
to asihttp...@googlegroups.com
Hello,

How do i limit the size of the image to 1 MB while uploading from the iPhone ?

--
You received this message because you are subscribed to the Google Groups "ASIHTTPRequest" group.
To post to this group, send email to asihttp...@googlegroups.com.
To unsubscribe from this group, send email to asihttpreques...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asihttprequest?hl=en.

Shamil

unread,
Feb 13, 2012, 6:31:02 PM2/13/12
to asihttp...@googlegroups.com
Hello Gavin,

Could you also help me with writing the code to upload the image. As in i am not sure of the header files that i should include

G S

unread,
Feb 13, 2012, 7:00:49 PM2/13/12
to asihttp...@googlegroups.com

On Mon, Feb 13, 2012 at 3:31 PM, Shamil <shae...@gmail.com> wrote:
>
> Hello Gavin,
>
> Could you also help me with writing the code to upload the image. As in i am not sure of the header files that i should include
>

I ended up creating a subclass of ASIFormDataRequest, which itself is derived from NSOperation (through ASIHTTPRequest).  I then add this request to an NSOperationQueue that I create.

You don't have to create a subclass, but you can do what mine does: call ASIFormDataRequest's initWithURL and setData methods:

[super initWithURL:[NSURL URLWithString:URLString]];

[self setData:data withFileName:[@"theFileName"]
    andContentType:@"image/jpeg"
    forKey:@"userfile"];


In my case the NSData (for setData) comes from a UIImage:

NSData* imageData = UIImageJPEGRepresentation(uploadImage, .6);


As far as limiting the size of the file on the phone, before the user uploads it, I don't know.  I'm sure NSData has a way to check its size.  I also don't know if ASIHTTPRequest has its own operation queue you can use.  Unfortunately I don't have time to dig up any more information, so check out the documentation.

Good luck!

Gavin

yalamandarao inaganti

unread,
Feb 14, 2012, 12:23:06 AM2/14/12
to asihttp...@googlegroups.com
hai sir,

This is yalamandarao ...

How to download pdf file and store in iPhone ? is there any code.... ?

Iphone application...

yalamandarao inaganti

unread,
Feb 14, 2012, 12:42:27 AM2/14/12
to asihttp...@googlegroups.com
This is XML File i need to access image in my iPhone application. i
got title and link. so plz tell me how to get image in objective c?


<item>
<title>Macworld Australian - February 2012</title>
<guid isPermaLink="false">http://downmagaz.ws/computer_magazine/7956-macworld-australian-february-2012.html</guid>
<link>http://feedproxy.google.com/~r/DownloadMagazine/~3/KBmxzdVfSPQ/7956-macworld-australian-february-2012.html</link>
<description>&lt;div
align="center"&gt;&lt;!--dle_image_begin:http://downmagaz.ws/uploads/posts/2012-02/1329164211_macworld.jpg|--&gt;&lt;img
src="http://downmagaz.ws/uploads/posts/2012-02/1329164211_macworld.jpg"
alt="Macworld Australian - February 2012" title="Macworld Australian -
February 2012" /&gt;&lt;!--dle_image_end--&gt;&lt;/div&gt;&lt;br
/&gt;&lt;div align="center"&gt;&lt;b&gt;Macworld Australian - February
2012&lt;/b&gt;&lt;br /&gt;English | 84 pages | PDF | 59
MB&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Australian
&lt;b&gt;Macworld&lt;/b&gt; is the longest-running Mac magazine
outside the USA. We bring you the latest news, reviews, help and tips
for the Mac, iPad, iPhone and everything else from Apple. Plus you'll
find photography, lifestyle and the latest gadgets. It's the full
package for Mac fans.&lt;img
src="http://feeds.feedburner.com/~r/DownloadMagazine/~4/KBmxzdVfSPQ"
height="1" width="1"/&gt;</description>
<category><![CDATA[Computer, Digital]]></category>
<dc:creator>Revlis</dc:creator>
<pubDate>Mon, 13 Feb 2012 23:25:20 +0300</pubDate>
<feedburner:origLink>http://downmagaz.ws/computer_magazine/7956-macworld-australian-february-2012.html</feedburner:origLink></item>

Reply all
Reply to author
Forward
0 new messages