prototype18 Posted September 20, 2010 Share Posted September 20, 2010 I need to display multiple files that have a similar file name, but there are timestamps added to the filename once its been modified. What could I use in PHP to display all these files? Right now I use a $path variable that looks into a SQL table to build the path to the folder that the files reside in.. ELSEIF($Recordmyrow['Applicatio'] == "pdf file") { IF($securityvalue != "P"){$display = false;} IF(file_exists(substr($path, 5))){} ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";} } So there is a file called file1.pdf then another one called file1.mmddyyyy.pdf My code shows file1.pdf but not file1.mmddyyyy.pdf Is there some kind of wildcard I can use to get it to show all the files if they exist? Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 20, 2010 Share Posted September 20, 2010 http://www.jonasjohn.de/snippets/php/glob-examples.htm Quote Link to comment Share on other sites More sharing options...
Miss_Rebelx Posted September 20, 2010 Share Posted September 20, 2010 So your logic says... Check for securityvalue, if its not right, then set a boolean to false. Check for a file to exist (the file name starts at 5 characters into the $path variable), and if it exists do nothing. If it doesn't exist, save a link to the $column4 variable. Well, do you not want it to *do* something if the file exists? You say your code 'shows' one file but not another. Where does it 'show' it? I'm assuming $path is the full path to the pdf file, not just the directory? or? Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 20, 2010 Author Share Posted September 20, 2010 yes $path brings you right to the file, but now were adding new files that have the same name but just have a timestamp added to the end of the file right before the file extension type. Quote Link to comment Share on other sites More sharing options...
Miss_Rebelx Posted September 20, 2010 Share Posted September 20, 2010 Can you paste how you're constructing your $path variable? And where do you show these file names? (I don't see in the code you provided where these names do or don't show up so I can't tell why they are or aren't showing up as you want them) Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 20, 2010 Share Posted September 20, 2010 Assuming all the files will be pdf's and are in the same directory, here is all you need. $files = glob(substr($basefile, 0, -4)."*.pdf"); $files will be an array of all the files that match the $basefile with the same name in the format you specified above. NOTE: That example is case sensitive. So, if the primary name of the file has a different case than the other files or if some files use "pdf" while others use "PDF" you will not get all the matches. You can check the manual for glob() in the "User Contributed Notes" to see examples on how to make the matching case-insensitive. http://us3.php.net/manual/en/function.glob.php Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 20, 2010 Author Share Posted September 20, 2010 Okay, I screwed up somehow, because the code I put up before isn't where the path is being built... I guess... Here is the code that has an effect on the actual path... //Check display value and print row IF($display) { //Check if file exists IF(substr($path, 0, 5) == "file:") { IF(file_exists(substr($path, 5))) { IF(strtolower(substr($path, -4, 4)) == ".pdf") { $column4 = "<A HREF=pdf.php?src=".substr($path,5).">VIEW</A>"; } IF(strtolower(substr($path, -4, 4)) == ".jpg") { $column4 = "<A HREF=jpg.php?src=".substr($path,5).">VIEW</A>"; } } ELSE{$column4 = "Not Available";} } So can I throw the glob function somewhere in here...? Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 20, 2010 Author Share Posted September 20, 2010 When I try to change or apply the glob function to the code here, it doesn't work right. Please help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 20, 2010 Share Posted September 20, 2010 It appears that the file name is part of a string, specifically: file_exists(substr($path, 5) By the way, instead of using that same function multiple times, just assign that to a variable so the PHP parser doesn't need to run that function multiple times. Not knowing everything about your particular need, this is just a shot in the dark: //Check display value and print row if($display) { //Check if file exists if(substr($path, 0, 5) == "file:") { $filename = substr($path, 5); if(file_exists($filename)) { $basename = substr($filename, 0, strrpos($filename, '.')); $ext = strtolower(substr(strrchr($filename, '.'))); $files = glob("{$basename}.*.pdf"); foreach($files as $file) { if($ext == ".pdf") { $column4 = "{$file} <a href=\"pdf.php?src={$file}\">VIEW</a><br />\n"; } if($ext == ".jpg") { $column4 = "{$file} <a href=\"jpg.php?src={$file}\">VIEW</a><br />\n"; } } } else { $column4 = "Not Available"; } } } Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 21, 2010 Author Share Posted September 21, 2010 I don't think the glob function is working because the files that need to be accessed are on a shared network drive. Would that screw things up? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 21, 2010 Share Posted September 21, 2010 I don't think the glob function is working because the files that need to be accessed are on a shared network drive. Would that screw things up? As long as you can access the "default" file I see no reason you wouldn't be able to access the other files in the same directory. You say you don't think it is working but have provided no details as to why you believe it is not working. Give an example of a default file name and the other files in the same directory that should match. I tested the logic above so I know it works. Using a folder with the following files: convert.file_size.php convert.images.php convert.php create mp3 playlist.php I used the following code: $path = 'file:temp/convert.php'; $filename = substr($path, 5); $basename = substr($filename, 0, strrpos($filename, '.')); $ext = strrchr($filename, '.'); $allfiles = glob("{$basename}.*.php"); And get the following output: Array ( [0] => temp/convert.file_size.php [1] => temp/convert.images.php ) Quote Link to comment Share on other sites More sharing options...
prototype18 Posted September 21, 2010 Author Share Posted September 21, 2010 Okay, this is the code that builds the $path variable. Your code did kind of work, just didn't give the desired result. The path variable is looking at a SQL table for these UNC paths to folders on the network. //Replace variables in path $path = $Recordmyrow['Path']; $path = str_replace("['XXX']",$item,$path); $path = str_replace("['XXX']",$sqlmyrow['XXX''],$path); $path = str_replace("['XXX']",$sqlmyrow['XXX''],$path); $path = str_replace("['XXX']",$sqlmyrow['XXX''],$path); $path = str_replace("['XXX']",$sqlmyrow['XXX''],$path); $path = str_replace("['SESSIONID']",session_id(),$path); $path = str_replace("['APPLICATIO']",$applicatio,$path); $path = str_replace("['APPLICATIO3']",substr($applicatio,3),$path); $path = str_replace("['YEAR24']",substr($applicatio,2,4),$path); $path = str_replace("['YEAR34']",substr($applicatio,3,4),$path); $path = str_replace("['YEAR44']",substr($applicatio,4,4),$path); $path = str_replace("['YEAR54']",substr($applicatio,5,4),$path); $path = str_replace("['LATITUDE']",$sqlmyrow['LAT'],$path); $path = str_replace("['LONGITUDE']",$sqlmyrow['LON'],$path); // ELSEIF($Recordmyrow['Applicatio'] == "CO") { IF($securityvalue == "P"){$display = false;} IF(file_exists(substr($path, 5))){} ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";} } // ELSEIF($Recordmyrow['Applicatio'] == "CO (Printable)") { IF($securityvalue != "P"){$display = false;} IF(file_exists(substr($path, 5))){} ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";} } //Check display value and print row IF($display) { if(substr($path, 0, 5) == "file:") { $filename = substr($path, 5); if(file_exists($filename)) { $basename = substr($filename, 0, strrpos($filename, '.')); $ext = strtolower(substr(strrchr($filename, '.'))); $files = glob("{$basename}.*.pdf"); foreach($files as $file) { if($ext == ".pdf") { $column4 = "{$file} <a href=\"pdf.php?src={$file}\">VIEW</a><br />\n"; } if($ext == ".jpg") { $column4 = "{$file} <a href=\"jpg.php?src={$file}\">VIEW</a><br />\n"; } } } else { $column4 = "Not Available"; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.