重命名PHP文件夹中的文件,而不是php root

时间:2011-12-26 08:22:55

标签: php file ftp directory rename

我有一个PHP脚本,用于重命名FTP服务器根目录上的文件。

我需要更改此项以重命名FTP根目录中的文件。

当前的工作 将根目录中的文件重命名为despgoods.csv

希望工作 在root / despgoods / despgoods.csv

中重命名文件

我的PHP脚本是:

$ftp_server = "ftp.ftpserver.co.za";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "username";
$ftp_user_pass = "password";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$targetname = "DespGoods.csv";
$contents = ftp_nlist($conn_id, '');
if (!$contents) {
    echo 'No Files Present on Server: <br/> ';
    echo mysql_error();
    die;
}
$filename = $contents[0];
if ($filename == "despGoods.csv") {
    echo 'DespGoods already exists, no need to rename: <br/> ';
    echo mysql_error();
} else {
    ftp_rename($conn_id, $filename, $targetname);
    ftp_close($conn_id);
    echo "1 available file renamed to DespGoods.csv <br>";
}

在此先感谢,我一如既往地感谢您的帮助,

此致 莱恩

2 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/function.ftp-chdir.php

$contents = ftp_nlist($conn_id, 'despgoods');
$filename = $contents[0];
if ($filename == "despGoods.csv") {
    echo 'DespGoods already exists, no need to rename: <br/> ';
} else {
    ftp_rename($conn_id, 'despgoods/'.$filename, 'despgoods/'.$targetname);
    echo "1 available file renamed to DespGoods.csv <br>";
}
ftp_close($conn_id);

P.S。 mysql_error()在您的代码中做了什么?

答案 1 :(得分:0)

使用此

$ftp_server = "ftp.ftpserver.co.za";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "username";
$ftp_user_pass = "password";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$targetdir= "despgoods"; //target directory under which file is to be modified
$targetname = "DespGoods.csv";
ftp_chdir($conn_id, $targetdir);
$contents = ftp_nlist($conn_id, '');
if (!$contents) {
    echo 'No Files Present on Server: <br/> ';
    echo mysql_error();
    die;
}
$filename = $contents[0];
if ($filename == "despGoods.csv") {
    echo 'DespGoods already exists, no need to rename: <br/> ';
    echo mysql_error();
} else {
    ftp_rename($conn_id, $filename, $targetname);
    ftp_close($conn_id);
    echo "1 available file renamed to DespGoods.csv <br>";
}

同时检查http://www.php.net/manual/en/function.ftp-chdir.php