使用PHP将文件从远程服务器上传到FTP服务器
如何使用PHP将链接上的远程文件(例如http://site.com/file.zip
)上载到FTP服务器?我想上传'Vanilla论坛软件'到服务器,我的移动数据运营商收取高价,所以如果我可以上传文件而不必从移动设备上传它,我可以省钱并完成工作。使用PHP将文件从远程服务器上传到FTP服务器
制造将此功能:
function downloadfile($file, $path) {
if(isset($file) && isset($path)) {
$fc = implode('', file($file));
$fp = explode('/', $file);
$fn = $fp[count($fp) - 1];
if(file_exists($path . $fn)) {
$Files = fopen($path . $fn, 'w');
} else {
$Files = fopen($path . $fn, 'x+');
}
$Writes = fwrite($Files, $fc);
if ($Writes != 0){
echo 'Saved at ' . $path . $fn . '.';
fclose($Files);
}
else{
echo 'Error.';
}
}
}
你可以使用这样的:
downloadfile("http://www.webforless.dk/logo.png","folder/");
希望它工作得很好,记得CHMOD目标文件夹777.(如果您需要将其上传到另一个FTP服务器,则可以使用其他评论中发布的FTP脚本之一s))
此致敬礼。 Jonas
对不起,我的英语和我迟到的回复。我的意思是将文件www.example.com/ file.zip直接下载到ftp.somesite.com,而无需将ww.example.com/ file.zip下载到我的存储空间,然后将其上传到ftp.somesite.com。感谢你的回复 –
像这样的事情
$con=ftp_connect("ftp.yourdomain.com");
$login_result = ftp_login($con, "username", "password");
// check connection
if ($conn_id && $login_result) {
// Upload
$upload = ftp_put($con, 'public_html/'.$name, "LOCAL PATH", FTP_BINARY);
if ($upload) {
// UPLOAD SUCCESS
}
}
A)通过URL下载文件:
$destination = fopen("tmp/myfile.ext","w");
//Myfile.ext is an example you should probably define the filename with the url.
$source = fopen($url,"r");
while (!feof($source)) {
fwrite($destination,fread($source, 8192));
}
fclose($source);
fclose($destination);
B)上传FTP上的文件:
$file = 'tmp/myfile.ext';
$fp = fopen($file, 'r');
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "UPLOAD OK";
} else {
echo "ERROR";
}
ftp_close($conn_id);
fclose($fp);
This只是一个简单的例子,这个代码可能有很多改进,但主要思想在这里。
注意:如果您有专用服务器,则可能通过调用wget下载文件可能更快更容易。
FTP的更多信息,可以发现in the doc
在目标服务器上的Web可访问的文件夹中创建一个php脚本,更改$ remotefile和$ localfile的值,将浏览器指向脚本url并将文件拉出。
<?php
$remotefile="http://sourceserver.com/myarchive.zip";
$localfile="imported_archive.zip";
if(!copy($remotefile, $localfile)) {
echo("Transfer Failed: $remotefile to $localfile");
}
?>
简单:
copy('ftp://user:[email protected]/file.txt', 'ftp://user:[email protected]/file.txt');
PHP的服务器会消耗带宽,上传和下载同步。
文件大是为了让'file_get_contents('http://site.com/file.zip')'无用吗?我认为你问题中的措辞有点扭曲。我的猜测是你可以访问你的主机,你想上传远程文件。 – Shef