0

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.

4
  • why not store the full path of the file in the database? Commented Oct 9, 2012 at 10:36
  • @HorusKol i did edit my question to add some information about the project. So i really want to do that by recursive iterator. if you have any clue on what's wrong, i'll take it with lot of joy :) Commented Oct 9, 2012 at 12:27
  • I am sorry but I really didn't get the essence of the problem. Could you reduce your question to the important parts? Commented Oct 9, 2012 at 12:51
  • as I said first : "I would like to find a file in a folders tree and i would like to obtain its path." i have the name of the file (but there is no extension, so i can't search for '.jpg' (for example) ) So to do this, i need recursive functions or recursive iterator (which seems to be more comfy to code) Commented Oct 9, 2012 at 13:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @dan-lee, i've find it out before reading your comment :) i'll answer my own question to give the code.. But maybe you could help me with a copy/unlink file..
0

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 )

2 Comments

Use rename() for that (as moving is nothing different than renaming).
So many thanks @dan-lee ! Well, as usual Sugar got me into some troubles... : i replaced copy($old_path,$new_path); with rename($old_path,$new_path); and, well, it works well, when i save a nnew Doc (let's say Doc01), and then edit it few times..But sugar keep the last folder_name (say Folder05) in some memory and when i want to upload a new Doc (called Image01), even if i give another folder name like FolderImg, it renames my new doc into Folder05.. and i don't know how to fix it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.