12, October 2008

upload with curl - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: upload with curl
« previous next »
Pages: [1] 2 3 Print

Author Topic: upload with curl  (Read 2648 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« on: Aug 27, 2006, 08:15:43 PM »

Today I played a little bit with Curl (again)

while I have a compleet upload class sometimes I need simple upload functions in custum CMS apps.

This time I have to import a CSV file into a database and I wnat to upload the and remove the file afterwards.
So I thought this must be easy with Curl, but id doesn't like this: All examples are about uploading to a remote server but is this remote also the server where the script is running? It doesn't look like...

finally I found a snippet that match a little bit like waht I need:

Code:
<?php
$postData 
= array();
$postData'file_name' ] = "@test.txt";
$postData'submit' ] = "UPLOAD"// don't know why this is here
      
$ch curl_init();

curl_setopt($chCURLOPT_URL$url ); // what is the $url its where the script is executed   
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
curl_setopt($chCURLOPT_POST);

curl_setopt($chCURLOPT_POSTFIELDS$postData );

$response curl_exec$ch ); 

At the end should $response holding the content of the text file, but it doesn't...

By the way this could be a good example to store uploaded data into a database...


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #1 on: Aug 27, 2006, 10:51:53 PM »

You should replace the $url here :

curl_setopt($ch, CURLOPT_URL, $url );

with the actual url that you are sending the post data.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Aug 27, 2006, 11:43:37 PM »

You should replace the $url here :

curl_setopt($ch, CURLOPT_URL, $url );

with the actual url that you are sending the post data.
I post the data to the same script (PHP_SELF)

so I need add there an url like: http://www.mydomain.com/myuploadform.php Visit through proxy ?


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #3 on: Aug 27, 2006, 11:45:26 PM »

Yes and it will post the data to that url.

Have in mind that the keys to the $postData array are the keys in the $_POST array when you retrieve the data.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Aug 27, 2006, 11:52:03 PM »

... but the file is not inside the post array


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #5 on: Aug 28, 2006, 12:01:15 AM »

The @ sign indicates that it will upload the file (like in a multipart form)

You should check the $_FILES array for the file.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Aug 28, 2006, 12:03:26 AM »

The @ sign indicates that it will upload the file (like in a multipart form)

You should check the $_FILES array for the file.

I will give it try (have to write some bills first) Wink


Last blog : Upload images for usage in TinyMCE
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Aug 28, 2006, 01:13:09 PM »

same result like before Sad

This is my code:
Code:
<?php
if (isset($_POST['Submit'])) {
$postData = array();
$postData['upload'] = "@".$_FILES['upload']['name'];
$postData['Submit'] = $_POST['Submit'];

$ch curl_init();
curl_setopt($chCURLOPT_URL"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); 
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$postData);
if (!$response curl_exec($ch)) {
echo curl_error($ch);
}
curl_close($ch);
print_r($response);
}
?>


and this is my form:
Code:
<form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <div>
    <label for="upload">Select a file...</label>
<input type="file" name="upload" size="30">
    <input type="submit" name="Submit" value="Submit">
  </div>
</form>
the result is this error: "failed creating formpost data"


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #8 on: Aug 28, 2006, 01:23:58 PM »

Try to send a file without using the $_FILES array at the beginning, to see if it works.

eg. $postData['upload'] = "@myfile.txt";

then in the url that you post type something like:

var_dump ( $_FILES );

Is it working that way?


Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Aug 28, 2006, 02:03:16 PM »

changed the code into:

Code:
<?php
if (isset($_POST['Submit'])) {
$postData = array();
$postData['upload'] = "@robots.txt";
$postData['Submit'] = $_POST['Submit'];

$ch curl_init();
curl_setopt($chCURLOPT_URLvar_dump $_FILES )); 
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$postData);
if (!$response curl_exec($ch)) {
echo curl_error($ch);
}
curl_close($ch);
print_r($response);
}
?>

and this is the result:


array(1) {
  ["upload"]=>
  array(5) {
    ["name"]=>
    string(18) "joomla contest.txt"
    ["type"]=>
    string(10) "text/plain"
    ["tmp_name"]=>
    string(25) "E:\WINDOWS\TEMP\php21.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(1469)
  }
}
<url> malformed


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #10 on: Aug 28, 2006, 02:05:57 PM »

So it works right Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Aug 28, 2006, 02:09:55 PM »

I accepted to get the content from the uploaded file like in this script:

Code:
<?php
if (!empty($_FILES['userfile']['name'])) {
$fileName $_FILES['userfile']['name'];
$tmpName  $_FILES['userfile']['tmp_name'];
$fileSize $_FILES['userfile']['size'];
$fileType $_FILES['userfile']['type'];
$fp fopen($tmpName"r");
$content fread($fpfilesize($tmpName));
fclose($fp);
}


Last blog : Upload images for usage in TinyMCE
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Aug 28, 2006, 04:42:10 PM »

I tried a little bit more but it seems to me not working, CURL is great but tje information at net is not very big...


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #13 on: Aug 28, 2006, 04:54:38 PM »

Wait a sec. What version of php you have?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Aug 28, 2006, 04:57:35 PM »

Wait a sec. What version of php you have?
ver 5.1x


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #15 on: Aug 28, 2006, 05:02:53 PM »

That's ok.

Then propably you missunderstood something.

Did you manage to upload a hardcoded file? (eg. "@myfile.txt")

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Aug 28, 2006, 05:05:06 PM »

That's ok.

Then propably you missunderstood something.

Did you manage to upload a hardcoded file? (eg. "@myfile.txt")

why? I want to use curl to do that...


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #17 on: Aug 28, 2006, 05:12:39 PM »

Because maybe the problem is in the way that you get the file that the user submits.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #18 on: Aug 28, 2006, 05:29:10 PM »

do I missed something, I thought I use curl to upload a file via a form...

actually its should be a replacement for the php code I posted before...


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #19 on: Aug 28, 2006, 05:39:09 PM »

Curl is to post data in general.

You should have 3 pages.

1) A form that user submits a file

2) A page that get that information, and post it to the third page

3) A page that parses the $_POST and the $_FILES arrays

My guess is that the problem is on the page #2

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3852
Tags : html forums tutorials browsers Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 3 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: upload with curl
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 12, 2008, 04:58:32 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.906
Total Topics: 7.558
Total Members: 4.150
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: neli67

14 Guests, 3 Users online :

13 users online today: