LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2005, 12:03 AM   #1
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Rep: Reputation: 15
PHP upload file to other server


Hi all,

I have a question on using php to upload a file from 1 server to another server.

The condition is : when a user click a link, then the server will upload a file to another server. (remarks: not client side upload)

which php function can use in this situation? any idea or suggestion ??


thx!
 
Old 02-04-2005, 12:44 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
One
Two

Option one can handle HTTP uploads as well as FTP, but will probably require more learning (you have to do most things through curl's setopt function, and there's a hell of a lot in there to read through).
Option two is easier, but only does FTP transfers.
 
Old 02-04-2005, 12:45 AM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
A third option is just to shell out from PHP and run the command line client of your choice... probably the easiest to make work, but the hardest to troubleshoot.
 
Old 02-04-2005, 01:17 AM   #4
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
Smile

it it possible to use put command to put the file to the target server ??

my point is no need the user to donwload the file.

simply simulate the upload action instead of upload from client side.

when the user click the link then trigger the shell script command to put the file from server A to server B

the target server will have a servelt to handle to upload file.

thx for reply!!
 
Old 02-04-2005, 01:34 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Sorry, I guess I wasn't clear - those functions execute on the server, because they're PHP functions. PHP never executes in the client's browser. If your script says
Code:
ftp_put($conn, "uploadedfile", "/etc/resolv.conf")
then the server will upload it's resolv.conf file as "uploadedfile" on the second server.
 
Old 02-04-2005, 01:52 AM   #6
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
the situation is 2 linux servers. one is running PHP, another is running JSP servlet

how can i let the user click a link or button to trigger the action to upload a file from PHP server to JSP server directly?

PHP server will use the HTTP "put" the file to the JSP server. and the JSP server will receive it as a normal upload like HTTP <form action="servlet" method="" enctype="">

is it possible to do that ??


 
Old 02-04-2005, 02:13 AM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Yes, the curl functions (link #1) will do that. Read through the docs, especially the setopt function, because that's where the functionality really is.

The best way to start would be to get file uploads working (from the PHP server preferably, if you have command line access) with the command line curl tool - this will give you a head start on figuring out which options you will need to set when you use the library functions. As soon as you can upload the file and have the JSP servlet receive it correctly, the rest is just grunt work.
 
Old 02-04-2005, 02:28 AM   #8
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
how do i know my php server install curl or not ??
 
Old 02-04-2005, 02:33 AM   #9
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
I go to php.net and see some curl example and i try to run at my server but see the following message.

example
====================================
<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
====================================
message
====================================

Warning: fopen("example_homepage.txt", "w") - Permission denied in /var/www/html/testcurl.php on line 4

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /var/www/html/testcurl.php on line 6

You have reached this web page by typing "example.com", "example.net", or "example.org" into your web browser.

These domain names are reserved for use in documentation and are not available for registration. See RFC 2606, Section 3.


Warning: fclose(): supplied argument is not a valid File-Handle resource in /var/www/html/testcurl.php on line 11
 
Old 02-04-2005, 02:37 AM   #10
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
oh ! sorry. I make a mistake. I change the file permission to apache and the url to google.com then the program can run again!!

thx for suggestion, I will try to use the curl lib Thx again!!!
 
Old 02-04-2005, 02:37 AM   #11
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Quote:
Warning: fopen("example_homepage.txt", "w") - Permission denied in /var/www/html/testcurl.php on line 4
Curl (the PHP library part) is definitely installed - you can tell because curl_setopt returned a warning message rather than an "unknown function" error. The first error is the source of the problem - your PHP script is probably running as a user that doesn't have permission to read the file you want to upload. Do you have shell access to this server?
 
Old 02-04-2005, 02:50 AM   #12
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
How do i know which version I have
 
Old 02-04-2005, 03:05 AM   #13
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
version of...?

phpinfo() would usually have most info you'd need on versions and such...
 
Old 02-04-2005, 03:11 AM   #14
antony_csf
Member
 
Registered: Jun 2004
Posts: 51

Original Poster
Rep: Reputation: 15
thx !!! I see it !! thx~~!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
file upload with php+libcurl via POST Cupacabra Linux - Software 0 01-12-2005 12:53 PM
Cant upload a file bigger then 1mb with php OrangeLefty Programming 1 04-19-2004 01:54 AM
PHP: upload file to client... ugenn Programming 3 08-26-2002 10:59 AM
File upload via PHP saravanan1979 Programming 1 02-14-2002 04:28 AM
File upload via PHP!!! saravanan1979 Programming 0 02-14-2002 01:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration