eo92866 Posted December 7, 2010 Share Posted December 7, 2010 i'd like to only obtain the file path and extension of a file... and then only utilize that information in $handle. [php $path = "/my/file/path/US_*.xls"; echo dirname($path); $pathinfo = pathinfo($path); $extension = $pathinfo['.xls']; echo basename($pathinfo); echo basename($extension); $handle = fopen("$extension", "r"); [/code] can someone please assist? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 I have no idea what you're talking about, but a shot in the dark: $pathinfo = pathinfo('/path/to/somefile.xls'); $files = glob($pathinfo['dirname'] . '/*.' . $extension); Quote Link to comment Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 i'd like to only obtain the file path and extension of a file... Easy... reading the documentation for pathinfo() shows you how. $path = "/my/file/path/US_*.xls"; $info = pathinfo($path); $path = $info['dirname']; $ext = $info['extension']; and then only utilize that information in $handle. How exactly do you want to utilize the path/extension in $handle? Using only a path and/or extension doesn't make much sense for opening a file. Are you trying to do what AbraCadaver guessed at: get a listing of all .xls files in a directory? Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 hi... thank you for the responses... i read the info on php.net what i'm exactly trying to do is... load a directory /my/path/name and since there are multiple files with the same extension in that folder... i'd like to only search/return the first portion of the file name... essentially narrowing down the results of the differing files and load the variable/array and then into $handle. i've been trying to figure a way to use basename to implement this??? $handle will essentially load the file for a loop that i will run later. the thinking behind this is to automate the loading of files to later run a parser later. hope i explained better??? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 hope i explained better??? Not really... Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 just mad a modification to the post. but i am trying to load a file by paring down the results with filepath, piece of file name and extension. then load the result into $handle. this would automate the loading of a file without having a user to do any kind of file uploading. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 just mad a modification to the post. but i am trying to load a file by paring down the results with filepath, piece of file name and extension. then load the result into $handle. this would automate the loading of a file without having a user to do any kind of file uploading. fopen() doesn't work that way. Ignore fopen() and basename() and any other PHP and give a step by step of what you want to accomplish. Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 1. load a file path 2. match only a file with the name US within the file path 3. and match only a file extension with .xls in that file path 4. load that match into fopen and $handle this is what i have so far: $files = array_diff (scandir("/my/file/path/") , array(".", "..") ); print_r($files); $filepathinfo = pathinfo($files); $extension = $filepathinfo['.xls']; echo basename($filepathinfo); $handle = fopen("$extension", "r"); Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 1. load a file path 2. match only a file with the name US within the file path 3. and match only a file extension with .xls in that file path 4. load that match into fopen and $handle Well, you can do the first 3, but as for 4, if there are multiple files you can't open them with one fopen(), it would need to be a loop. And to just read, you're better off with file_get_contents(). What do you want to do with the files? And don't say "load them into $handle"! This will put all the matching filenames into an array and then load the contents of each file into another array: $files = glob("/my/file/path/US_*.xls"); // just to see what is there print_r($files); foreach($files as $file) { $contents = file_get_contents($file); } // just to see what is there print_r($contents); Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 load into $handle. just kidding... i appreciate your assistance. the ultimate goal is to find the correlating matching file and have that .xls file have it's contents extracted and placed into a mysql table. but i don't want users to have to do any uploading of files... only select a file from a pull down menu that they want the .xls fields to be extracted and placed into mysql. so, how would i have the information be loaded to send into mysql. and i'm using mysql 5.0.45 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 Glob as I've shown, foreach over the files and echo them in a drop down or something in a form. When submitted, take the filename, get the data and insert into MySQL (you'll need to search on that, I have no idea how to extract data from an Excel file with PHP). Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 thank you... for the excel portion... i think i'm just going to go with .csv... haven't had much success with that. and was thinking of inserting vbs code into php to convert the .xls into .csv and then run the foreach loop... finally inserting into sql. very nice round about process. and thank you. Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 7, 2010 Author Share Posted December 7, 2010 i know you mentioned that fopen doesn't work with having glob inserted... you mentioned utilizing file_get_contents... would file_get_contents be the best function to insert glob into fgetcsv too??? Quote Link to comment Share on other sites More sharing options...
eo92866 Posted December 8, 2010 Author Share Posted December 8, 2010 so my final code that works... as i have the error_reporting turned on now $files = glob("/my/file/path/US_*.csv"); // just to see what is there #print_r($files); $max_loop = 1; $count = 0; //loop for iterating $files into array $file and then into $value foreach($files as $file => $value) { //increment the loop by 1 $count++; //set the variable $count to variable $max_loop if ($count==$max_loop) { //create a break, because now have made the counter equal to 1 break; //get the contents of $value $contents = file_get_contents($value); } } // just to see what is there #print_r($contents); // place $value into fopen... to be opened and into array $handle, the "r" stands for read only $handle = fopen("$value", "r"); hope it helps someone down the line as well. 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.