crashwave Posted April 13, 2014 Share Posted April 13, 2014 (edited) Hello all I need your help with a code we are rewriting/updating files are scattered amongst several folders, one folder is the main folder (original). all folders have the same folder structure and most having identical file NAMES as the main folder. Those with identical file names are NOT, usually, the same file; they have been edited by the owner of that folder. Think of it like git master/original and forks/branches. the code we have accesses files in EITHER the main folder or the 'forks' using a function that returns the directory like this main_folder().'filepath'; OR fork_folder().'filepath'; or at times like this <?php echo main_folder(); ?>filepath what I need is to change main_folder() to equal fork_folder() but only if 'filepath' exists in that folder, if it doesn't then main_folder() does not change. So essentially whenever main_folder() is called I need to find what comes after it ; ?> Or ;?> OR .' get the file that follows, IF ANY, figure out if it exists in fork and change main_folder() or not. It was suggested to me that the only way to do this is to incorporate js in the code. But there has to be a completely php way do this. it would have been much easier if main_folder() and fork_folder() were changed to main_folder('filepath') fork_folder('filepath') but that is not an option. Thanks for any input. Edited April 13, 2014 by crashwave Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/ Share on other sites More sharing options...
denno020 Posted April 13, 2014 Share Posted April 13, 2014 You could use a combination of is_dir() and file_exists() to determine if the $fork_folder/$fork_file exists. Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475920 Share on other sites More sharing options...
crashwave Posted April 13, 2014 Author Share Posted April 13, 2014 yes denno020, but how do I get the file to begin with to check if it exists. That is main, 1st hurdle. I need main_folder().'filepath'; to be changed to fork_folder().'filepath'; whenever main_folder() is called in the code which is outside the folders. I have it working if I specify filepath using function hme_url($file) { $file = ltrim( $file, '/' ); if ( empty( $file ) || ( false !== strpos( $file, '..' ) ) ) { //??? $url = trailingslashit(main_folder());//main or fork } elseif (forks_avail() && file_exists( trailingslashit( fork_folder() ) . $file ) ) { $url = trailingslashit( fork_folder() ); } else { $url = trailingslashit( main_folder() ); } return $url.$file ; } can be prob written better with globals and change main_folder() in the function but that isn't the main issue. Getting the $file part that is after the main_folder() request is the problem. Then I have to deal with going back after getting $file and replacing the whole string with fork_folder(). $file. figuring out if it exists is the easy part. Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475924 Share on other sites More sharing options...
denno020 Posted April 13, 2014 Share Posted April 13, 2014 (edited) Nothing can be written better with globals. I need main_folder().'filepath'; to be changed to fork_folder().'filepath'; whenever main_folder() is called in the code which is outside the folders. Does this mean that if main_folder() is called in a php file that is not inside a fork_folder directory, you want main_folder() to be replaced with fork_folder()? If that's the case, then you could use __DIR__ to find out the absolute path of the current PHP file, then search the path for a folder that you would expect to find in a fork_folder file, if it's not there, then that mean's you're not currently in one of the fork_folders, so switch main_folder() to fork_folder(), like: $currentDir = __DIR__; if (preg_match("/fork_folder_name/", $currentDir)) { fork_folder(); } else { main_folder(); } Edited April 13, 2014 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475926 Share on other sites More sharing options...
crashwave Posted April 13, 2014 Author Share Posted April 13, 2014 Thanks for reply. Yes most of the code is outside the folders but not fork_folder either. I haven't used Joomla/Wordpress in a while but I think it could be considered similar to their module/template structure. say the main code requests the module folder file, but there are forks/children of the module with changes. they are not totally duplicates of the module just change a few things in it. The easiest thing is say an image. wordpress I think sees the main_module and gets it's files. say it calls $main_folder./image.png but I want it to get $fork_folder./image.png instead if that one exists. But the way it is written in the main code is the problem I cannot figure out how to get the filename because it's usually echo/return/include main_folder() . 'filename' Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475927 Share on other sites More sharing options...
denno020 Posted April 13, 2014 Share Posted April 13, 2014 I'm struggling to follow exactly what you want here.. You keep saying about using a file from fork_folder if it exists, which keeps leading me back to using file_exists().. Like this: if (file_exists($fork_folder."/image.jpg")) { include/echo/return $fork_folder."/image.jpg"; } else { include/echo/return $main_folder."/image.jpg"; } If this isn't what you're after, then you'll have to try and explain the problem a bit better, because as I said, I'm struggling to follow exactly what you want.. Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475929 Share on other sites More sharing options...
crashwave Posted April 13, 2014 Author Share Posted April 13, 2014 Yes exactly but I do not know the file is /image.jpg until after main_folder()."/image.jpg" is already requested. the way the code is echo '<img src="'. main_folder() . '/image.jpg">'; but it could be anything /file.pdf, anotherphp.php How do I get the file that is requested after main_folder() function to figure out if that file exists in fork_folder. doing a file exists is easy if I knew what file it's looking for at the time. Quote Link to comment https://forums.phpfreaks.com/topic/287724-replace-function-value-depending-on-whats-after-function/#findComment-1475934 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.