View Full Version : Need help for selecting file from directory!!!
02-18-2008, 10:37 AM
I've a HTML page from where i pass a variable into another page. based on that variable say in my case 32,44 and 48 are the variables. Now,I need to go to this directory(which is constant) and find the file with say if 48 is entered, file name having 48 needs to be searched and the whole file name is to be displayed . How do i do this? Please help.
Could you perhaps explain a little more? I appreciate that English may not be your first language, but I am having trouble understanding your post!
02-18-2008, 10:51 AM
I'm having an HTML page from where i pass a value 48 to another page. So this another page has to go to a particular directory where a file is stored. The code has to choose a particular file having name 48 in it and display its contents on the page.
Ok, I nearly understand! Do you mean that the whole filename is "48" or that the string "48" appears somewhere in the filename?
02-18-2008, 11:05 AM
yes mise.... only a part of the file name contains 48 ... the code needs to search the file with 48 in it(it contains a long name having 48 as part of the string).
Ok, now I have complete understanding! :)
This should work (Note that I haven't tested it or thought of a neat way to make the code smaller!):
$value = 48;
$directory = "whatever";
$list = scandir($directory); //Note - this only works in PHP 5.X and later.
foreach ($list as $item)
if (strpos($item, $value) !== FALSE)
$filename = $directory."/".$item;
echo file_get_contents($filename); //Again, only PHP 5.X
throw new Exception("Couldn't find the file!");
02-18-2008, 12:03 PM
thanks mise.. but file_get_contents is not working on my php.. may be i'm having lesser version than 5x. Can u please give me an alternative code which works for below 5x version. Also i do not need to print the whole contents of the file.
The file is a .txt file where contents are written using tab. That is after one command their is a tab space. I need to extract a word based on this tab space. Like for example,
settop decode -audio 0x1 -mpeg_type ASF -audio_type 0x86 /wma/L2/WMA_V4.0/test1_WMA_40_v2_128kbps_48kHz_2.asf test1_WMA_40_v2_128kbps_48kHz_2 C:\Audio_Regress\recorded_files 90 48000
from the above line till asf its a command and the test1_WMA_40_v2_128kbps_48kHz_2 after asf is the thing which i need to extract and after asf it is tab space. Their are around 1000 lines like these from which i need to extract the second column word.
Is scandir working on your system?
02-18-2008, 01:14 PM
no mlse... scandir is also not working :(
Even fgets is not working... i don't know whats wrong :(
But i need to say something...The file which i'm trying to open and read is not on the local machine! It on another machine and i'm trying to open and then read from it... Is it ok. I tried that location from my local machine and i was able to open it.
Ok ... firstly, what is your php version? You can use phpversion (http://uk2.php.net/manual/en/function.phpversion.php) to find that out.
To open files on a remote machine using fopen or file_get_contents, you need to switch on allow-url-fopen (http://uk2.php.net/manual/en/ref.filesystem.php#ini.allow-url-fopen) in your php.ini, if that is possible (i.e. if you are not running on a shared server). Otherwise use curl (http://uk2.php.net/manual/en/ref.curl.php) functionality to store the remote page contents in a local string using output buffering (http://uk2.php.net/manual/en/ref.outcontrol.php). If you're on a shared host and you don't have allow-url-fopen or curl and your PHP version is pre 5.X then, to use the vernacular, you are buggered.
To strip out the file from the command, use preg_match (http://uk2.php.net/manual/en/function.preg-match.php).
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.