0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89

4 Answers4

1

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.

silver
  • 64
  • 1
  • 1
  • 8
1

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 ); 
serverSentinel
  • 994
  • 6
  • 20
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
    }
  }
?>
Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
  • I do believe this is what I was looking for. Still testing, but so far, this works. I had to change ("backup") to ("backup/") for it to work though. Also, I did not need the unset portion either. Accepting your response as the answer. Thank you sir. – John Beasley Jul 25 '17 at 20:48
  • tell me if works, I cannot test (or demo to you) on an online tester due a filesystem request. – Oscar Zarrus Jul 25 '17 at 20:49
0

your regix pattern:

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

you can use $pattern variable in the preg_match function