我想将所有css文件的缩小版本从一个文件夹复制到另一个文件夹。 如何优化此代码?
$path = "dev/css/";
$path_new = "productive/css/";
if (count(glob($path_new."*.css"))) { // first delete all files in $path_new
foreach (glob($path_new."*.css") as $f => $name) {
$file = pathinfo($name, PATHINFO_FILENAME).".css";
unlink($file);
}
}
if (count(glob($path."*.css"))) { // create minified files
foreach (glob($path."*.css") as $f => $name) {
$file = pathinfo($name, PATHINFO_FILENAME).".css";
$css = file_get_contents($path."/".$file);
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
$css = str_replace(': ', ':', $css);
$css = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css);
file_put_contents($path_new."/".$file, $css)
}
}
我可以使用相同的缩小代码来缩小JS文件吗?
答案 0 :(得分:-2)
要从服务器中删除文件,请使用unlink($fileName)
要将新的css写入文件,请使用file_put_contents($fileName, $css)