美文网首页
【PHP-性能调优】合并JavaScript和CSS文件。可以尽

【PHP-性能调优】合并JavaScript和CSS文件。可以尽

作者: SS_c2e5 | 来源:发表于2020-02-21 11:19 被阅读0次
    function combine_my_files($array_files, $destination_dir, $dest_file_name){
     if(!is_file($destination_dir . $dest_file_name)){ //continue only if file doesn't exist
     $content = "";
     foreach ($array_files as $file){ //loop through array list
     $content .= file_get_contents($file); //read each file
     }
     //You can use some sort of minifier here
     //minify_my_js($content);
     $new_file = fopen($destination_dir . $dest_file_name, "w" ); //open file for writing
     fwrite($new_file , $content); //write to destination
     fclose($new_file);
     return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combined file
     }else{
     //use stored file
     return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combine file
     }
    }
    
    //用法是这样的:
    $files = array(
     'http://example/files/sample_js_file_1.js',
     'http://example/files/sample_js_file_2.js',
     'http://example/files/beautyquote_functions.js',
     'http://example/files/crop.js',
     'http://example/files/jquery.autosize.min.js',
     );
    echo combine_my_files($files, 'minified_files/', md5("my_mini_file").".js");
    

    相关文章

      网友评论

          本文标题:【PHP-性能调优】合并JavaScript和CSS文件。可以尽

          本文链接:https://www.haomeiwen.com/subject/mmtcqhtx.html