How to handle fputs () errors-php Tutorial

Source: Internet
Author: User
What if an exception occurs in fputs? Page prompt: AssemblycodeWarning: fputs () expectsparameter1toberesource, booleangivenloud. phponline100Warning: feof () expectsparameter1toberesource, boolea fputs () what should I do?
Page prompt:

Assembly code
  Warning: fputs() expects parameter 1 to be resource, boolean given in x.php on line 100Warning: feof() expects parameter 1 to be resource, boolean given in x.php on line 101Warning: fgets() expects parameter 1 to be resource, boolean given in x.php on line 102


When prompted, these three rows are repeatedly output. There may be dozens or even hundreds of rows. the page is stuck and I will directly turn off the browser.

This is not the case every time. most of them are normal, but this error occurs occasionally when you encounter a connection.

X. php file:
PHP code
  $ Data = ""; $ text = "xawasdf"; $ server = "abc.com"; $ fp = fsockopen ($ server, 43, $ errNo, $ errStr, 10 ); fputs ($ fp, $ text. "\ r \ n"); // 100 rows while (! Feof ($ fp) {// 101 rows $ data. = fgets ($ fp, 128); // 102 rows} fclose ($ fp );



Are there any possible changes to these lines of code?

------ Solution --------------------
$ Fp = fsockopen ($ server, 43, $ errNo, $ errStr, 10 );
Failed
Error code should be added
------ Solution --------------------
Fsockopen does not open abc.com at all.
------ Solution --------------------
Fsockopen () returns a file pointer which may be used together with the other file functions (such as fgets (), fgetss (), fwrite (), fclose (), and feof ()). if the call fails, it will return FALSE


Write =


$ Data = "";
$ Text = "xawasdf ";
$ Server = "abc.com ";
$ Fp = fsockopen ($ server, 43, $ errNo, $ errStr, 10 );
If ($ fp)
{
Fputs ($ fp, $ text. "\ r \ n"); // 100 rows
While (! Feof ($ fp) {// row 101
$ Data. = fgets ($ fp, 128); // 102 rows
}
Fclose ($ fp );
}
------ Solution --------------------
The fputs parameter is passed as a null handle because fopen fails to cause the returned content (fputs parameter) to be abnormal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.