【问题标题】:PHP finding file where post INCLUDES portion of filenamePHP查找包含文件名部分的帖子的文件
【发布时间】:2017-07-25 20:11:07
【问题描述】:

我正在向 PHP 进程发布一个变量,以尝试在目录中查找文件。

问题是文件名比用户提交的要长得多。他们只会提交如下所示的航次编号:

222INE

而文件名看起来像这样:

CMDU-YMUNICORN-222INE-23082016.txt

所以我需要 PHP 能够查看目录,找到具有匹配航次号的文件,并确认它的存在(我确实需要能够下载所述文件,但那将是不同的问我是否无法弄清楚)。

不管怎样,这里是接受一个发布变量的 PHP 进程:

<?php
  if($_POST['voyage'] == true)
  {
    $voyage = mysqli_real_escape_string($dbc, $_POST['voyage']);
    $files = glob("backup/................."); // <-this is where the voyage will go
    // it should look like this
    // $files = glob("backup/xxxx-xxxxxxxx-222INE-xxxx.txt");

    if(count($files) > 0)
    {
      foreach($files as $file)
      {
        $info = pathinfo($file);
        echo "File found: " . $info["name"];
      }
    }
    else
    {
      echo "File doesn't exist";
    }
  }
?>

文件名总是以 CMDU 开头。第二部分可能会有所不同。然后是航次号。日期,后跟 txt。

【问题讨论】:

    标签: php file glob


    【解决方案1】:

    好的,首先,你必须做一个目录列表

    <?php
      if($_POST['voyage'] == true)
      {
        $voyage = $_POST['voyage']; //in this case is not important to escape
        $files = scandir("backup"); // <-this is where the voyage will go ***HERE YOU USE DIR LISTING***
       unset($files[0], $files[1]) // remove ".." and ".";
    
        if(count($files) > 0)
        {
          $fileFound = false;
          foreach($files as $file)
          {
    
            if((preg_match("/$voyage/", $file) === 1)){
              echo "File found: $file \n";
              $fileFound = true;
            }
    
          }
           if(!$fileFound) die("File $voyage doesn't exist"); // after loop ends, if no file print "no File"
        }
        else
        {
          echo "No files in backup folder"; //if count === 0 means no files in folder
        }
      }
    ?>
    

    【讨论】:

    • 我相信这就是我想要的。仍在测试,但到目前为止,这是可行的。我不得不将 ("backup") 更改为 ("backup/") 才能正常工作。另外,我也不需要未设置的部分。接受您的回复作为答案。谢谢先生。
    • 告诉我是否可行,由于文件系统请求,我无法在在线测试仪上进行测试(或向您演示)。
    【解决方案2】:

    您可以使用scandir 函数。
    它将返回目录中的文件数组。
    所以,你可以这样做:

    $dir = "backup/";  
    $files = scandir( $dir );
    $myFile = null;  
    
    foreach( $files as $each ) {
        if(preg_match(/*some magic here*/, $each)) {
            $myFile = $dir . $each;
    }  
    return $myFile;  
    

    我知道这段代码可能有一些错误,但我会尝试这样的。

    【讨论】:

    • 看起来很有希望,但是,我会用什么来代替 /*some magic here*/ ?
    【解决方案3】:

    我会使用 scandir 函数来获取备份目录中的文件列表并将其过滤到适当的文件中。

    $voyageFiles = array_filter( scandir( "backup" ), 
    
         function($var) use ($voyage) { 
            // expand this regexp as needed. 
            return preg_match("/$voyage/", $var ); 
         } 
    )
    $voyageFile = array_pop( $voyageFiles ); 
    

    【讨论】:

    • 在这种情况下我无法回显 $voyageFile。有什么想法吗?
    【解决方案4】:

    你的正则表达式:

    $voyage = $_POST['voyage'];
    $pattern = '/^CMDU-.*-'.$voyage.'-.*\.txt/';
    

    你可以在 preg_match 函数中使用$pattern 变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2011-08-02
      • 1970-01-01
      相关资源
      最近更新 更多