search
HomeBackend DevelopmentPHP Tutorial调试一段PHP程序时遇到的三个问题_php技巧

1,filesize()函数返回错误的值。
使用curl将某个页面下载到本地时,需要将下载到的临时文件tmpHtml.txt的内容读取到一个缓冲区中。由于我使用fread()进行读取,需要传入要读取的大小,所以先用filesize(‘./tmpHtml.txt')获取临时文件大小。怪异的是,获取到的临时文件大小不正确,下断点调试,在filesize()调用后,手工去硬盘上寻找文件,文件大小与filesize()得到的结果不一样。
在php.net上搜索filesize,可以看到函数说明中有这么一句:Note: 此函数的结果会被缓存。参见 clearstatcache() 以获得更多细节。
再去查阅clearstatcache(),果然找到了原因:
PHP将缓存这些(提供了函数表供查询)函数的返回信息以提供更快的性能。然而在某些情况下,你可能想清除被缓存的信息。例如如果在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存。这种情况下,可以用 clearstatcache() 函数来清除被 PHP 缓存的该文件信息。
2,在UTF-8编码的PHP脚本中,对GBK编码的中文网页内容做模式匹配时,如何匹配中文。
在昨天的开发中,需要匹配包含GBK编码字符串‘苹果'的内容,所以写出如下代码:
复制代码 代码如下:

$pat = '/调试一段PHP程序时遇到的三个问题_php技巧/';
$pat = iconv(‘UTF-8', ‘GB2312', $pat);
$ret = preg_match_all($pat, $contents, $matches);

可是死活匹配不上,于是尝试先将内容转换成UTF-8编码,如下:
复制代码 代码如下:

$pat = '/调试一段PHP程序时遇到的三个问题_php技巧/';
$contenst = iconv(‘GB2312', ‘UTF-8', $contents);
$ret = preg_match_all($pat, $contents, $matches);

于是就能匹配上了。但是想不通啊,怀疑这里面有诈。
但悲剧的是,今天又用了第一种方法,又匹配中了。看来问题出在别的地方。
啊!老夫是猪,问题2是由问题1引起的!filesize()获取的不对,自然匹配不上了!第二种方法匹配上,是在解决问题1之后啊!

3,浏览器中审查元素得到的报价图片下载地址,为何与curl得到的下载地址不同。
可能……最后答案依然是:我是猪。
因为URI对象为:attachment.php?aid=Mzk3MTB8YTg5ZTYyNTJ8MTMyNjcyNDEwMXw5NWYydC9aOUE0a05EVm9ydlErSFBRamZJNWJQL1NHdWJLK3ZraU9GTDZYdnBUdw%3D%3D¬humb=yes
aid是个什么呢?很可能是个与session有关的东东,变一变也很正常的嘛。后来抓看起来像静态路径的东东就没问题了。

以上三个故事中包含两个悲剧,这就是PHP初学者必犯的低级错误。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools