Why can't crontab-e execute fwrite to write files? Collapsed.

Source: Internet
Author: User
Why can't crontab-e execute fwrite to write files? Collapsed.
function Write_FileFun_new($filename,$filedir, $htmldata = '') {if(!is_dir($filedir)) {mkdir($filedir, 0777);}$htmlfile = $filedir.$filename;if($fp = fopen($htmlfile, 'wbt')) {fwrite($fp,$htmldata);fclose($fp);} else {echo "
 
  

Can not write to files, please check directory $htmldir.

";exit;}}$testnum_temp=" ";Write_FileFun_new("test.php","inc/", $testnum_temp);


Running pages in a browser is normally written,
In crontab-e, the scheduled execution is performed once per minute without writing files. crontab-e is normal and can be executed once per minute.
Why is this? it's just a little worse. if all other programs are running normally, this cannot be written. Why?


Reply to discussion (solution)

The content in test. php is always If the value is 1, it is not changed, that is, the test. php file is not automatically added after the value is 1.

Inc/is relative path
First, find out who it is.

Crontab-e is set:
*/1 *****/usr/bin/php/var/www/html/write. php

Inc/is/var/www/html/inc
Test. php is in/var/www/html/inc/test. php

I ran write. php in the browser successfully ......

Write_FileFun_new ("test. php", "/var/www/html/inc/", $ testnum_temp );
It cannot be switched to an absolute path. Is there any permission required? Test. php is set to 777

Check the log. Is there any warning information?

There is no warning in the diary. The following is displayed:
Apr 8 02:59:01 localhost CROND [25675]: (root) CMD (/usr/bin/php/var/www/html/write. php)

Run the commands in crontab directly on the command line to see what the response is.

How can I write a command line? Directly click this field: */1 *****/usr/bin/php/var/www/html/write. php

Write a sh file and handle the permissions. call/usr/bin/php/var/www/html/write. php in this file and use the root identity in crontab ..

Since the web method can be successful, crontab should also be successful
The difference may be caused by the way your directories are created and files are opened.
Generally, apache runs as a module in web mode, while crontab runs as cli.
The two use different master files, and basic functions such as file operations should be placed in the master file, because they also use

It is very likely that fopen in cli mode cannot correctly handle t mode that is only valid in window, leading to failure.
I think you can see the error message when running/usr/bin/php/var/www/html/write. php on the console.

Said in the manual
Note:
Again, for portability, we strongly recommend that you rewrite the code dependent on the 'T' mode to use the correct line terminator and change it to the 'B' mode.

PHP Notice: Undefined variable: testnum in/var/www/html/write. php on line 28

An error occurred while running the command line,
It seems that the testnum variable cannot be found. this variable is stored in 'Inc/test. php' in write. php ');
That is, the content of the file to be written above $ testnum_temp =" ";
Content already exists in test. php.
Why can't I find this variable?

I'm dizzy. now I can. thank you,
It turned out to be the include_once ('Inc/test. php'); problem. I don't know why relative paths won't work (the browser runs normally), and the path structure is correct.
Change to include_once ('/var/www/html/inc/test. php.

Didn't you say that? The relative path must first determine the relative person

Thank you. I did not expect the path containing the file.

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.