PHP:如何动态更改文件中的输出

Redirect output do minified.js file

file_put_contents("minified.js", $packer->pack());

This redirects the output to the minified.js file. If I change anything in the code, the output is not updated in minified.js. I must always delete the content and run it again.

How can I do this dynamically and make the content in minified.js always replaced automatically?

Check the last modification time of the file where the original version is located.

$original_file = $_SERVER["DOCUMENT_ROOT"].'/main.js';

$last_modified = date("Y-m-d H:i:s", filemtime($original_file));

file_put_contents($last_modified, $_SERVER["DOCUMENT_ROOT"].'/date.txt');
$last_modified_read = file_get_contents($_SERVER["DOCUMENT_ROOT"].'/date.txt');

if(strtotime($last_modified_read) < strtotime($last_modified)){
  echo "Minify!";
  file_put_contents($_SERVER["DOCUMENT_ROOT"].'/date.txt', $last_modified);
}

filemtime()

This is happening because of header function.... your code in minified.php is...

code

<?php 

// Načteme config 
require_once("../functions/config.php"); 
// Načteme adresáře 
$Directories = JS_Directories(); 
// Načteme soubory z adresářů 
$Buffer = ""; 
foreach ($Directories as $Directory) { 
foreach(glob($Directory . "*.js") as $File) { 
$Buffer .= file_get_contents($File); 
} 
}; 
// Načteme minifikátor 
require_once("minifier.php"); 
// Spustíme minifikátor 
$packer = new Tholu\Packer\Packer($Buffer, 'Normal', true, false, true); 
// Nastavíme MIME typ
$presentFile = file_get_contents("minified.js"); //Get Old data then merge with new.
file_put_contents("minified.js", $presentFile.$packer->pack()); //Moved above header then merge.
header("Content-type: text/javascript"); 
// Výstup 
echo $packer->pack(); 



?>

just move file_put_contents("minified.js", $packer->pack());above header, and it'll works.