Gaelle
Gaelle

Reputation: 614

php & recursive Iterator : finding a specific file and its path

I'm a newbie in SPL and recursiveIterator... So could you help me?

What I want to achieve :

I would like to find a file in a folders tree and i would like to obtain its path.

My folder tree could seems to be like this :

./ressources
./ressources/Images
./ressources/Images/Image01
./ressources/Images/Image02
./resources/Images/ImagesSub/Image03
./ressources/Docs
./ressources/Docs/Doc01

and so on...

I obtain the name of my File with sql query (warning : they never have an extension). Now, i want to find the file's location by doing a recursive Iterator on './ressources' folder.

Then, when i've found the file, i would like to return the whole link './ressources/Folder/File'.

I've read Gordon's solution but it doesn't work, I tried only to echo something, but doesn't display anything.

Here is my code :

$doc_id = $bean->id;
$query = "SELECT a.document_revision_id  FROM  documents as a, document_revisions as b ";
$query .= "WHERE a.document_revision_id = b.id AND a.id = '" . $doc_id . "' LIMIT 1";
$results = $bean->db->query($query, true);
$row = $bean->db->fetchByAssoc($results);

$file_id = $row['document_revision_id'];
$ressources = './ressources/';
$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ressources, RecursiveDirectoryIterator::KEY_AS_FILENAME), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $entry) {
    if ($entry->getFilename() === $file_id){
        echo '<script> alert('.$entry->getFilepath().');</script>';
    }
}

(i know doing an alert into a echo is bullsh*t, but whith sugar it is quite difficult to display something Specifications

I'm trying to do this in a SugarCrm CE 6.5.2 logic_hook and it's running on archlinux. And my PHP version is 5.4.6

It is really urgent, so I would be reaaaally happy if you could help me!!

Thanks by advance!

EDIT FROM 12/10/09 2pm: What is my sugar project and why i can't get the pathname from my database

I created a custom field in Documents module called folder_name_c. You fill it with the name of the folder (under ressources) where you want to upload your document.

I want to allow the user to move the file uploaded from its ancient folder to new one when i edit the document.

When editing a document, I did a after_retrieve hook to permit the logic_hook to work when editing (before, it was just done for edit view)

So, if i get the $bean->folder_name_c, it pick up the field's content. If i try sql, it will pick the folder_name_c only after i click "save".

So, i don't have any clue to get my old folder_name to create an

$old_link = '.ressources/'.$old_folder.'/'.$file_id;

I can only create the

$new_link = '.ressources/'.$bean->folder_name_c.'/'.$file_id;

So, after a long time, i figured out that i could browse my ressources folder and my sub folders to find my file named $file_id and then create the $old_link

FYI, by creating a new custom field under studio in sugar, i gained a lot of time. I don't want to pass my life on adding a custom_code calling database or else. this is URGENT and recursive iterator seems to be simple and quick.

Upvotes: 0

Views: 565

Answers (2)

Gaelle
Gaelle

Reputation: 614

So, I've found out how to use recursive iterators for my problem! Here is my code :

$ressources = './ressources/';
$directoryIter = new RecursiveDirectoryIterator($ressources);
$iter = new RecursiveIteratorIterator($directoryIter, RecursiveIteratorIterator::SELF_FIRST);
$old_path = '';
$new_path = $ressources.$bean->folder_name_c.'/'.$file_id;
chmod($new_path,0777);

foreach ($iter as $entry) {

    if (!$directoryIter->isDot()) {

        if ($entry->getFileName() == $file_id) {
            $old_path = $entry->getPathName();
            chmod($old_path, 0777);
            copy($old_path,$new_path);

         }
    }
}

So i succeed to get my file's origin path! :)

But as always, there is a problem:

I want to cut and paste my file from $old_path to $new_path (as you can see in my code). The copy here works well, but i don't know where i have to unlink() the old_path.. if anyone knows ... (and if i wrote the copy in the wrong line, just tell me please! :D )

Upvotes: 0

dan-lee
dan-lee

Reputation: 14502

There is no method such as getFilepath for the (Recursive)DirectoryIterator, just use $entry itself, when used in a string context it's casted to such one (à la __toString):

$file_id = 'test';
$ressources = './ressources/';

// [...]

echo '<script>alert('.$entry.');</script>'; // is casted to a string which contains the full path

// results in alerting ./resources/SubFolder/test

I tested it with the same structure and without extension.

Upvotes: 1

Related Questions