MrLarkins Posted August 14, 2009 Share Posted August 14, 2009 Hello, My problem: I have many pages that have urlencoding. I'd like to write a script that will open all the files in a folder and subsequent sub-folders that are .html extension and urldecode them and overwrite the original with the decoded. Is this going to be hard for me? Where do I start? Quote Link to comment Share on other sites More sharing options...
trq Posted August 14, 2009 Share Posted August 14, 2009 Is this going to be hard for me? Wouldn't know. Where do I start? glob, file_get_contents and file_put_contents. Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 how about this? i was working on this while you were posting <?php $current_dir = "$DOCUMENT_ROOT"."algebra1/"; $dir = opendir($current_dir); while ($file = readdir($dir)) { $parts = explode(".", $file); if (is_array($parts) && count($parts) > 1) { $extension = end($parts); if ($extension == "html") $html_file=fopen("$file","r+"); urldecode($html_file); fclose($html_file); } } closedir($dir); ?> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 Warning: fclose(): supplied argument is not a valid stream resource in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 15 Warning: fopen(footer.html) [function.fopen]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 11 guess not...whats that stuff mean? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html' as $file) { file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file", urldecode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file"))); } Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 again, i was working while you were posting, lol...and it seems yours is shorter than mine. I'm going to try yours, but please take a look at mine and tell me where i went wrong...b/c it makes logical sense to me but doesn't work. <?php $current_dir = "$DOCUMENT_ROOT"."algebra1/"; $dir = opendir($current_dir); while ($file = readdir($dir)) { $parts = explode(".", $file); if (is_array($parts) && count($parts) > 1) { $extension = end($parts); if ($extension == "html") $encoded=file_get_contents($file); $decoded=urldecode($encoded); file_put_contents($file,$decoded); } } closedir($dir); ?> edit** yours give an error Parse error: syntax error, unexpected T_AS in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 2 Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 Simple mistake. foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file", urldecode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file"))); } Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 lol, yours is returning the same errors as mine...i don't feel so dumb now failed to open stream: No such file or directory Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 What does this produce? <pre> print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html')); </pre> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 <?php <pre> print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html')); </pre> ?> produces Parse error: syntax error, unexpected '<' in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 3 while <pre> print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html')); </pre> produces print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html')); Quote Link to comment Share on other sites More sharing options...
Markh789 Posted August 15, 2009 Share Posted August 15, 2009 Because it contains HTML, set the tags outside. <pre> <?php print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html')); ?> </pre> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 ok, thanks. here's the output Array ( [0] => /home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html [1] => /home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html ) and yes, those are the only two files but keep in mind, there are many other folders with many other html files in there that need to be urldecoded and of course this is the full error message Warning: file_get_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4 Warning: file_put_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html) [function.file-put-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4 Warning: file_get_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4 Warning: file_put_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html) [function.file-put-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4 Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 i modified your code and it seemed to work <?php foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { file_put_contents("$file", urldecode(file_get_contents("$file"))); } ?> but how do i get it to work into the subdirectories as well? Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 15, 2009 Author Share Posted August 15, 2009 I had to modify the code a bit to strip out some now un-needed scripting garbage new code still does not did into the lower directories and do its magic! <?php foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { $decode=urldecode(file_get_contents("$file")); $s1=str_replace('<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("',"",$decode); $s2=str_replace('"));//--></SCRIPT>',"",$s1); $s3=str_replace('<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("',"",$s2); $s4=str_replace('<SCRIPT LANGUAGE="JavaScript"><!-- eval(unescape("document.write=null;',"",$s3); file_put_contents("$file","$s4"); } ?> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 16, 2009 Author Share Posted August 16, 2009 still working on it, still having problems here's what i got so far <?php $current_dir = glob($_SERVER['$DOCUMENT_ROOT']."algebra1/"); foreach($current_dir as $dir) { $dir1 = opendir($dir); while ($file = readdir($dir1)) { $parts = explode(".", $file); if (is_array($parts) && count($parts) > 1) { $extension = end($parts); if ($extension == "html") $decode = urldecode(file_get_contents($file)); $s1 = str_replace('<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("',"",$decode); $s2 = str_replace('"));//--></SCRIPT>',"",$s1); $s3 = str_replace('<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("',"",$s2); $s4 = str_replace('<SCRIPT LANGUAGE="JavaScript"><!-- eval(unescape("document.write=null;',"",$s3); file_put_contents($file,$s4); } } } ?> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 17, 2009 Author Share Posted August 17, 2009 I'm still having problems. My code gives the following: Warning: file_put_contents(.) [function.file-put-contents]: failed to open stream: Is a directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 21 Warning: file_put_contents(..) [function.file-put-contents]: failed to open stream: Is a directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 21 what is with the 'file_put_contents(.)' and 'file_put_contents(..)' ? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 . and .. represent the current and parent directory I believe. They're included when using readdir(), so you have to skip them in your code (or use the simpler glob() exclusively). Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 17, 2009 Author Share Posted August 17, 2009 (or use the simpler glob() exclusively). i tried using the glob() first, but it didn't dig into the subdirectories...or did i do it wrong? thorpe helped with it again, this is what was used foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file", urldecode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file"))); } but it only returned the files in the immediate folder, not all the other files in the subdirectories Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 You will need to call glob() recursively, to dig into subdirectories. There's a user function on the manual page. Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 17, 2009 Author Share Posted August 17, 2009 You will need to call glob() recursively, to dig into subdirectories. sorry, but I have no idea how to ... There's a user function on the manual page. because that all looks like gibberish to me...i wish i were smarter. believe it or not, I am trying. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 No worries. Here's an example using that user function: <?php function rglob($pattern, $flags = 0, $path = '') { if (!$path && ($dir = dirname($pattern)) != '.') { if ($dir == '\\' || $dir == '/') { $dir = ''; } return rglob(basename($pattern), $flags, $dir . '/'); } $paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT); $files = glob($path . $pattern, $flags); foreach ($paths as $p) { $files = array_merge($files, rglob($pattern, $flags, $p . '/')); } return $files; } foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { //file_put_contents($file, urldecode(file_get_contents($file))); echo "$file<br />"; } ?> I'm not too sure about the paths used in the foreach loop, so it prints them for now so you can check if they are right, before you modify the files. Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 17, 2009 Author Share Posted August 17, 2009 wow! that shows a lot of files! i knew there would be a bunch, just didn't realize how many. see for yourself. ( over a 1000 ) and now i'm wondering about the string replacement that i need to do on all these files. can i do it like <?php function rglob($pattern, $flags = 0, $path = '') { if (!$path && ($dir = dirname($pattern)) != '.') { if ($dir == '\\' || $dir == '/') { $dir = ''; } return rglob(basename($pattern), $flags, $dir . '/'); } $paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT); $files = glob($path . $pattern, $flags); foreach ($paths as $p) { $files = array_merge($files, rglob($pattern, $flags, $p . '/')); } return $files; } foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { file_put_contents($file, urldecode(file_get_contents($file))); file_put_contents($file, str_replace('<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("',"",file_get_contents($file))); } ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 You would do it like this: <?php $replace = array( '<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("', '"));//--></SCRIPT>', '<SCRIPT LANGUAGE="Javascript"><!-- document.write(unescape("', '<SCRIPT LANGUAGE="JavaScript"><!-- eval(unescape("document.write=null;' ); foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { $data = urldecode(file_get_contents($file)); $data = str_replace($replace, '', $data); file_put_contents($file, $data); } ?> Quote Link to comment Share on other sites More sharing options...
MrLarkins Posted August 17, 2009 Author Share Posted August 17, 2009 tried it, and it says it did it, but the files still show the code that wasn't replaced. i'm checking the strings that need to be replaced character by character. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 17, 2009 Share Posted August 17, 2009 If it's because of the many spaces, you could try with regular expressions: <?php $replace = array( '~<SCRIPT LANGUAGE="Javascript"><!--\s*document\.write\(unescape\("~i', '~"\)\);//--></SCRIPT>~i', '~<SCRIPT LANGUAGE="Javascript"><!--\s*document\.write\(unescape\("~i', '~<SCRIPT LANGUAGE="JavaScript"><!--\s*eval\(unescape\("document\.write=null;~i' ); foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) { $data = urldecode(file_get_contents($file)); $data = preg_replace($replace, '', $data); file_put_contents($file, $data); } ?> 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.