doufei0933
2010-08-17 13:09
浏览 84
已采纳

如何在PHP中通过ftp上传文件?

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." ;
}

But it produce the following warning:

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 );

图片转代码服务由CSDN问答提供 功能建议

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

  $ conn_id =“myid”; 
 $ conn_id = ftp_connect('server'); 
 $ ftp_user_name =“username”;  
 $ ftp_user_pass =“password”; 
 
 //使用用户名和密码登录
 $ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass); 
 
 //检查连接
if((!  $ conn_id)||(!$ login_result)){
 echo“FTP连接失败!”  ; 
退出; 
 
}其他{
 echo“连接到用户$ ftp_user_name”; 
} 
 
 //上传文件
 $ upload = ftp_put($ conn_id,“images / signatures  /“。$ fileName,$ _FILES ['tmp_name'],FTP_BINARY); 
 
 //检查上传状态
if(!$ upload){
 echo”FTP上传失败!“  ; 
} else {
 echo“Successfully Uploaded。”  ; 
} 
   
 
 

但它产生以下警告:

 警告:ftp_put():文件名不能 第70行上的/var/www/echdp/_ProviderSignature.php为空!FTP上传失败!
   
 
 

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

  $ upload = ftp_put($ conn_id,“images / signatures / myfile.txt”,“/ var / www / images / hello.txt  “,FTP_BINARY); 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • doujuegai8830 2010-08-17 13:19
    最佳回答

    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>
    

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

    For debugging of $_FILES array. Try

    var_dump($_FILES);
    

    to see if the data are correct.

    Your variable:

    $_FILES['tmp_name'] 
    

    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
    

    Have a look on the example: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题