NYCPHP Meetup

NYPHP.org

[nycphp-talk] I am about to pull my hair out over file uploads with cURL. Help!

Anthony Papillion papillion at gmail.com
Wed Feb 16 04:08:34 EST 2011


Hi Everyone,

So I'm working with this new API and doing something that SHOULD be
really, really simple. The API documentation says that I should pass
parameters in a POST call They are:

cmd
session
file_name
message_guid
media

The final variable (media) should contain the binary data of the file
I'm uploading through this call. I'm using cURL and the following code
to perform the call to the API:

post = array("cmd"=>"uploadmessage",
                    "session"=>"$_SESSION[sid]",
                    "message_guid"=>"$msg_guid",
                    "file_name"=>"$file_name.mov",
                    "media"=>"@$target_path",);

                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        curl_setopt($ch, CURLOPT_VERBOSE, 0);
                        curl_setopt($ch, CURLOPT_POST, true);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_URL, $baseURL);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);


                        $response = curl_exec($ch);

A few notes:

1. the file is stored in a subdirectory called 'uploads' which is
directly under the current directory the script is executing in. The
variable $target_path will include the directory name and file name
(which will vary). So an example of a $target_path value might be
/uploads/myfile.mp4

2. Since I realize I *might* need to pass the full path to the file,
I've tried replacing $target_path with something like
/home/anthony/workspaces/appname/uploads/$filename and that didn't work
either.

For some reason, I am simply unable to upload this file! Isn't the code
above *supposed* to work? Can anyone spot what's wrong? I've been
wrestling with this little snippet of code for two days and Googleing my
heart out. Help, please!

Thank You,
Anthony Papillion



More information about the talk mailing list