用php如何把一些文件和图片上传到另一指定的服务器

用php如何把一些文件和图片上传到另一指定的服务器,第1张

一个实例:
首先,在自己台式机和笔记本上都开通了ftp,这个不会的同学可以网上查serv-u,相关教程肯定不少的。
然后在台式机本地做了个测试:
$ftp_server = "1921681100";
$ftp_user_name = "laohu";
$ftp_user_pass = "123456";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$file = 'testtxt';
$remote_file = '/test/atxt';
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "文件移动成功\n";
} else {
echo "移动失败\n";
}
ftp_close($conn_id);
运行后:文件移动成功。
要的就是这个效果了,之后用台式机做程序服务器,上传附件时全用ftp方法上传至笔记本上,笔记本ip是105,相应代码如下:
if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
$ftp_server = "1921681105";
$ftp_user_name = "lesley";
$ftp_user_pass = "123456";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$file = $_FILES['uploadfile']['tmp_name'];
$remote_file = '/test/'$_FILES['uploadfile']['name'];
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "文件:"$_FILES['uploadfile']['name']"上传成功\n";
} else {
echo "上传失败\n";
}
ftp_close($conn_id);
}
对应的前台页面代码:
<form action="uploadfilephp" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile" id="uploadfile" />
<input type="submit" name="submit" value="submit" />
</form>
运行后确实成功。
需要注意:
在用ftp_put方法时,第四个参数传送模式,需要用FTP_BINARY(二进制模式),用FTP_ASCII(文本模式)时,能上传但无法显示,其他文件重命名、中文乱码解决、上传权限控制等,就不在此提及了。

android客户端实现FTP文件需要用到 commons-net-301jar
先将jar包复制到android libs目录下
复制以下实现代码
以下为实现代码:
/
通过ftp上传文件
@param url ftp服务器地址 如:
@param port 端口如 :
@param username 登录名
@param password 密码
@param remotePath 上到ftp服务器的磁盘路径
@param fileNamePath 要上传的文件路径
@param fileName 要上传的文件名
@return
/
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClientconnect(url, IntegerparseInt(port));
boolean loginResult = ftpClientlogin(username, password);
int returnCode = ftpClientgetReplyCode();
if (loginResult && FTPReplyisPositiveCompletion(returnCode)) {// 如果登录成功
ftpClientmakeDirectory(remotePath);
// 设置上传目录
ftpClientchangeWorkingDirectory(remotePath);
ftpClientsetBufferSize(1024);
ftpClientsetControlEncoding("UTF-8");
ftpCliententerLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClientstoreFile(fileName, fis);

returnMessage = "1"; //上传成功
} else {// 如果登录失败
returnMessage = "0";
}
} catch (IOException e) {
eprintStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtilscloseQuietly(fis);
try {
ftpClientdisconnect();
} catch (IOException e) {
eprintStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/12812530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存