PHP FTP:访问被拒绝

时间:2011-11-01 06:57:32

标签: php ftp

我正在从我的php页面上进行FTP上传。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();

$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);


if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
 ?>

它在浏览器中给我一个警告

Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.

我检查了文件的访问权限。但它可以访问。任何人都可以告诉我为什么会这样吗?

3 个答案:

答案 0 :(得分:3)

很可能这是一个许可问题。当您通过FTP上传文件时,还需要检查目录的权限。当你说它可以访问时,并不意味着它是可写的。

答案 1 :(得分:1)

您不会检查the login operation的结果:

if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
    echo "Connected as $ftp_user_name@$ftp_server\n";
} else {
    echo "Couldn't connect as $ftp_user_name\n";
}

您还应该尝试从PHP主机到FTP主机的手动FTP操作,以确保您可以使用这些凭据登录并放置文件。这将帮助您确定它是您的错误代码还是FTP凭据。

答案 2 :(得分:-1)

           <?php 
           class Ftp {
          function upload()
         {
           $ftp_server="50.56.113.39";
           $ftp_user_name="******";
           $ftp_user_pass="***";
           $file = "form.pdf";//tobe uploaded
           $remote_file = "uploads1/test.pdf";

        // set up basic connection
         $conn_id = ftp_connect($ftp_server);

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

         // upload a file
        if (ftp_put($conn_id, $remote_file, $file,  FTP_BINARY))
         {
          echo "successfully uploaded $file\n";
         //exit;
         } 
         else
           {
            echo "There was a problem while uploading $file\n";
            //exit;
            }
           // close the connection
         ftp_close($conn_id);

          }
            }
          ?>