将“任何字符”传递到 PHP 值中


Pass "any character" into a PHP value?

>更新:

我每天都会导入一个XML提要,该提要通过XML提要供应商每24小时更新一次,然后转储到我们的FTP中。文件以日期开头,然后是导出时间,后跟文件名的其余部分。

所以每天都有一个新文件添加到我们的FTP中,我使用date()来获取日期,但时间可能会根据服务器运行导出的时间而有所不同(通常只关闭一分钟左右)。

所以我需要的是在$date$file之间传递 4 个数字值,从而从必要的文件夹中获取最新文件?

该脚本将多个值连接在一起以创建 URL,然后将该值设置为 header(); 中的location

<?php
$date = date("Ymd");
$webroot = "http://WEBSITE-URL/";
$file = "-XML-FILE.xml";
$xmlfilelocation = $webroot.$date."0206".$file;
header('Location: '.$xmlfilelocation);
?>

0206字符串是时间,可能会有所不同,无论如何我可以在那里传递任何值吗?

创建一个$time = ???;变量,它等于任何 4 个字符?

您可以使用

glob()函数来实现此目的。由于可能有多个文件,您可能需要返回第一个文件:

$serverRoot = '/the/real/server/location/';
chdir($serverRoot); // go to the directory where the file is located
$xmlfile = $date."*".$file;
foreach (glob($xmlfile) as $filename) {
    header('Location: '.$webroot.$filename);
    break; // or even exit
}

注意:您必须使用服务器路径来实现glob()功能。

最后,

我为此使用了另一种方法:

<?php
//Get web directory
$webdir = "THE-URL";
//Get local directory for scandir
$dir = 'THE-DIRECTORY';
//Create array of file names
$filelist = scandir($dir);
//Count file name and reduce by 1 to account for [0] array item
$recordcount = count($filelist) - 1;
//Get the latest filename from array
$latestfile = $filelist[$recordcount];
//Compile the web directory and filename to create a URL
$xmlfile = $webdir.$latestfile;
//Redirect to necessary URL
header('Location: '.$xmlfile);
?>