PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 2nd, 2012, 12:44 PM
jiceman jiceman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jiceman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
[Resolved] Post with cURL

Hello,

I'm trying to post a file from the local server to a remote server. I'm using cURL to do so, but it's not working right.

PHP Code:
 $filename '@'.getcwd()."/test.txt";
$vars[0] = "file=".urlencode($filename);
$vars[1] = "newName=".urlencode("success.txt");
$postString implode('&'$getVars);

$url "www.myserver.com/postProcess.php";

$ch curl_init();
curl_setopt($ch,CURLOPT_URL$url);
curl_setopt($ch,CURLOPT_POSTcount($vars));
curl_setopt($ch,CURLOPT_POSTFIELDS$poststring);
curl_setopt($ch,CURLOPT_RETURNTRANSFERtrue);
$result curl_exec($ch);
curl_close($ch); 


From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server.

a print_r($_POST) just gives me these results though...
Code:
Array ( [file] => @/var/www/html/test.txt [newName] => success.txt ) 

The actual file itself is not being sent, just the path

Thanks to anyone who can help me out.

Last edited by jiceman : November 5th, 2012 at 08:07 AM. Reason: problem solved

Reply With Quote
  #2  
Old November 2nd, 2012, 12:46 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Quote:
From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server.
Where did you read that? It's not true. Use file_get_contents to get a file's contents.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old November 2nd, 2012, 01:40 PM
jiceman jiceman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jiceman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
Thank you Dan.
I read that the "@" symbol was supposed to tell cURL that what followed was a reference to a file on the local system, and to send that file during the post.

I'm trying to send a pdf file. The pdf file is 2 pages, and now I'm creating a blank 2 page pdf file.
I'm trying 2 different methods, but I get the same results of just a blank 2 page pdf.

PHP Code:
 $handle fopen("files/test.pdf"'a');
fwrite($handle$_POST['file']);
fclose($handle);

file_put_contents("files/test2.pdf"$_POST['file']); 

Reply With Quote
  #4  
Old November 2nd, 2012, 01:50 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Oh man, I totally brain farted on @. Yes, that's correct.

This code should work:

$data['Filedata'] = "@".$file_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;


POSTFIELDS needs to have a variable called 'Filedata'. you can't just stick it into the array.

And once it makes it to the posted script, it will be an actual file upload, not a regular POST field.

Reply With Quote
  #5  
Old November 2nd, 2012, 03:24 PM
jiceman jiceman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jiceman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
Dan, You're awesome. You are great help.
One last thing...

Is it possible to send the file like that and other variables as well, particularly an array.

So for example, what I was sending before (being unable to send the file) was something like this...
PHP Code:
 $vars['names'][] = "a";
$vars['names'][] = "b";
$vars['names'][] = "c";
$vars['names'][] = "d";
$vars['names'][] = "e";
$vars['file'] = "@/var/www/html/file.pdf"

I then turned that into a GET type string. (like this: "var=value&var2=value2&var3=value3")
On the target server, the $_POST variable would recreate the arrays as desired.
the only issue was that the file wouldn't get sent.

So now the file is being sent, but if my array is something like...
PHP Code:
 $vars['test'] = "test value";
$vars['names'][] = "a";
$vars['names'][] = "b";
$vars['names'][] = "c";
$vars['names'][] = "d";
$vars['names'][] = "e";
$vars['Filedata'] = "@/var/www/html/file.pdf"

And just the $vars is being posted in the POSTFIELDS instead of the string form,
Then the file does get posted like I wanted, but now the $_POST['names'] just equals the string "Array".
$_POST['test'] does equal "test value" though.

Is it possible to send the file, and data arrays, so that $_POST will recreate the arrays like it did before?

Reply With Quote
  #6  
Old November 2nd, 2012, 05:06 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
Yes, something like this should work:
PHP Code:
 $vars['names[0]'] = "a";
$vars['names[1]'] = "b";
$vars['names[2]'] = "c";
$vars['names[3]'] = "d";
$vars['names[4]'] = "e";
$vars['file'] = "@/var/www/html/file.pdf"
Comments on this post
ManiacDan agrees!
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #7  
Old November 5th, 2012, 08:06 AM
jiceman jiceman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jiceman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 59 sec
Reputation Power: 0
That makes sense now that I see it. Thank you E-Oreo.
It works perfectly.
Thanks again ManiacDan.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Post with cURL

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap