Three problems with debugging an PHP program

  • 2020-05-10 17:54:55
  • OfStack

The 1, filesize() function returns the wrong value.
When a page is downloaded locally using curl, the contents of the downloaded temporary file tmpHtml.txt need to be read into a buffer. Since I'm reading with fread(), I need to pass in the size to read, so I get the temporary file size first with filesize('./ tmpHtml.txt '). Weirdly, the temporary file size is not correct, so debug the breakpoint, after the call to filesize(), manually go to the hard disk to find the file, the file size is not the same as filesize().
Search for filesize on php.net, and you will see the following statement in the function description: Note: the result of this function will be cached. See clearstatcache() for more details.
Go back to clearstatcache() and find out why:
PHP will cache the return information of these functions (which provide a table of functions to query) to provide faster performance. In some cases, however, you may want to clear the cached information. For example, if you check the same file multiple times in a script and the file is in danger of being deleted or modified during the execution of the script, you need to clear the file status cache. In this case, the clearstatcache() function can be used to clear the file information cached by PHP.
2. In the UTF-8 encoded PHP script, how to match the Chinese language when pattern matching the GBK encoded Chinese web content?
In yesterday's development, we needed to match the content containing the GBK encoding string 'apple', so we wrote the following code:
 
$pat = '/<img onclick="zoom\(this, \'(attachments.*?)\'\)".*?alt=" apple \.jpg" \/><\/a>/'; 
$pat = iconv( ' UTF-8',  ' GB2312', $pat); 
$ret = preg_match_all($pat, $contents, $matches); 

However, it does not match, so we try to convert the content to UTF-8, as follows:
 
$pat = '/<img onclick="zoom\(this, \'(attachments.*?)\'\)".*?alt=" apple \.jpg" \/><\/a>/'; 
$contenst = iconv( ' GB2312',  ' UTF-8', $contents); 
$ret = preg_match_all($pat, $contents, $matches); 

So now we have a match. But I can't figure it out. I suspect there's a catch.
But unfortunately, today we used the first method again, and we matched again. It looks like the problem is somewhere else.
Ah! The old man is a pig. Question 2 is caused by question 1! filesize() is not getting it right, so it doesn't match! The second method is matched after solving problem 1.

3. The download address of the quotation picture obtained by the review element in the browser is different from that obtained by curl.
May be... The answer is still: I'm a pig.
Because the URI object is: attachment.php? aid = 3 D Mzk3MTB8YTg5ZTYyNTJ8MTMyNjcyNDEwMXw5NWYydC9aOUE0a05EVm9ydlErSFBRamZJNWJQL1NHdWJLK3ZraU9GTDZYdnBUdw % % 3 D & nothumb = yes
What is aid? It's probably something related to session. It's normal to change it by 1. Then it's ok to grab something that looks like a static path.

These three stories contain two tragedies, which is a common mistake for beginners to PHP.

Related articles: