I have a PHP file on my server which should get executed to import a CSV File. How can I do that via BATCH, just like a Cron Job does.
Thanks
You can directly add php file to crontab, for this file must have 755 permission.
To add
php -f absolute file path
or you can create .sh file and call php file from it, for this also .sh file must have 755 permission.
#!/bin/bash
php -f absolute file path
Related
I want to regularly update (rewrite, not append) a txt file from php by using file_put_contents. another php API reads this file and prints the content for the user.
is it possible that when the user wants to read the file via PHP API, it returns empty? because when the first PHP file tries to update the file, it erases the data and then writes new content. if it is possible, how to avoid it?
It can prevent and sure the source file won't be empty try following solution :
you can keep your processing text file in tmp folder e.g. tmp_txt which you can create parallel to same location where as your current text file, so first your text file goes to in this tmp folder
Create a shell script file and keep that under the tmp folder or any other folder
add the shell script which will observer the file size, and put that in to cron job scheduler
find /your project root path/tmp_txt/ -type f -size +1 -name "mytext.txt" -exec mv {} /your project root paht/folder where you want it/
"find" is command for search the file and next your tmp folder path"
"-type f" this will consider only the file
"-size +1" +1 mean above 1 KB
"-name "mytext.txt"" you can define your file name, if dynamic names then -name "*.txt"
"-exec mv {}" this will move the file on path that next to it, if match the file size with above condition which is 1KB you can change that as per your need
e.g. cronjob entry which will run the every minutes
bash /yor project root path/tmp_txt/shellscriptfilename>> /dev/null 2>&1
I have an index.php file that runs a python script as follows:
shell_exec("/usr/custom/test.py")
The test.py file tries to write to a file (which by default does not exist in the directory) as follows:
#!/usr/bin/env python
import io
with io.FileIO("foobar.txt, "w") as file:
file.write("Hello!")
Now, if I run this file directly as root in Ubuntu, it works fine. The file is created and has "Hello!" inside.
If I run it through the php script (by visiting my index.php page), I get the following error in /var/log/apache2/error.log
Traceback (most recent call last):
File "/usr/custom/test.py", line 5, in <module>
with io.FileIO("foobar.txt", "w") as file:
IOError: [Errno 13] Permission denied: 'foobar.txt'
now...I tried another python file that just prints something and I was able to get that to run fine. So here's where I'm getting confused.
The permissions of the folder and the test.py file is the same and as follows:
-rwxrwxrwx 1 www-data www-data 102
I can't figure out why this won't let me write to the file. I even tried to create the file first (touch) and then give it permissions...but it didn't work.
Does anyone know how I can get this to work?
When you call that script from php it tries to create file in the same folder as python file itself (in /usr/custom/), not php file. To solve that problem, you will need to write absolute path for the file:
with open('/var/www/html/foobar.txt', 'w') as file:
file.write("Hello!\n")
If you need that file to be created in the same place as your php file, then you will need to pass path as an argument in shell_exec call. Read this documentation for argument parsing. You can use getcwd in php to get path for current directory.
I am trying to write some batch script to download and save an XLS file from a URL. I am able to down load the file by using
#ECHO OFF
start /d "C:\Program Files\Internet Explorer" IEXPLORE.EXE url link
exit
I would now like to save these files to a folder or directory.
Any help anyone could provide here would be greatly appreciated.
There are at least two ways to do it.
Like Buddy suggested, use a command-line downloader, for example wget.
Under Linux, you can run PHP directly (even without a webserver). See PHP: Command Line PHP on Microsoft Windows in PHP manual.
Don't run a browser, or - even worse - IE, just to run a PHP script.
I am trying to save a file to the current directory in Linux and then display it on a webpage. Currently I run a C++ executable from a php script with the following code
exec("/var/www/html/radsim/plotFluence $rMin $rMax $zMin $zMax $lum $graphStyle $basepath[$path]", $return);
When I run the executable from the console in Linux the file is created fine, the problem arises when I try from within the php; the file is simply not in the directory . The user inputs values and the executable is run but no file is made. The C++ looks like this
canvas->Print(("/var/www/html/radsim/"+histoName+_FileFormat).c_str());
The permisions are set to 777. In addition, on another PHP script, I use fopen("data.txt", 'w') or die() to create a text file, but it always dies.
Seems like a sandbox. There must be a php config option - best to start here: http://php.net/manual/en/configuration.changes.php
I have an interesting situation where I have a perl watcher script (using Linux::Inotify2) watch for files to be dropped in a certain directory, then hand them off to a PHP script for processing. The watched directory and the files in it are not owned by the user the watcher script is running under, but the entire directory tree the files are being dumped in are rwxr-xr-x and the file is world readable.
Here's my delemma. The PHP script cannot open a file handle on the file passed to it when called from the perl script using system(), exec() or ``. However, the PHP script can open a file handle on the same file when the script is run manually from the command-line using the same effective user.
Anyone have any ideas why this would be the case?
Your fopen() calls probably rely on relative paths that break when the working directory change.