Jump to content

basename question


eo92866

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

load into $handle.  :D 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.