如何在PHP中通过ftp上传文件? [英] How to upload a file via ftp in PHP?

查看:48
本文介绍了如何在PHP中通过ftp上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有html浏览类型和提交按钮的表单.我使用浏览按钮选择了一个文件,然后提交了表单.在表单提交时,将调用以下代码.

I have a form with html browse type and a submit button. I chose a file using browse button and submit the form. On form submission following code is called.

$conn_id="myid";
$conn_id = ftp_connect ( 'server' );
$ftp_user_name="username";
$ftp_user_pass="password";

// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );

// check connection
if ((! $conn_id ) || (! $login_result )) {
    echo "FTP connection has failed!" ;
    exit;

} else {
    echo "Connected to for user $ftp_user_name" ;
} 

// upload the file
$upload = ftp_put( $conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY );

// check upload status
if(!$upload){
    echo "FTP upload has failed!" ;
} else {
    echo "Successfully Uploaded." ;
}

但是会产生以下警告:

Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!

但是当我在上面的代码中硬编码源路径时,它将文件上传到服务器上:

But when I hard code the source path in above code then it upload the file on server:

$upload = ftp_put( $conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY );

推荐答案

具有enctype的html代码示例:

Example of html code with enctype:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="myfile" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

Form 标记中是否有 enctype ?没有该属性,您的文件将不会发送.

Do you have enctype in Form tag? Without the attribute your file won't be send.

用于调试$ _FILES数组.试试

For debugging of $_FILES array. Try

var_dump($_FILES);

查看数据是否正确.

您的变量:

$_FILES['tmp_name'] 

被错误地使用,因为$ _FILES包含的文件不是一个文件.因此,您应该写:

is used wrongly because $_FILES contain files not a one file. Therefore you should write:

$_FILES['your-name-in-html-form']['tmp_name'] // your-name-in-html-form = myfile in the example above

看示例: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/

这篇关于如何在PHP中通过ftp上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆