[英]PHP Write to file on different server
I need to write to a file that is located on a different server using php. 我需要使用php写入位于不同服务器上的文件。
So, I get user data from a form on one server, and then need to write to a text file that is located on another server. 因此,我从一个服务器上的表单获取用户数据,然后需要写入位于另一个服务器上的文本文件。 Do I need to authenticate or something similar?
我需要验证或类似的东西吗?
The second server that I need to write to is a windows server. 我需要写的第二台服务器是一台Windows服务器。 Is this an issue?
这是一个问题吗?
Thanks! 谢谢!
You need to have some service on the remote (windows) machine that will accept the file and store it. 您需要在远程(Windows)计算机上接受该文件并存储它。 There are many ways to do this:
有很多方法可以做到这一点:
I use HTTP approach and it works quite well (because I already run Apache on the remote machines), but any other option is viable as well. 我使用HTTP方法,它运行得很好(因为我已经在远程机器上运行Apache),但任何其他选项也是可行的。
I don't know if this is a possibility for you but... 我不知道这是否有可能对你而言......
Rather than trying to update a file on another server, especially something simple like a txt file I would have the current file on server one and then I would copy it to server two from server two with cURL using a cron job set to copy the file every x minutes. 而不是尝试更新另一台服务器上的文件,特别是像txt文件这样简单的东西,我会将当前文件放在服务器上,然后我会将它从服务器2复制到服务器2,使用cron作业集来复制文件每隔x分钟。
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
set_time_limit(3600); # 5 minutes for PHP
curl_setopt($ch, CURLOPT_TIMEOUT, 3600); # and also for CURL
$outfile = fopen( $dirname, 'wb');
curl_setopt($ch, CURLOPT_FILE, $outfile);
// grab file from URL
$response = curl_exec($ch);
fclose($outfile);
// close CURL resource, and free up system resources
curl_close($ch);
`
I would do this just because you would not have to deal with setting up a FTP account for the update or anything like that. 我这样做只是因为您不必处理为更新设置FTP帐户或类似的事情。 Your server that you need the file on is going out and grabbing the file for itself.
您需要该文件的服务器正在外出并为自己抓取该文件。
I use the above script to collect mp3s from various blogs. 我使用上面的脚本从各种博客收集mp3。 Sometime they will be 100-200MB and it works fine running off a shared server.
有时它们将是100-200MB,它可以很好地运行共享服务器。
There is no issue whether you are writing to either servers, you can accomplish this with PHP FTP . 您是否正在写入任一服务器都没有问题,您可以使用PHP FTP完成此任务。
Make sure you have correct permissions to write files on the servers or your code will not work and get an access denied error. 确保您具有在服务器上写入文件的正确权限,否则您的代码将无法正常工作并获得访问被拒绝错误。
with php you can make a ftp connection and write the file on the .net server with ftp 使用php,您可以建立ftp连接并使用ftp在.net服务器上写入文件
authentication can be done by ftp 身份验证可以通过ftp完成
for information how you can do this http://nl.php.net/ftp 有关如何执行此操作的信息http://nl.php.net/ftp
Are you running ftp on the second server ? 你在第二台服务器上运行ftp吗?
You can also do a ajax call to the seconde server with the data 您还可以使用数据对seconde服务器执行ajax调用
On the seconde server you place a script that writes the file with the content but then you must run http on the seconde server with a script language like php/asp 在seconde服务器上放置一个用内容写入文件的脚本,但是你必须使用像php / asp这样的脚本语言在seconde服务器上运行http
If you want to use SFTP, you can do it using cURL. 如果要使用SFTP,可以使用cURL进行。 Stack Overflow post
Stack Overflow post
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.