Home > php教程 > php手册 > PHP跨网域跨主机跨server上传文件实例教程

PHP跨网域跨主机跨server上传文件实例教程

WBOY
Release: 2016-05-26 08:19:52
Original
1384 people have browsed it

有时我们为了方便、安全、快速,会把上传的文件单独放一台主机用二级域名访问,但是PHP如何把上传的文件放到另外一台主机呢?这就要跨域跨主机上传了,现在我们用实例来告诉你如何实现.

如何跨网域跨主机跨server上传文件?一般最基本的上传方式是:

1.使用者把文件上传到 web server

2. web server 把上传的文件 利用 move_uploaded_file() 函式,将档案移到指定的文件夹内.

但是,有时候我们需要把上传的档案放到另一台专门放文件的 file server,这时候,就无法利用 move_uploaded_file() 去搬移文件了,而需要利用 ftp 去传送文件至 file server,方法很简单...直接看程式码:

<?php
$file = $_FILES[&#39;file&#39;]; 
$file_tmp = $file[&#39;tmp_name&#39;]; 
$file_name = $file[&#39;name&#39;]; 
if(is_uploaded_file($file_tmp)){ //确定user有"上传"文件 
$file_ext = strrchr($file_name,&#39;.&#39;); //上传文件的副文件名 
$file_name_new = date(&#39;YmdHis&#39;).$file_ext; 
$host = &#39;127.0.0.1&#39;; 
$port = &#39;21&#39;; 
$user = &#39;admin&#39;; 
$pass = &#39;123456&#39;; 
$link = ftp_connect($host,$port); 
$login = ftp_login($link,$user,$pass); 
ftp_chdir($link,&#39;filedir&#39;); //切换到要放文件的文件夹 
if(ftp_put($link,$file_name_new,$file_tmp,FTP_BINARY)){ 
    $msg = &#39;上传成功&#39;;   
}else{ 
    $msg = &#39;上传失败&#39;; 
} 
}else{ 
    $msg = &#39;上传失败&#39;; 
} 
ftp_close($link); 
echo $msg;
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template