Php 文件上载并移动到另一台服务器

Php 文件上载并移动到另一台服务器,php,Php,我有一个应用程序,它有一个输入=文件 现在我需要上传到我的服务器,然后将文件移动到其他服务器。我怎样才能避免超时 另外,对于ajax上传程序有什么好的建议吗。谢谢。使用scp或rsync将文件传输到另一台服务器。每隔几分钟使用cron作业,而不是从php脚本执行一次,这将防止在服务器到服务器传输时间过长时出现任何超时。Flash Uploader:毫无疑问,或者基于后者 文件传输:使用PHPCURL进行HTTP POST表单传输,参见第二个示例 在进行传输之前,请执行以下操作: set_time

我有一个应用程序,它有一个输入=文件

现在我需要上传到我的服务器,然后将文件移动到其他服务器。我怎样才能避免超时


另外,对于ajax上传程序有什么好的建议吗。谢谢。

使用scp或rsync将文件传输到另一台服务器。每隔几分钟使用cron作业,而不是从php脚本执行一次,这将防止在服务器到服务器传输时间过长时出现任何超时。

Flash Uploader:毫无疑问,或者基于后者

文件传输:使用PHPCURL进行HTTP POST表单传输,参见第二个示例

在进行传输之前,请执行以下操作:

set_time_limit(-1);       // PHP won't timeout
ignore_user_abort(true);  // PHP won't quit if the user aborts
编辑:我看不出你需要CRON作业的正当理由,除非相关文件在某个时间发生更改,这是同步的真正定义。另一方面,如果您只想将文件复制到远程服务器,那么没有理由不使用普通PHP

另外,您应该注意文件大小。如果文件大小小于20mb,您就安全了

编辑2:顺便说一下,在正确的条件下,输出缓冲关闭,隐式输出打开,您可以向用户显示当前远程传输进度。我已经做了,其实并不难。您只需要一个隐藏的iframe,它发送进度请求来更新父窗口。 它的工作原理有点像AJAX,但使用iframe代替XHR,因为XHR与iframe不同,是以批量而不是块的形式返回的

如果有兴趣,我可以帮你解决这个问题,尽管问

Edit3:动态远程上传示例/说明:

简而言之,我假设您的文件已经由用户上传到服务器,而不是目标远程服务器。我还假设用户在上传文件后登录到handle.php

handle.php看起来像:

// This current script is only cosmetic - though you might want to
// handle the user upload here (as I did)

$name = 'userfile'; // name of uploaded file (input box) YOU MUST CHANGE THIS
$new_name = time().'.'.pathinfo($_FILES[$name]['name'],PATHINFO_EXTESION); // the (temporary) filename
move_uploaded_file($_FILES[$name]['tmp_name'],'uploads/'.$new_name);

$url = 'remote.php?file='.$new_name;  ?>

<iframe src="<?php echo $url; ?>" width="1" height="1" frameborder="0" scrolling="no"></iframe>

<div id="progress">0%</div>

<script type="text/javascript">
    function progress(percent){
        document.getElementById('progress').innerHTML='%'+percent;
    }
</script>
set_time_limit(0);       // PHP won't timeout

// if you want the user to be able to cancel the upload, simply comment out the following line
ignore_user_abort(true);  // PHP won't quit if the user aborts

// to make this system work, we need to tweak output buffering
while(ob_get_level())ob_end_clean(); // remove all buffers
ob_implicit_flush(true); // ensures everything we output is sent to browser directly


function progress($percent){
    // since we're in an iframe, we need "parent" to be able to call the js
    // function "progress" which we defined in the other file.
    echo '<script type="text/javascript">parent.progress('.$percent.');</script>';
}

function curlPostFile($url,$file=null,$onprogress=null){
    curl_setopt($ch,CURLOPT_URL,$url);
    if(substr($url,0,8)=='https://'){
        curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    }
    if($onprogress){
        curl_setopt($ch,CURLOPT_NOPROGRESS,false);
        curl_setopt($ch,CURLOPT_PROGRESSFUNCTION,$onprogress);
    }
    curl_setopt($ch,CURLOPT_HEADER,false);
    curl_setopt($ch,CURLOPT_USERAGENT,K2FUAGENT);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_MAXREDIRS,50);
    if($file){
        $fh=fopen($file);
        curl_setopt($ch,CURLOPT_INFILE,$fh);
        curl_setopt($ch,CURLOPT_INFILESIZE,filesize($file));
    }
    $data=curl_exec($ch);
    curl_close($ch);
    fclose($fh);
    return $data;
}

$file = 'uploads/'.basename($_REQUEST['file']);

function onprogress($download_size,$downloaded,$upload_size,$uploaded){
    progress($uploaded/$upload_size*100); // call our progress function
}

curlPostFile('http://someremoteserver.com/handle-uplaods.php',$file,'onprogress');
progress(100); // finished!

试试flash jquery flash上传器。不。。那只是为了上传。如何移动到另一台服务器,我仍然不知道,也许你应该设置一些接口或api或..@experitex:我有一个api来做它将同步/上传到另一台服务器从webprocess中移开,并与其他代码cronjobs/gearman/等异步处理。@Wrikken:Hmmm,你是对的,我对你如何实现远程传输的进展很感兴趣。我当然很想知道你是怎么做的。请解释。根据手册:最长执行时间,以秒为单位。如果设置为零,则不施加时间限制。哦,我知道是0,为什么我写-1?O.O