cURL Image Upload

software development

#1

I need to be able to upload images for a cms application hosted on DreamHost. File access is denied for PHP so I have to use cURL. Unfotuantly I don’t know this language. I was wondering if someone could give me an example of how to have the user use form objects to browse their computer and pick a file to upload, then run a curl script to upload that file to my hosted space … appreciate the help in advance.


#2

There are example scripts on the PHP.net site:
http://us3.php.net/manual/en/ref.curl.php#45491
http://us3.php.net/manual/en/ref.curl.php#62274


Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#3

hrm … i tried the examples and I’m having difficulties … I got a script like this …

<?php $post_data = $file_name; $ext = substr($post_data, -3, 3); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/cmsuploads/images/".$nameoffile.".".$ext ); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $postResult = curl_exec($ch); if (curl_errno($ch)) { print curl_error($ch); } curl_close($ch); print "$postResult"; ?>

but it tells me this:

"Not Found
The requested URL /cmsuploads/images/test.jpg was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. "

Does the file have to exist in order to upload it?


#4

And if the file does exist I get this…

Method Not Allowed
The requested method POST is not allowed for the URL /cmsuploads/images/test.jpg.


#5

You haven’t thought out the problem yet.

  1. The browser must submit form data to your PHP script using the POST method.
  2. Your PHP script must obtain the image file data from the form.
  3. Your PHP script must either POST the image file data to a CMS script, or otherwise use FTP to login to the server hosting the CMS to upload the image file data.

Now that you know what the problem is, you can work on the solution…

:cool: Perl / MySQL / HTML+CSS


#6

okay … what is CURLOPT_URL supposed to be set too? I think that is where I am getting confused. Is that the destination URL … or a call to another script that is supposed to do somethign else that I am missing? I have a hard time finding documentation on what these different curl options are supposed to do. Some are selfexplanatory others are not. The documentaion on php.net is incomplete to say the least so any explanation would be helpful.

just for reference right now I have,

csmadd.php:

<FORM ACTION="imageupload.php" method="post">
File Location: <INPUT NAME="file_name" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File">

</FORM>

imageupload.php:

<?php

$post_data = array();

$post_data['file_name'] = "@cat.jpg";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mydomain.com/cmsuploads/images/" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);

if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
print "$postResult";
?>

my error is now:

“failed creating formpost data”


#7

I am willing to pay some money to someone who will help me write and get this script functioning the way I want it to be. And can explain it to me so I can do it in the future. Contact me via Email adam AT hungrydesigners DOT com if you are interested in making a few bucks for something that is supposively easy … but escapes me. I’ll get back to you on Monday at the latest. You can also IM me if you see me on.

msn: adam_c_mills AT hotmail DOT com
aim: RookieAbuse
yahoo: kungfuBananaSlug

I do appreciate help so far, and any help in the near future.


#8

Hi. I noticed on your form that you don’t have the MAX_FILE_SIZE field. Php requires it. I just had this trip me up earlier this week because I set the value to “30000” and tried to upload a 65KB image.

Try adding that. Syntax is:

Also, I wasn’t paying attention, but you also have to set the form POST to enc-type=FILE.

I hope that helps you.

================================
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com


#9

appreciate the response. The issue I was having is that cURL is used to change something that exist … I needed to create a temporary file … make a copy of it and rename it. But despite what support at DreamHost told me about access permissions I managed to get the upload script working without the use of cURL, straight php … and it works great. Thank you for the reply.


#10

You only need to use cURL if you are accessing an external URL, or any address preceeded with a protocol (like http://). If you are accessing something that is already on your server, then you are probably best using an absolute (relative from root) path, which is always preceeded by a forward slash.


Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#11

I’ve been using straight PHP for image uploads for years. I mentioned the MAX_FILE_SIZE field because you said you were getting messages that the file size was 0.

In any case, I’m glad you figured it out. :slight_smile:

================================
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com