Solution Timeline

All versions (edits) of solutions to Use PHP to delete file types in a directory and its sub directories appear below in the order they were created. Comments that appear under revisions were those created when that particular revision was current.

To see the revision history of a single solution (with diffs), click on the solution number (ie. "#1") in the upper right corner of a solution revision below.

← Bounty Expand all edits

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',
      '~<script[^>]*>(.*?)</script>~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');
}

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',
      '~<script[^>]*>(.*?)</script>~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:

<?php

$storeFolder1 = '../../testserverOne/';
$storeFolder2 = '../../testserverTwo/';

if (empty($_FILES) !== true) {
    $file = $_FILES['file']['tmp_name'];
    $path1 = $storeFolder1 . rtrim($_POST['path'], '/.');
    $path2 = $storeFolder2 . rtrim($_POST['path'], '/.');

    $mask = umask(0);

    if (is_dir(dirname($path1)) !== true) {
        mkdir(dirname($path1), 0777, true);
    }

    if (is_dir(dirname($path2)) !== true) {
        mkdir(dirname($path2), 0777, true);
    }

    umask($mask);

    $success = false;

    if (move_uploaded_file($file, $path1) === true) {
        $success = copy(strip_code($path1), strip_code($path2));
    }

    if (true) { // there are most likely better ways to do what you want
        strip_files($storeFolder1);
        strip_files($storeFolder2);
    }

    die($success ? 'Uploaded' : 'Error');
}

function strip_code($file)
{
    if (preg_match('~[.]html?$~i', $file) > 0) {
        $data = file_get_contents($file);
        $patterns = [
            '~<[?](?:php)?.*?[?]>~gims',
            '~<script[^>]*>(.*?)</script>~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.

try this,

    <?php
    $ds = DIRECTORY_SEPARATOR;
    $storeFolder1 = './../testserverOne/';
    $storeFolder2 = './../testserverTwo/';

    if (!empty($_FILES)) {
        $tempFile = $_FILES['file']['tmp_name'];
        $targetPath1 = dirname( __FILE__ ) . $ds . $storeFolder1 . $ds;
        $targetPath2 = dirname( __FILE__ ) . $ds . $storeFolder2 . $ds;

        $fullPath1 = $storeFolder1.rtrim($_POST['path'], "/.");
        $fullPath2 = $storeFolder2.rtrim($_POST['path'], "/.");

        if (!is_dir($storeFolder1)) {
            $mask = umask(0);
            mkdir($storeFolder1, 0777, true);
            mkdir($storeFolder2, 0777, true);
            umask(umask(0));
        }

        $iterator1 = new RecursiveDirectoryIterator($storeFolder1);
        $directoryIterator1 = new RecursiveIteratorIterator($iterator1);
        foreach ($directoryIterator1 as $file1) {
            $extension1 = $file1->getExtension();
            if (!$file1->isDir() && $extension1 == 'php' || $extension1 == 'js') {
                unlink($file1->getPathname());
            }else if (!$file1->isDir() && $extension1 == 'html') {

               $data = file_get_contents($file1->getFilename());//strip <script> and php tag in file server
               $data=preg_replace('/<script\b[^>]*>.*?<\/script>/ims', '', $data);//remove all content from <script> tag
               file_put_contents($file1->getPathname(), preg_replace('/<\?(php)?.*?\?>/ims', '', $data), \LOCK_EX);//remove all php tag

            }
        }

        $iterator2 = new RecursiveDirectoryIterator($storeFolder2);
        $directoryIterator2 = new RecursiveIteratorIterator($iterator2);
        foreach ($directoryIterator2 as $file2) {
            $extension2 = $file2->getExtension();
            if (!$file2->isDir() && $extension2 == 'php' || $extension2 == 'js') {
                unlink($file2->getPathname());
            }else if (!$file2->isDir() && $extension2 == 'html') {
               $data = file_get_contents($file2->getFilename());//strip <script> and php tag in file server
               $data=preg_replace('/<script\b[^>]*>.*?<\/script>/ims', '', $data);//remove all content from <script> tag
               file_put_contents($file2->getPathname(), preg_replace('/<\?(php)?.*?\?>/ims', '', $data), \LOCK_EX);//remove all php tag

            }
        }
        $data = file_get_contents($tempFile); //strip <script> and php tag from freshly uploaded file
        $data=preg_replace('/<script\b[^>]*>.*?<\/script>/ims', '', $data); //remove all content from <script> tag
        file_put_contents($tempFile, preg_replace('/<\?(php)?.*?\?>/ims', '', $data), \LOCK_EX); //remove all php tag

        if (move_uploaded_file($tempFile, $fullPath1)) {
            copy($fullPath1, $fullPath2);
            die('Uploaded');
        } else {
            die('Error');
        }

    }