Solved

PHP Curl - How to FTP string...

Posted on 2007-11-14
9
1,792 Views
Last Modified: 2012-08-13
I am trying to use the curl library in PHP  to send some data to another server. I can not write to a file on the server and then transfer it. How can I tell PHP/Curl to transfer not from a file but from a variable I have prepared?
0
Comment
Question by:albee01
  • 5
  • 3
9 Comments
 
LVL 1

Author Comment

by:albee01
ID: 20280764
I believe what I need is to use is the CURLOPT_READFUNCTION callback but I am uncertain how to properly structure it.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20281991
php has built in FTP functions .. do need to use CURL ?
Have a look at http://www.php.net/manual/en/ref.ftp.php first.
0
 
LVL 1

Author Comment

by:albee01
ID: 20282517
Unfortunately, yes. PHP on the server I must use does not enable the ftp functions, only curl.
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 100 total points
ID: 20282624
ok ... here is some example code

<?php

$file = "sample.html"
$fp = fopen ($file, "r");


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL,"ftp://username:password@ftp.example.com/path/to/file/file.html");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$error = curl_exec ($ch);

curl_close ($ch);
?>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:albee01
ID: 20283426
Thanks for the code but that doesn't do what I need. Your example only transfers from a file. I can't write to a file. I am trying to transfer the contents of a variable, not the contents of a file.

$string = "Text to transfer";

I want to write $string to the remote file without writing a file locally.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20283767
In the example i raed the file into a variable. Its essentially the same thing.
Instead of

$file = "sample.html"
$fp = fopen ($file, "r");

use

$fp = "text to transfer";
0
 
LVL 1

Author Comment

by:albee01
ID: 20283965
I get this: curl_setopt(): supplied argument is not a valid File-Handle resource
0
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 25 total points
ID: 20288381
You are getting the error because the $fp variable is not a file handler, and the curl_setopt() is expecting a file handler as argument. I dont' think there is any way to send a variable through curl onto the ftp server.

Closest thing I could find was this, which may be of help:
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/
0
 
LVL 1

Author Comment

by:albee01
ID: 20288771
I was leaning toward it not being possible. Thanks for the help. I'll split the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 49
myqsl update statement on phpMyAdmin 8 21
Session timeout 5 13
how to handle $x_amount to payment gateway in php - stumped. 14 13
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now