alixaxel's solution to "Use PHP to delete file types in a directory and its sub directories"

To strip Javascript and PHP code from HTML files, you can simply use this function:<\/p>\n

function strip_code($<\/span>file<\/span>) {<\/span>\n  if (preg_match('~[.]html?$<\/span>~', $<\/span>file<\/span>) > 0) {<\/span>\n    $<\/span>data<\/span> = file_get_contents($<\/span>file<\/span>);\n    $<\/span>patterns<\/span> = [\n      '~<<\/span>[?](?:php)?.*?[?]>~gims',\n      '~<script<\/span>[^<\/span>><\/span>]*>(.*?)</script><\/span>~gims',\n    ];\n\n    foreach ($<\/span>patterns<\/span> as $<\/span>pattern<\/span>) {<\/span>\n      $<\/span>data<\/span> = preg_replace($<\/span>pattern<\/span>, '', $<\/span>data<\/span>);\n    }\n\n    file_put_contents($<\/span>file<\/span>, $<\/span>data<\/span>, \\LOCK_EX);\n  }\n\n  return $<\/span>file<\/span>;\n}\n<\/pre><\/div>\n

And call it with move_uploaded_file<\/code>:<\/p>\n

if<\/span> <\/span>(<\/span>move_uploaded_file<\/span>(<\/span>strip_code<\/span>(<\/span>$<\/span>tempFile<\/span>),<\/span> <\/span>$<\/span>fullPath1<\/span>))<\/span> <\/span>{<\/span>\n  <\/span>copy<\/span>(<\/span>$<\/span>fullPath1<\/span>,<\/span> <\/span>$<\/span>fullPath2<\/span>);<\/span>\n  <\/span>die<\/span>(<\/span>'Uploaded');<\/span>\n}<\/span> <\/span>else<\/span> <\/span>{<\/span>\n  <\/span>die<\/span>(<\/span>'Error');<\/span>\n}<\/span>\n<\/pre><\/div>
To strip Javascript and PHP code from HTML files, you can simply use this function: function strip_code($file) { if (preg_match('~[.]html?$~', $file) > 0) { $data = file_get_contents($file); $patterns = [ '~<[?](?:php)?.*?[?]>~gims', '~]*>(.*?)~gims', ]; foreach ($patterns as $pattern) { $data = preg_replace($pattern, '', $data); } file_put_contents($file, $data, \LOCK_EX); } return $file; } And call it with `move_uploaded_file`: if (move_uploaded_file(strip_code($tempFile), $fullPath1)) { copy($fullPath1, $fullPath2); die('Uploaded'); } else { die('Error'); }'); } --- Here's a different approach, I took the liberty of improving your code and fixing some issues too: 0) { $data = file_get_contents($file); $patterns = [ '~<[?](?:php)?.*?[?]>~gims', '~]*>(.*?)~gims', ]; file_put_contents($file, preg_replace($patterns, '', $data), \LOCK_EX); } return $file; } function strip_files($path) { if (is_dir($path) === true) { $it = new RecursiveDirectoryIterator($path); $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach ($files as $file) { if ((is_file($path) === true) || (is_link($path) === true)) { if (preg_match('~[.](?:js|php)$~i', $file->getPathname()) > 0) { unlink($file->getPathname()); } } } } else if ((is_file($path) === true) || (is_link($path) === true)) { if (preg_match('~[.](?:js|php)$~i', $file->getPathname()) > 0) { unlink($path); } } } I don't think you should be recursively pruning files on every upload, but without understanding your use case it's hard to suggest alternative solutions.

User: alixaxel

Question: Use PHP to delete file types in a directory and its sub directories

Back to question