我正在使用以下内容将循环中的文件上传到远程服务。 由于我的服务器磁盘空间不足,我需要在成功上传后删除文件。何时/在哪里(在我的代码中)可以安全地在上传的文件中使用unlink(),确保在设法上传到远程服务器之前我没有删除它?
在下面的代码片段中,$ filename是我上传的本地文件的名称,$ f是我的日志文件。
$postfields = array('file' => '@'.$filename, 'username'=>'XXX','password'=>'YYY');
$retries_limit = 0;
$ch = curl_init("http://urlofservice.com/request/add");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$post_result = curl_exec($ch);
while((curl_errno($ch) > 0 or $retries_limit == 0) and $retries_limit < 10){
$retries_limit++;
if(curl_errno($ch)) {
fwrite($f,"cURL execution failed for ".$filename." with error ".curl_errno($ch).": " . curl_error($ch) ."#\r\n");
$secs = 120;
sleep($secs);
fwrite($f,"Sleeped for ".$secs." secs and attempting retry # ".$retries_limit." for ".$filename." #\r\n");
} else {
fwrite($f,"HTTP POST Request for ".$filename." made successfully #" ."\r\n");
}
}
curl_close($ch);
答案 0 :(得分:0)
我建议您不要,但是您可以像这样在您的服务器上添加一些身份验证并使用身份验证登录服务器。请查看以下代码作为示例
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); //Your credentials goes here
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
// Only for https server or comment it if not
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch);