John Beasley
John Beasley

Reputation: 3081

PHP finding file where post INCLUDES portion of filename

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.

Upvotes: 0

Views: 149

Answers (4)

Masih Fathi
Masih Fathi

Reputation: 25

your regix pattern:

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

you can use $pattern variable in the preg_match function

Upvotes: 0

Oscar Zarrus
Oscar Zarrus

Reputation: 790

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

Upvotes: 1

serverSentinel
serverSentinel

Reputation: 994

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 ); 

Upvotes: 1

silver
silver

Reputation: 64

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.

Upvotes: 1

Related Questions