如何在文件名包含的目录中复制文件..请参阅下面的示例
file_123_XXXXXX.zip其中XXXXXX是随机数..
我想将file_123_XXXXXX.zip从服务器复制到相同的文件名到我的本地文件夹
如果我在服务器中设置完全相同的文件名,那么代码正在运行,但如果文件名每天随机更改会怎么样。
提前感谢..
这是我的代码:
include("./config.php");
$local_file1 = 'C:\Destination\file_123_XXXXXX.zip'; //how to copy the original filename XXXXX
if(file_exists($local_file1))
{
echo "
$('#getUpdts').attr('disabled','disabled')
.addClass('ui-state-disabled');
$('#proc').removeAttr('disabled')
.removeClass('ui-state-disabled');
";
echo "infoMsg('File is already downloaded..')";
}
else
{
$ftp_user = ftp_user;
$ftp_pw = ftp_pw;
$conn_id = ftp_connect('192.xxx.xxx.xxx') or die("Couldn't connect to 192.xxx.xxx.xxx");
$server_file1 = "/fromlocation/file_123_XXXXXX.zip"; //the filename with random that i want to get
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pw);
if(!file_exists($local_file1))
{
$contents = ftp_size($conn_id, $server_file1);
if ($contents >0) {
if (ftp_get($conn_id, $local_file1, $server_file1, FTP_BINARY)) {
echo "infoMsg('Successfully downloaded');";
} else {
echo "alertMsg('Unable to download');";
}
}else{
echo "alertMsg('Does not exist.');";
}
}
else
{
echo "alertMsg('does not exists');";
}
// close the connection
ftp_close($conn_id);
}
答案 0 :(得分:0)
正如我在评论中提出的,你可以考虑:
功能代码:
function get_last_modified_file($dir)
{
$path = $dir;
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read()))
{
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime)
{
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
return $latest_filename;
}
使用您的代码 - 它看起来像:
include("./config.php");
// Copy - Pasterino the function get_last_modified() here
// Fill variables $server_dir & $local_dir
$ftp_user = ftp_user;
$ftp_pw = ftp_pw;
$server_dir = "the path we will search into the last file/";
$local_dir = " the destination path/";
$conn_id = ftp_connect('192.xxx.xxx.xxx') or die("Couldn't connect to 192.xxx.xxx.xxx");
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pw);
// Get last modified file
$last_ctime_file = get_last_modified_file($server_dir);
if(file_exists($local_dir . $last_ctime_file"))
{
// We don't download it / echo Warning, etc..
echo "blalbllalba";
}
else
{
// We download it - Same code you used
$contents = ftp_size($conn_id, $server_dir . $last_ctime_file);
if ($contents >0)
{
if (ftp_get($conn_id, $local_dir . $last_ctime_file,
$server_dir . $last_ctime_file, FTP_BINARY))
echo "infoMsg('Successfully downloaded');";
else
echo "alertMsg('Unable to download');";
}
else
echo "alertMsg('File is empty.');";
}
ftp_close($conn_id);
答案 1 :(得分:0)
列出目录并使用preg_match()
ftp_chdir($conn_id, "/fromlocation/");
foreach (ftp_nlist($conn_id, ".") as $server_file1) {
if (!preg_match('/^file_123_\d{6}\.zip/i', $server_file1)) continue;
if (is_file($server_file1)) continue;
// then the rest of your code...
$contents = ftp_size($conn_id, $server_file1);
if ($contents > 0) {
if (ftp_get($conn_id, $local_file1, $server_file1, FTP_BINARY)) {
echo "infoMsg('Successfully downloaded');";
} else {
echo "alertMsg('Unable to download');";
}
} else {
echo "alertMsg('Does not exist.');";
}
}