PHP finding file where post INCLUDES portion of filename

限于喜欢 提交于 2019-12-11 17:28:51

问题


I am posting a variable to a PHP process in an attempt to find a file in a directory.

The problem is the filename is much longer than what the user will submit. They will only submit a voyage number that looks like this:

222INE

Whereas the filename will look like this:

CMDU-YMUNICORN-222INE-23082016.txt

So I need PHP to be able to look into the directory, find the file that has the matching voyage number, and confirm it's existence (I really need to be able to download said file, but that'll be for a different question if I can't figure that out).

Anyway, so here is the PHP process that takes a posted variable:

<?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";
    }
  }
?>

The filename will always begin with CMDU. The second part may vary. Then the voyage number. The the date, followed by txt.


回答1:


Ok, first, you must do a directory listing

<?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
    }
  }
?>



回答2:


You may use the scandir function.
It will return an array of files inside a directory.
So, you can do something like this:

$dir = "backup/";  
$files = scandir( $dir );
$myFile = null;  

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

I know this code may have some errors, but I would try something like this.




回答3:


I'd use the scandir function to get a list of files in the backup directory and filter it down to the appropriate file.

$voyageFiles = array_filter( scandir( "backup" ), 

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



回答4:


your regix pattern:

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

you can use $pattern variable in the preg_match function



来源:https://stackoverflow.com/questions/45312636/php-finding-file-where-post-includes-portion-of-filename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!