Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Need help for selecting file from directory!!!

    Hi all,
    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.

    Thanks,
    Ramki.

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi,

    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!

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok..

    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.


    Thanks,
    Ramki.

  • #4
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ok, I nearly understand! Do you mean that the whole filename is "48" or that the string "48" appears somewhere in the filename?

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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).

    Thanks,
    Ramki.

  • #6
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    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!):

    PHP Code:
    $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;
          break;
        }
    }

    if (
    $filename)
      echo 
    file_get_contents($filename);  //Again, only PHP 5.X
    else
      throw new 
    Exception("Couldn't find the file!"); 

  • #7
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.





    Thanks,
    Ramki.
    Last edited by ramki067; 02-18-2008 at 12:13 PM.

  • #8
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Is scandir working on your system?

  • #9
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.
    Last edited by ramki067; 02-18-2008 at 01:25 PM.

  • #10
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Ok ... firstly, what is your php version? You can use phpversion 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 in your php.ini, if that is possible (i.e. if you are not running on a shared server). Otherwise use curl functionality to store the remote page contents in a local string using output buffering. 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.
    Last edited by mlse; 02-18-2008 at 02:07 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •