0

Below is my code,

test.php

<?php
$prefixnew = "testnew";
$file1 = fopen("testnew.txt","w");
$content_wr_g = "Test sitest"
fwrite($file1,$content_wr_g);
fclose($file1);
?>

test.bat

@ECHO off


C:\wamp\bin\php\php5.3.13\php.exe -f  "C:\...\test.php"


popd

UPDATE

When i run (test.php) directly from browser it is working properly. if i run from scheduled tasks, it is not working.

I know very well about task scheduler steps to create a task. When i run set of update queries or delete queries means, it working nicely.

When i have code like fopen(), fwrite() etc it is not working

KMS
  • 566
  • 4
  • 16
  • Can you show us what your scheduled task looks like? Is it a Cron job? – Ibrahim Hafiji Apr 18 '18 at 10:50
  • Created a task in windows "Task schduler" program – KMS Apr 18 '18 at 10:57
  • Possible duplicate of [https://stackoverflow.com/questions/4701861/how-do-i-run-a-php-script-using-windows-schedule-task?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa] – Ibrahim Hafiji Apr 18 '18 at 11:00
  • @IbrahimHafiji no sir. Please see my updated question – KMS Apr 18 '18 at 11:05
  • Try setting a path to open the file. E.g `$file1 = fopen("C:\Users\username\Desktop\testnew.txt","w");` – Ibrahim Hafiji Apr 18 '18 at 11:11
  • The default directory when called from the browser is different than the default when called by the scheduler. As @IbrahimHafiji said, supply a complete path to the file you want to open (and you don't need the drive letter if it's on the same drive as your program). – Dave Apr 18 '18 at 12:43

1 Answers1

0

Instead of using relative path, use absolute path to the file:

$file1 = fopen(__DIR__.'/testnew.txt', 'w');

I assume you are using >= PHP 5.3, if not try:

$file1 = fopen(dirname(__DIR__).'/testnew.txt', 'w');
Mike Doe
  • 16,349
  • 11
  • 65
  • 88