PHP:将文件从一台服务器上传到另一台服务器 [英] PHP: upload file from one server to another server
问题描述
我正在服务器上开发网站,而存储在另一台服务器上,因此我必须以某种方式进行处理.我很不高兴遇到这种情况,发现解决方案是使用卷发.
I'm developing a website in a server and the storage is in another server and I have to deal with that somehow. I nerve experience this case and I found that the solution is to use curl.
请向我详细说明如何从零开始使用Curl.
更新:
我使用以下代码测试cURL是否已安装并启用:
I use the following code to test if cURL is installed and enabled:
<?PHP
phpinfo();
$toCheckURL = "http://board/accSystem/webroot/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $toCheckURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if(!$data) {
echo "Domain could not be found";
} else {
switch($code) {
case '200':
echo "Page Found";
break;
case '401':
echo "Unauthorized";
break;
case '403':
echo "Forbidden";
break;
case '404':
echo "Page Not Found";
break;
case '500':
echo "Internal Server Error";
break;
}
}
?>
结果如下:
我收到(找到页面)消息
And I got (Page found) message
现在我可以不用担心使用cURL了,对吧?
Now I can use cURL without worry, right ?
注意: 两台服务器都是本地的
Note: Both servers are local
推荐答案
作为PHP开发人员,您可能已经熟悉PHP最方便的文件系统功能fopen.该函数打开文件流并返回资源,然后可以将其传递给fread或fwrite来读取或写入数据.有些人没有意识到,虽然文件资源不一定必须指向本地计算机上的某个位置.
As a PHP developer, you may already be familiar with PHP's most handy file system function, fopen. The function opens a file stream and returns a resource which can then be passed to fread or fwrite to read or write data. Some people don't realize though that the file resource doesn't necessarily have to point to a location on the local machine.
下面是一个将文件从本地服务器传输到ftp服务器的示例:
Here's an example that transfers a file from the local server to an ftp server:
$file = "filename.jpg";
$dest = fopen("ftp://username:password@example.com/" . $file, "wb");
$src = file_get_contents($file);
fwrite($dest, $src, strlen($src));
fclose($dest);
可以在PHP手册的附录M中找到受支持的不同协议的列表.您可能希望使用采用某些加密机制的协议,例如FTPS或SSH,具体取决于网络设置和所移动信息的敏感性.
A listing of different protocols that are supported can be found in Appendix M of the PHP manual. You may wish to use a protocol that employs some encryption mechanism such as FTPS or SSH depending on the network setup and the sensitivity of the information you’re moving.
curl扩展使用客户端URL库(libcurl)传输文件.实现curl解决方案的逻辑通常如下:首先初始化会话,设置所需的传输选项,执行传输,然后关闭会话.
The curl extension makes use of the Client URL Library (libcurl) to transfer files. The logic of implementing a curl solution generally follows as such: first initialize a session, set the desired transfer options, perform the transfer and then close the session.
使用curl_init函数完成curl会话的初始化.该函数返回可以与其他curl函数一起使用的资源,就像通过文件系统函数中的fopen获取资源一样.
Initializing the curl session is done with the curl_init function. The function returns a resource you can use with the other curl functions much as how a resource is obtained with fopen in the file system functions.
使用curl_setopt设置上传目标和传输会话的其他方面,该参数将使用curl资源,代表设置和选项值的预定义常量.
The upload destination and other aspects of the transfer session are set using curl_setopt which takes the curl resource, a predefined constant representing the setting and the option’s value.
下面是一个使用HTTP协议的PUT方法将文件从本地主机传输到远程服务器的示例:
Here's an example that transfers a file from the local host to a remote server using the HTTP protocol's PUT method:
$file = "testfile.txt";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://example.com/putscript");
curl_setopt($c, CURLOPT_USERPWD, "username:password");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));
$fp = fopen($file, "r");
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_exec($c);
curl_close($c);
fclose($fp);
可以在php文档中找到有效的curl选项列表.
A list of valid options for curl can be found in the php documentation.
ftp扩展允许您实现对ftp服务器的客户端访问.当需要使用诸如前两个选项之类的选项时,使用ftp传输文件可能会过大.理想情况下,最好使用此扩展名,并且需要更高级的功能.
The ftp extension allows you to implement client access to ftp servers. Using ftp to transfer a file is probably overkill when options like the previous two available... ideally this extension would be best used more advanced functionality is needed.
使用ftp_connect与ftp服务器建立连接.您可以通过使用ftp_login为其提供用户名和密码来验证与ftp服务器的会话.使用ftp_put函数将文件放置在远程服务器上.它接受目标文件名,本地源文件名和预定义常量以指定传输模式:FTP_ASCII用于纯文本传输,或FTP_BINARY用于二进制传输.传输完成后,将使用ftp_close释放资源并终止ftp会话.
A connection is made to the ftp server using ftp_connect. You authenticate your session with the ftp server using ftp_login by supplying it a username and password. The file is placed on the remote server using the ftp_put function. It accepts the name of the destination file name, the local source file name, and a predefined constant to specify the transfer mode: FTP_ASCII for plain text transfer or FTP_BINARY for a binary transfer. Once the transfer is complete, ftp_close is used to release the resource and terminate the ftp session.
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "username", "password");
ftp_put($ftp, "destfile.zip", "srcfile.zip", FTP_BINARY);
ftp_close($ftp);
这篇关于PHP:将文件从一台服务器上传到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!