search
HomeBackend DevelopmentPHP Tutorial求怎样解决fsockopen函数,错误

<?//获取文件大小function remote_filesize($url_file){    if (!remote_file_exists($url_file)) return false;    $headInf = get_headers($url_file,1);    return $headInf['Content-Length'];}//判断文件是否存在function remote_file_exists($url_file){     $url_file = trim($url_file);    if (empty($url_file)) return false;    $url_arr = parse_url($url_file);    if (!is_array($url_arr) || empty($url_arr)) return false;    $host = $url_arr['host'];    $path = $url_arr['path'] ."?".@$url_arr['query'];    $port = isset($url_arr['port']) ?$url_arr['port'] : "80";    $fp = fsockopen($host, $port, $err_no, $err_str,30);    if (!$fp) return false;    $request_str = "GET ".$path." HTTP/1.1\r\n";    $request_str .= "Host:".$host."\r\n";    $request_str .= "Connection:Close\r\n\r\n";    fwrite($fp,$request_str);    //fread replace fgets    $first_header = fread($fp, 128);    fclose($fp);    if (trim($first_header) == "") return false;    //check $url_file "Content-Location"    if (!preg_match("/200/", $first_header) || preg_match("/Location:/", $first_header)) return false;    return true;}echo remote_filesize("http://wlm212.bjphp1.qq1.cc/down/ee.exe");?>


这个是我在网上找的,取远程文件大小的例子。
如果域名正常,文件存在就返回文件大小,
如果域名正常,文件不存在,就返回false。

现在问题出现了,如果域名不正常,就像上面这个地址,域名解析不了,也就是无法访问。这个时候会输出错误信息。

php_network_getaddresses: getaddrinfo failed: 不知道这样的主机。 in D:\PHPnow-1.5.6\htdocs\webadmin\inc\filesize.php on line 15

怎样能把这个错误信息去掉呢,在不修改web服务器配置的情况下,只在代码里面解决。
因为我要用ajax,取文件大小信息。所以不能报错,一报错,javascript获取的返回值,就是错误信息了,这个就导致前台无法运行了。

有会的吗,指点一下小弟。


回复讨论(解决方案)

    try
 {
$fp = fsockopen($host, $port, $err_no, $err_str,30);
  }catch(Exception $e)
  {
  }

加了个异常处理,还是不管用。

$fp =  @fsockopen($host, $port, $err_no, $err_str,30);
屏蔽掉错误信息(这个函数在异常处理之前出生)

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 Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

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

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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