Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about this function, although it seems to be of little use, but it can still be used in some cases. If you are creating your own function library, you can add it to favorites.
Tip: this function is not my original one. The original address is called here.
The following code is provided:
/**
* Function for getting the number of PDF file pages
* The file should be readable to the current user (in linux)
* @ Param [string] $ path [file path]
* @ Return [array] [the first digit of the array indicates success or not, and the second digit indicates prompt information]
*/
Function getPdfPages ($ path ){
If (! File_exists ($ path) return array (false, "file \" {$ path} \ "does not exist! ");
If (! Is_readable ($ path) return array (false, "file \" {$ path} \ "unreadable! ");
// Open the file
$ Fp = @ fopen ($ path, "r ");
If (! $ Fp ){
Return array (false, "open file \" {$ path} \ "failed ");
} Else {
$ Max = 0;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 255 );
If (preg_match ('/\/Count [0-9] +/', $ line, $ matches )){
Preg_match ('/[0-9] +/', $ matches [0], $ matches2 );
If ($ max <$ matches2 [0]) $ max = $ matches2 [0];
}
}
Fclose ($ fp );
// Number of returned pages
Return array (true, $ max );
}
}
/**
* Test code
*/
$ Results = getPdfPages ("demoages ");
If ($ results [0]) {
// Put the processing code after successful reading here
} Else {
// Put the failed processing code here
}
?>
In terms of performance, it takes about 1 second to read a 600-page file on my ubuntu machine.
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB