... ... ... In this tutorial we want to upload a file to some (password protected) remote FTP server via a web form. <form action="curlupload.php" method="post" enctype="multipart/form-data"> <div> <label for="upload">Select file</label> <input name="upload" type="file" /> <input type="submit" name="Submit" value="Upload" /> </div> </form>The form is simple and has only one file field and the submit button. Don’t forget to protect this kind of pages.Next we need some PHP code to handle the upload and opens a stream to send the file via cURL to the remote FTP server (place this code above the html code):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://ftp_login:[email protected]/'.$_FILES['upload']['name']); 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.'; } }
...
...
In this tutorial we want to upload a file to some (password protected) remote FTP server via a web form.
<form action="curlupload.php" method="post" enctype="multipart/form-data">
<div>
<label for="upload">Select file</label>
<input name="upload" type="file" />
<input type="submit" name="Submit" value="Upload" />
</div>
</form>The form is simple and has only one file field and the submit button. Don’t forget to protect this kind of pages.Next we need some PHP code to handle the upload and opens a stream to send the file via cURL to the remote FTP server (place this code above the html code):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://ftp_login:[email protected]/'.$_FILES['upload']['name']);
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.';
}
}
------------------------
最重要是这个,ftp://用户名:密码@ftp服务器地址/文件路径/要更新的文件
Class Curl_Ftp
{
private $ftpname; //FTP用户名
private $ftppaw; //FTP密码
private $urlftp; //FTP地址
private $filename; //文件名
public __construct($name, $password, $ftp)
{
$this->ftpname = $name;
$this->ftppaw = $password;
$this->urlftp = $ftp;
// $this->filename = $filename;
}
public function GetFtp()
{
if (isset($_POST['Submit']))
{
if (!empty($_FILES['upload']['name']))
{
$ch = curl_init();
$this->filename = $_FILES['upload']['tmp_name'];
$fp = fopen($this->filename, 'r');
curl_setopt($ch, CURLOPT_URL, $this->ftp.$this->filename);
curl_setopt($ch, CURLOPT_USERPWD, "$name:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->filename));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0)
{
$error = '文件上传成功';
}
else
{
$error = '文件上传失败';
}
}
else
{
$error = '未选择文件';
}
}
}
}
?>
大家帮我看看这个类还有哪里要改和不足?
2、如果要传到子目录中呢?
3、如果遇到重名文件如何处理?