fopen(), fwrite() function is not working in php - php

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

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');

Related

How to schedule a task that runs php using window task scheduler?

I've been trying to run a php file using window task scheduler. I've tried using the .bat file, but it won't work.
Here's what I've done:
I've created a task "Download Image"
I've created a .bat file, and the content is:
"C:\xampp\php\php.exe" -f "D:\server\newxml\download.php";
The php file that I want to run is:
ini_set('max_execution_time', 300);
$doc = simplexml_load_file('xml_edit_feeds.xml');
foreach ($doc->xpath("//item") as $item) {
$name = $item->productname;
$realname = preg_replace('/\s/', '',$name);
$url = $item->thumbnail_url;
$img = 'D:/server/newxml/imagethumbnail/'.$realname.'.png';
$filename = 'D:server/newxml/imagethumbnail/'.$realname.'.png';
file_put_contents($img, file_get_contents($url));
}
?>
This is the screen shoot of the windows task scheduler:
Is there something wrong from what I did? Thank you for your help
I didn't try this before, but I think you should use a browser exe program to open your webpage instead of php.exe.
Eureka! I got the answer to this, so, I what I've done is:
Set the Program/Script to: C:\xampp\php\php.exe
Set the arguments to: D:\path\to\php.php (In my case D:\server\newxml\download.php)
Set the start-in to the folder that contains download.php (php file that you want to run). In my case: D:\server\newxml\

is it possible to run assimp commandline tool with the php

I used following command to convert the 3d models with the assimp Assimp, and it is working fine on Windows:
assimp2json seaknight.obj seaknight.json
I need to know how can I run this command from the PHP? I know that there re functions to run the shell execution from PHP, but it didn't work and I don't get any error.
PHP code is used is follows.
system("D:\assimp2json-2.0-win32\Release\assimp2json.exe assimp2json seaknight.obj seaknight.json");
and another one is
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json.exe assimp2json seaknight.obj seaknight.json');
chdir($old_path);
Found it myself
working code is below
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json monster.blend monster.json');
chdir($old_path);
no need to include the .exe, after remove it the command worked

Finding correct of PHP binary - exec()

I'm trying to execute a separate PHP script from within a PHP page. After some research, I found that it is possible using the exec() function.
I also referenced this SO solution to find the path of the php binary. So my full command looks like this:
$file_path = '192.168.1.13:8080/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path; // PHP_BINDIR prints /usr/local/bin
exec($cmd, $op, $er);
echo $er; // prints 127 which turns out to be invalid path/typo
doSomething.php
echo "Hi there!";
I know $file_path is a correct path because if I open its value; i.e. 192.168.1.13:8080/doSomething.php, I do get "Hi there!" printed out. This makes me assume that PHP_BINDIR.'/php' is wrong.
Should I be trying to get the path of the php binary in some other way?
The file you are requesting is accessible via a web server, not as a local PHP script. Thus you can get the result of the script simply by
$output = file_get_contents($file_path);
If you however for some reason really have to exec the file, then you must provide a full path to that file in your server directory structure instead of server URL:
$file_path = '/full/path/to/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path;
exec($cmd, $op, $er);

Creating a txt file using php in linux

We have few fields in the HTML page and this is being written into a file using php.
The file has the data, however, running a bash script which takes this file with a .txt as extension is not working.
When the file is opened and re-saved manually the bash script will work properly!
I've tried changing the permissions of the file but the bash script is still not using this file. Any help on this is greatly appreciated.
PHP Script:
$name = "test.txt";
$handle = fopen($name, "w");
fwrite($handle, "my message");
fclose($handle);
Bash Script:
INPUT="$1"
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read message number ; do
echo $message # or whaterver you want to do with the $line variable
#j=$[$(line)]
echo $number
echo "$message" | gnokii --sendsms $number --smsc $SMSC
done < $INPUT
IFS=$OLDIFS
You probably want to use fclose($handle) rather than unlink()
Edit OK then, "Because..."
fclose($handle) closes the file referenced by the handle: $handle
unlink() takes a file path string as a parameter, rather than a resource.
If used as intended, unlink() will actually delete the file.
unlink() as mentioned would delete the file. Just created a plain script like this
<?php
$name = "/var/www/test.txt";
$handle = fopen($name, "w");
fwrite($handle, "test,9944");
fclose($handle);
?>
This script works f9 with storing the data. But running bash script on this with the file input as test.txt does nothing. But again opening and saving with the same file name and it works.
finally got the answer its the end of file that is missing in text file that is created by php. anyway thank you all for contributing.

Cron jobs help

I am trying to make a cron job via my websites cpanel. I have talked to the support services and they gave me this command to run to execute a php file on my website.
/usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php
This doesnt seem to work though, I have also set the minute interval to every minute using */1.
I am using the code below inside of the file update.php:
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
require('minequery.class.php');
include('../config/config.php');
$date = date("D-M-Y");
$file = fopen("UPDATE_LOG($date).txt", 'w');
$query = mysql_query("SELECT * FROM servers") or die(mysql_error());
if($query) {
while($row = mysql_fetch_array($query)) {
$ip = $row['ip'];
$port = $row['port'];
$name = $row['name'];
$string = "[UPDATE LOG]: $date - Updated Server $name \n";
fwrite($file, $string);
print("[UPDATE LOG] Updated Server $name </br>");
}
mail("duncan#mymcstatus.net","UPDATED","Server has updated","From: Mymcstatus");
} else {
print("Cant query");
}
?>
When I go to update.php manually through the web browser that code works, but nothing seems to happen with the Cronjob. What am I missing or doing wrong?
There are a few things that could be going on here. The first is to check the file permissions of update.php and make sure it is executable. The cron may be executing as a different user that doesn't have permission to execute update.php. The 2nd thing you can try is including this as the very first line of update.php with no whitespace before it.
#!/usr/local/bin/php
Usually cron jobs aren't run from the same directory where your PHP lives so it's possible that it is running but the output file is being created elsewhere. Try changing the output file path to be the full path to the file, i.e:
$file = fopen("/home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/UPDATE_LOG($date).txt", 'w');
With the help of the above comments I managed to fix the file paths but it also came down to the command. I had put the wrong user path in here is the command that works.
/usr/local/bin/php -q /home/mymcstat/domains/mymcstatus.net/public_html/redesign/scripts/update.php
Thanks for the help
It's always a good idea to cd to your script's path. This way you don't need any change in the name of the include and require files and any other file names engaging in file operations. Your cronjob command could look like this:
cd /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/; /usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php
You don't need to supply any absolute file paths this way.

Categories