search
HomeBackend DevelopmentPHP Tutorial为什么crontab -e不能执行fwrite写入文件?蹦溃了。

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 "<center><p>Can not write to files, please check directory $htmldir.</p></center>";		exit;	}}$testnum_temp="<?php \$testnum = ".(intval($testnum)+1)."; ?>";Write_FileFun_new("test.php","inc/", $testnum_temp);


用浏览器运行页面是正常写入的,
在crontab -e中设置定时每分钟执行一次没有写入文件,crontab -e定时执行是正常,可以每分钟执行一次。
为什么会这样,就差这一个地方了,其它程序都正常执行,就差这个不能写入。为什么啊?


回复讨论(解决方案)

test.php里面的内容一直是,值是1没有变过,就是没有自动加1后写入test.php文件。

inc/ 是相对路径
请先弄清楚是相对谁的

crontab -e的设定是:
*/1 * * * * /usr/bin/php /var/www/html/write.php

inc/ 是/var/www/html/inc
test.php是在/var/www/html/inc/test.php

我在浏览器运行write.php是成功写入的啊……

Write_FileFun_new("test.php","/var/www/html/inc/", $testnum_temp);
改用绝对路径也不行啊,这个有没有权限要求的啊?test.php是设为777的

看看日志,有什么警告信息吗?

日记没警告啊,显示的是:
Apr 8 02:59:01 localhost CROND[25675]: (root) CMD (/usr/bin/php /var/www/html/write.php)

你把crontab里面的命令直接在命令行运行一下看看什么反应

请问命令行怎么写?直接打这个吗:*/1 * * * * /usr/bin/php /var/www/html/write.php

写一个sh,处理好权限 在这个文件里调用/usr/bin/php /var/www/html/write.php 最后在crontab 使用root 身份调用..

既然 web 方式下可以成功,那么 crontab 中也应该成功
差异的产生,可能与你的目录创建和文件打开方式有关
一般web方式下是以apache的模块方式运行的,而crontab中是以 cli 方式运行的
两者使用了不同的主控文件,而文件操作这种基础函数理应放在主控文件中,因为他自己也要用

极有可能是 cli 方式下的 fopen 不能正确处理只在window中才有效的 t 模式,而导致失败
我想你在控制台中运行 /usr/bin/php /var/www/html/write.php 应该是可以看到错误信息的

手册中说
Note: 
再一次,为移植性考虑,强烈建议你重写那些依赖于 't' 模式的代码使其使用正确的行结束符并改成 'b' 模式。

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

命令行运行出错,
好象找不到变量testnum,这个变量是在write.php里的include_once('inc/test.php');
也就是上面的要写入的文件内容$testnum_temp="";
初始test.php里面已经有内容
为什么会找不到这个变量?

我晕,现在可以了,谢谢,
原来是include_once('inc/test.php');的问题,不知道为什么用相对路径会不行(浏览器运行是正常的),路径结构又没错。
改成include_once('/var/www/html/inc/test.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
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

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 Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.