【问题标题】:FTP permission denied using php script使用 php 脚本拒绝 FTP 权限
【发布时间】:2015-05-23 04:38:47
【问题描述】:

我的 ftp 帐户有问题。这是我编写的用于连接到我的 FTP 并创建文件夹的代码:

<?php 
$ftp_server = "xxxxxxxxxxxxxxx";
$ftp_username = 'xxxxxxxx';
$ftp_password = 'xxxxxxxxxxxxxxx';

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// try to create directory $path
$path = '/2/7/7';
if(ftp_mkdir($ftp_conn, $path)) {
    echo 'done';
    die();
}
echo 'error';
?>

运行代码时出现以下错误:

警告:ftp_mkdir():权限被拒绝

当我使用 filezilla 或其他 ftp 管理器应用程序时,我可以轻松创建一个文件夹,但是在使用脚本处理它时会出现问题。我告诉我的网络管理员以确保该帐户是否具有读写权限。他们说已经设置了读写权限。我猜它可能还需要执行权限,这可能没有设置。

有没有办法使用 php 脚本检查权限?我还尝试使用 filezilla 检查文件夹权限,但显示如下:

【问题讨论】:

  • 路径'/2/7是否已经存在? ftp_mkdir 不能递归地创建目录(参见:php.net/manual/en/function.ftp-mkdir.php 看第一条评论)。
  • 不,不存在,但我的脚本在服务器上运行了几个星期。我最近得到了错误,我不知道发生了什么,而我没有更改任何代码。
  • 也许一开始它正在创建一个新目录,而较低级别已经存在......这些数字什么时候改变?
  • 如果/2/7 不存在,这显然是问题所在。当您的脚本仍在工作时,它可能已经存在。

标签: php ftp permissions


【解决方案1】:

试试这个解决方案吧recursive-ftp-make-directory

// recursive make directory function for ftp
function make_directory($ftp_stream, $dir)
{
// if directory already exists or can be immediately created return true
if (ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
// otherwise recursively try to make the directory
if (!make_directory($ftp_stream, dirname($dir))) return false;
// final step to create the directory
return ftp_mkdir($ftp_stream, $dir);
}

function ftp_is_dir($ftp_stream, $dir)
{
// get current directory
$original_directory = ftp_pwd($ftp_stream);
// test if you can change directory to $dir
// suppress errors in case $dir is not a file or not a directory
if ( @ftp_chdir( $ftp_stream, $dir ) ) {
// If it is a directory, then change the directory back to the original directory
ftp_chdir( $ftp_stream, $original_directory );
return true;
} else {
return false;
}
}

不要忘记为新的递归目录添加$dir='2/3/4/5/6'

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 2014-10-22
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多