[英]FTP upload file to distant server with CURL and PHP uploads a blank file
I'm trying to upload a file to a distant server, but looks like the source file does nothing. 我正在尝试将文件上传到远程服务器,但看起来源文件什么都不做。 All i get is a blank file on the server.
我得到的只是服务器上的空白文件。 My code is this:
我的代码是这样的:
<?php
$c = curl_init();
$file = "\PATHTOFILE\file.txt";
$fp = fopen($file, "r");
curl_setopt($c, CURLOPT_URL, "SERVERPATH/file.txt");
curl_setopt($c, CURLOPT_USERPWD, "USER:PASSWORD");
curl_setopt($c, CURLOPT_UPLOAD, 1);
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));
curl_exec($c);
echo "Success";
curl_close($c);
fclose($fp);
?>
After 2 days of banging my head against the keyboard.. finally i did it.. Here is how: 经过2天的敲击键盘我的头...最后我做到了..这是如何:
<?php
if (isset($_POST['Submit'])) {
if (!empty($_FILES['upload']['name'])) {
$ch = curl_init();
$localfile = $_FILES['upload']['tmp_name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
} else {
$error = 'Please select a file.';
}
}
echo $error;
?>
Here is the source , from where i found the solution 这里是源 ,从其中i找到了解决办法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.