文件搜索和匹配


File Search and Matching

我用PHP脚本创建PDF文件,现在我想按文件名搜索文件夹。文件夹中只有PDF文件,如下所示:

订单号+客户名称+金额

200_Anton_60.pdf
201_Peter_40.pdf
202_Melanie_60.pdf

详细文件名:

(Integer, unique and autoincrement) _ (string variable) _ (integer variable).pdf

现在我想在保存PDF文件之前检查:搜索文件夹中的现有文件,如果订单号和客户名称匹配,ONLY金额不同,请将PDF文件移到子文件夹中。

我希望你能帮助我!

试试这个:

$dir = "your_dir";
$order_no = 300;
$cust_name = "ABC";
$amount = 200;
foreach(glob($dir . '/*.pdf') as $file) 
{ 
    $dot = strrpos($file, '.');
    $file_amt = substr($file, 0, $dot);
    $file_amt = substr($file_amt, strrpos($file_amt, '/') + 1);
    $us = strrpos($file, '_');
    $file = substr($file, 0, $us);
    $file = substr($file, strrpos($file, '/') + 1);
    if($file == $order_no."_".$cust_name)
    { 
        if($file_amt == $order_no."_".$cust_name."_".$amount)
        {
            //do nothing
        }
        else
        {
            //if order no. and name matches
        }
    }
    else
    {
        //save directly
    }
}