可以输入密码,求完整代码。
解决方案 »
- 域名跳转问题
- 话说php
- 网站怎么调用discuz论坛的登录实现网站和论坛同步登录与退出???
- 求教:zend studio debugger 远程调试的问题(是zend studio 7.1版本,不是ZDE)
- SqlServer有点怪:php用mssql连sqlserver,select id from table,可以得到结果,select * 和select `id` 或select id, title都得不到结
- --------有没有什么完善的文章系统,帮忙推荐一个。。。。-------
- 我要采购ERP,ERP达人或销售代表进
- 关于array_walk()函数的使用疑惑
- 文件路径名表示问题
- 谁能告诉我怎么在4.1的php里使用支持gif的gd库
- 如何随机获取mysql中的任意一条记录?
- JS动态添加表单和取值的疑问。
...
...
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、如果遇到重名文件如何处理?