I have a PHP script that looks like:
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
fwrite($myfile, $text);
fclose($myfile);
When I run the above script in Powershell using
php myscript.php
A text file is created and written into.
If I try to run the same file with
Start-Job ScriptBlock {php myscript.php}
I'll get a response like
But my text file is never written into. It seems like Start-Job never starts my PHP script.
How do I get Start-Job to start running PHP scripts?
Ok I'll write an answer about all of this:
From test (with your script as origin) I added some echo to get something in Receive-Job in powershell.
It was obvious the script was running as intended, but not in the expected directory.
So I added a getcwd() within an echo and this show me the working dir for the job (or scriptblock) is my documents directory) and I found the file there wihtin my home\documents.
Here the final script I eneded with:
echo "I'm running in ".getcwd()." !\n";
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
echo "Ok, file was opened: \n".print_r(fstat($myfile),1)."\n";
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
$r=fwrite($myfile, $text);
echo "Write in file returned $r \n";
fclose($myfile);
echo "File closed\n";
And that is the output is with Powershell:
Start-Job -ScriptBlock { D:\wamp\bin\php\php5.4.3\php.exe D:\wamp\bin\php\php5.4.3\test.php }
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
51 Job51 Running True localhost D:\wamp\bin\php\php5....
And retrieving the output:
Receive-Job 51
I'm running in C:\Users\my_name\Documents !
Ok, file was opened:
Write in file returned 24
File closed
Hope it will help.
Related
When a user clicks submit I am trying to create a file.
Centos 7
php 7.2
I have tried to modify the code a couple different ways also tried file_put_contents does not seem to work.
Code:
index.php
<form action="sendmail.php" method="post">
<input type="text" placeholder="fname" name="fname">
<button type="submit">submit</button>
</form>
sendmail.php:
<?php
$fname = $_POST["fname"];
echo "bef: " . $fname;
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
echo "aft: " . $fname;
$txt = $fname;
fwrite($myfile, $txt);
fclose($myfile);
?>
Directory to create file
[root#webserver webdir]# ll -d /signupemails/
drwxrwxrwx. 2 apache apache 51 Aug 25 20:41 /signupemails/
If i change sendmail.php and hardcode the $fname and run php sendmail.php it will create the file fine
Creating it from the browser I get "Unable to open file!"
Your code should work as is, i just tested it and it seemed to work fine.
Is selinux disabled?
Try changing this line:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
PHP doesn't have the same logical "or" semantics as JavaScript. That expression returns a boolean, so $myfile will equal true instead of the file handle.
Write it this way:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w");
if (!$myfile) die("Unable to open file!");
I suggest do with js
For example
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");
Hi I am using fopen php function to maintain log. I can write to the file within my own computer. But how to make PHP able to write file over the local network. Other computer in the network access my computer PHP application through IP but fails with permission when writing to the log file. My code is:
function hotellog($txt) {
date_default_timezone_set("Asia/Kathmandu");
$newfile = '\\localhost\gandaki/wp-content/hotel_log.json';
if (file_exists($newfile)) {
$myfile = fopen($newfile, "a") or die("Unable to open file!");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
} else {
$myfile = fopen($newfile, 'w') or die("Can't create file");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
Other computers over networks get the error as 'permission denied'.
PHP can not (well, should not) be able to write files over the network onto your computer under normal circumstances. You would be better off writing the log file on your local computer and then scripting a file transfer via sftp, or something similar, from your local computer to the remote one.
trying to figure out how i can make a and save a file while including default info from another file. i tried include but it did not work. any suggestion on how to build this file?
<?php
$wrtID = $_POST["fileID"];
SQL statement here to get relevant info
mkdir("CNC/$wrtID", 0770, true);
?>
<?php
$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
#ECHO OFF
#ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
#ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "
include("defaultcnc.php");
if ( additional file pats needed ) {
include("component-1.php");
}
";
fwrite($slfile, $txt);
fclose($slfile);
?>
I don't see a problem in the first block of code.
On the second block, the interpreter will consider the second double quotes to mean the end of the string $txt = " include(". So everything after that would produce a PHP error.
But even if you escape those the mpr file will have the string include("defaultcnc,php"); but not the actual contents of that file. For that you should do file_get_contents("defaultcnc.php").
something like:
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);
if ( additional file pats needed ) {
$txt .= file_get_contents("component-1.php");
}
fwrite($slfile, $txt);
fclose($slfile);
?>
I assume additional file pats needed means something to you. It should be a condition evaluating either true or false.
I am working on API for DHRU CMS.
Unfortunately there are NO API setting to modify API CALLs, I can only provide domain: www.mydomain.com and Press SYNC. Then it will make a call to www.mydomain.com/api/index.php
Content of /api/index.php below:
<?php
if(!extension_loaded('ionCube Loader')){$__oc=strtolower(substr(php_uname(),0,3));
$__ln='ioncube_loader_'.$__oc.'_'.substr(phpversion(),0,3).(($__oc=='win')?'.dll':'.so');
if(function_exists('dl')){#dl($__ln);}if(function_exists('_il_exec')){return _il_exec();
}
$__ln='/ioncube/'.$__ln;
$__oid=$__id=realpath(ini_get('extension_dir'));
$__here=dirname(__FILE__);if(strlen($__id)>1&&$__id[1]==':'){$__id=str_replace('\\','/',substr($__id,2));
$__here=str_replace('\\','/',substr($__here,2));
}$__rd=str_repeat('/..',substr_count($__id,'/')).$__here.'/';
$__i=strlen($__rd);
while($__i--){if($__rd[$__i]=='/'){$__lp=substr($__rd,0,$__i).$__ln;if(file_exists($__oid.$__lp)){$__ln=$__lp;
break;
}}}if(function_exists('dl')){#dl($__ln);
}}else{die('The file '.__FILE__." is corrupted.\n");
}if(function_exists('_il_exec')){return _il_exec();
}echo('Site error: the file <b>'.__FILE__.'</b> requires the ionCube PHP Loader '.basename($__ln).' to be installed by the website operator. If you are the website operator please use the ionCube Loader Wizard to assist with installation.');
exit(199);
?>
HR+cP/ElGN2OERf73MJKKmN0O3NzOb4pGJD4uAtGY1PaUwYj7l+JHDMBep4pqmahy4Kl4sfAGrTZ
aL2G93Ks855rHFW7zCB0T54xRH9H3qrSrF0bW8cu82ephdXnKG7gU+xqeIGYrhCDtjkrnbjxfTW6
SPgO1ywEMkU6IXNYZ/qD4SE3xzccXKU68rw9qYO6SWnfNL48ek9+x1+AhB1etCJY5QRNVub/Y7XI
n42bVdO8Xx4gEGoUd5OI3lCEqfl2ojsJVJ5rHz8glJ798zsRYapbyXrQw6JU2u7f7mh1CjbJ5RBa
jWL/SqHd/M7MYfblBiTdEi45H444RK66kWf3/ubHaIum4m5VMsXpG5CRiVSOWfDL2POK0S9gsFyk
aUQBqJIkBirzm+/jdQaVFun5FJMf5lSB8Lb0KX0KmfLBiwYy9kLsGSNB7XCLOyJdFKNE7GjpPR3f
DIXXUSCiq4NjJ1DBc58FhrMIcRadT6muczYvxmenoA8AtIY/zF5YBsjzzFCivOlpWzHwfGheRTOv
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fdCFJUQ8jnpaWitSwrPTKqspU05+3Sp2
Cpkem6lDm3cHmbbAru/sUo6l626fiv4qaBklZc/voZVo5X+RAVbOtFSwegeKlufkH8UsGRjRDrWI
8WC8fh4XTuMY/3rAJ2h3tCus/9LnQE61exJUO/PkLFyhCRedRZ1DfJNS8CMJrGtFSzJVXqj3d0M/
V9XF2nVA5onD+5HtHfjBqxJrbVqP5nu8YtKlTBZdEfau+wuvBSV0qmNV+19RGucVwAoA2cpl67/j
3pDNnY4hmtNH/eo8Kcd7DXa8p/wWWVlF97V4niqQGCD2qe34SxH6fv0kex2X24FwlCVvnfkXkcn5
1pyHRWX57kLudQZUXYdqxognkPhf7j7APGaPv+nupVezNI2qXyuZGLKkL449yjQnrBYKP7MlrE1s
gS6vn5MLp0sMfpjlYterc0oF/qza1QDhPfnrQH6PgUWbA5dHOq7KUjn4q9W+H2ghedknFeEDp5Lw
W8eLCHw4OOe5rC8JL7VT603UQdHBLxUDvFWoWfclBCBSazLCeuFy/p08gO/V7bUvrrPqI3ALYXLc
PCZD7KQUI4qo29oJ+yrneu2hzGBFzBMWBasH9BcIK7BpZ+vWcdZ50Ev2OzwgtRC9OzxQCI30+p1j
ldE0nq8+FKvVWqkt+dDoQNEfvQJ2bsqopkiMoeqispfuUsqYI5C7/cQbvxNlbPqDPZfikSSATzru
Kl0jT8HmNKxuASj+yDldUhhAhLSKTucpfNbLUAGpsk277dOdT2k8ZK5UXs4cnZMTTBph5iTtdg+y
eKukLtvt6xH2ymHsQdpaERn1ZQehrhjlnl0YGuVHJqq840A+bKqUVcbyJ3PdT8QuhWpYS5H1oBT8
UcB3I15HN3KcKAATZJX6u9nV4iAlxKN3iZxqMfCAVpv9gAi99fXIEcvhkeoEi4ux4nYCDKVvJXnV
8tznbh8FbfIGw7WVAcT49uzkgurX/UVACSYSMdn6wpTRTE==
I cant figure out what this contents means, but but the contents above is answer to API call. Now I am trying to figure out what it is asking for and how to reply.
Here I have made a simple script to read values and dump them to TXT file.
But file is empty.. No result so far with this code:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$text = var_dump($__oc);
fwrite($myfile, $text);
$text = var_dump($__ln);
fwrite($myfile, $text);
$text = var_dump($__oid);
fwrite($myfile, $text);
$text = var_dump($__id);
fwrite($myfile, $text);
$text = var_dump($__here);
fwrite($myfile, $text);
$text = var_dump($__id);
fwrite($myfile, $text);
$text = var_dump($__rd);
fwrite($myfile, $text);
$text = var_dump($__i);
fwrite($myfile, $text);
$text = var_dump($__lp);
fwrite($myfile, $text);
fclose($myfile);
?>
I have also tried $_POST, $_GET and $_REQUEST but no result.
Is there any other way I can dump result from a API call?
Thanks for helping me out.
Example of fetching both $_GET and $_POST and write it to some logfile:
<?php
$rawpost = print_r($_POST, true);
$rawget = print_r($_GET, true);
$logfile = "/full/path/to/log/file/logfilename.log";
$fh = fopen($logfile,'a') or die("can't open the file");
fwrite($fh, "DATE: ".date('l jS \of F Y h:i:s A')."\r\n");
fwrite($fh, "\r\nRAW POST: =\t ".$rawpost."\r\n");
fwrite($fh, "\r\nRAW GET: =\t ".$rawget."\r\n");
fwrite($fh, "-------------------------------------------------------\r\n");
fclose($fh);
?>
With the above if someone posts data with the GET and POST method it will write it to the logfile. Your log file will need the proper write permissions if you are on a Linux system.
btw: The content of that index.php file is encoded with ionCube.
I have a PHP-Code like:
$timestamp = time();
echo time();
echo "<br>";
$datenbank = "timestamp.txt";
$datei = fopen($datenbank, "w") or die("Unable to open file!");
fwrite($datei, $timestamp) or die("Unable to write file!");
but I forget fclose(). Now, I have no more memory on the server. Could this be the reason?
Unlikely. It is not good practice to leave files open like that, but PHP will close the file when your script ends.