Jump to content

[SOLVED] Searching File Name For String With PHP


shanejeffery86

Recommended Posts

Hey guys.

 

I have some files on my web server and I need to search through their files name with a certain string.  That string will change based upon the record that the loop is on (using a foreach loop to run through my array).

 

Right now, the files are named like such -- ESC-440_Paper_Bassalee.

 

Therotically, if I could get all of the file names in that specific folder to be converted to string names, that would be ideally.  Then I could just explode the strings with the "_" delimiter and then search the arrays for the relationship.

 

Anyone have ANY idea how to handle this?

 

Thanks.

<?php

 

ini_set("memory_limit", "160M");

 

error_reporting(E_ALL);

 

include("database_connection.php");

include("../scheduler_sv/lib/DB.class.php");

require_once '../SpreadsheetReader/SpreadsheetReaderFactory.php';

$spreadsheetsFilePath = '/var/www/html/cmp/downloads/boston_speaker.csv';

$reader = SpreadsheetReaderFactory::reader($spreadsheetsFilePath);

$sheets = current($reader->read($spreadsheetsFilePath));

 

foreach($sheets as $record)

{

// echo '<pre>';

// print_r($record);

// echo '</pre>';

$session_code = $record[0];

$speaker_name = $record[1]." ".$record[2];

$session_name = $record[3];

$session_date_result = explode(",", $record[4]);

$session_date = $session_date_result[0];

$session_conference = 226;

 

$query = "INSERT INTO phpfox_downloads (conference, session_code, session_name, date, author_name)

VALUES ('".es($session_conference)."', '".es($session_code)."', '".es($session_name)."',

'".es($session_date)."', '".es($speaker_name)."')";

mysql_query($query) or die(mysql_error());

}

 

function es($var) {

return mysql_real_escape_string($var);

}

 

?>

GOT IT!

 

Code:

$source_folder = '/var/www/html/cmp/downloads/esc_boston/';
$sec = 1;
$limit = 200;

$file_list = glob_files($source_folder, $sec, $limit);

 

Function Call:

 

function glob_files($source_folder, $sec, $limit){
    if( !is_dir( $source_folder ) ) {
        die ( "Invalid directory.\n\n" );
    }
   
    $FILES = glob($source_folder."/*");
    $set_limit    = 0;
   
    foreach($FILES as $key => $file) {
   
        if( $set_limit == $limit )    break;
       
        if( filemtime( $file ) > $sec ){
       
            $FILE_LIST[$key]['name']    = substr( $file, ( strrpos( $file, "/" ) +1 ) );   
            $set_limit++;
       
        }
       
    }
    if(!empty($FILE_LIST)){
        return $FILE_LIST;
    } else {
        die( "No files found!\n\n" );
    }
}

 

Thank you VERY much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.