get and save path to php.ini file - path

Is it possible to get the path to the php.ini file with a php script and save this path to a variable? I know I can call phpinfo() to find out the path, but it prints a lot of info and I only need the path to php.ini file and no output at all. Thank you in advance.

Sure, there are two functions related to what you would like to do. The first one is exactly what you're looking for, the second one shows the bigger picture that there can be more than one ini file:
php_ini_loaded_fileDocs - Retrieve a path to the loaded php.ini file.
php_ini_scanned_filesDocs - Return a list of .ini files parsed from the additional ini dir.
Next to that, mind the gap with .user.ini files, they don't show up in php_ini_scanned_files nor phpinfo.

You could exec("php -i | grep php.ini"), and grab that output.
Or you could use outputbuffering (ob_start()), run phpinfo(), get the contents of the outputbuffer (ob_get_contents()) and search trough that (preg_match) to find the php.ini file...

This works on the command line, might also work for the CGI:
ob_start(); phpinfo();
if ( preg_match( '#>(.+)/php.ini#', ob_get_clean(), $matches ) ) {
echo 'php.ini location: ' . trim( $matches[1] ) . '/php.ini';
}

Related

Dynamically use current date in OUTPUT EXPORT filename

Stripped down example code using a static file name:
OUTPUT EXPORT /CONTENTS EXPORT=ALL /PDF DOCUMENTFILE='example.pdf'
My question is how to generate a datestamped file. I have tried using $DATE, '$DATE' and running it through a macro but can't seem to find the syntax.
Hey this is a really nice Idea for saving backups in a running syntax production - I will use this myself from now on :) .
So the following syntax works for me:
compute tdy= $time.
formats tdy (date11).
temporary.
select if $casenum=1.
write out="somepath\datemacro.sps" /"define !dated__filename () !quote(!concat('YOUR FILE NAME',' ','", tdy, "','.pdf')) !enddefine.".
exe.
Insert file ="somepath\datemacro.sps".
delete vars tdy.
The file name you used in the code above is now stored in a macro with the date added, and you can use it here:
OUTPUT EXPORT /CONTENTS EXPORT=ALL /PDF DOCUMENTFILE= !dated__filename .
Note that you can change "YOUR FILE NAME" into anything you like, including adding a path. And you can also change ".pdf" so save other kinds of files.
EDIT: Also of course change "somepath" to a valid path on your machine.

Is there a script that can extract particular link from txt and write it in another txt file?

I'm looking for a script (or if there isn't, I guess I'll have to write my own).
I wanted to ask if anyone here knows a script that can take a txt file with n links (lets say 200). I need to extract only links that have particular characters in them, let's say I only need links that contain "/r/learnprogramming". I need the script to get those links and write them to another txt files.
Edit: Here is what helped me: grep -i "/r/learnprogramming" 1.txt >2.txt
you can use ajax to read .txt file using jquery
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<script>
jQuery(function($) {
console.log("start")
$.get("https://ayulayol.imfast.io/ajaxads/ajaxads.txt", function(wholeTextFile) {
var lines = wholeTextFile.split(/\n/),
randomIndex = Math.floor(Math.random() * lines.length),
randomLine = lines[randomIndex];
console.log(randomIndex, randomLine)
$("#ajax").html(randomLine.replace(/#/g,"<br>"))
})
})
</script>
<div id=ajax></div>
If you are using linux or macOS you could use cat and grep to output the links.
cat in.txt | grep /r/programming > out.txt
Solution provided by OP:
grep -i "/r/learnprogramming" 1.txt >2.txt
Since you did not provide the exact format of the document I assume those links are separated by newline characters. In this case, the code is pretty straightforward using Python/awk since you can iterate over file.readlines() and print only those that match your pattern (either by using a lines.contains(pattern) or using a regex if the pattern is more complex). To store the links in a new file simply redirect the stdout to a new file like this:
python script.py > links.txt
The solution above works even if links are separated by an arbitrary symbol s, first read the file into a single string and split it over s. I hope this helps.

Lua io.write() not working

I am using a luvit Lua environment to run my lua code through my control panel. I am looking to write to a .txt file, but with the simple code that i am running, its not working.
The reason I wish to write to a .txt file is to log notices from my Discord Bot I am working on in the Discordia library.
I have a folder called MezzaBOT. In this file i have a write.lua file and also a log.txt file. I have this simple code in my write.lua file:
io.output('log.txt')
io.write('hello\n')
io.close()
I then run in my command promt with Luvit environment:
>luvit Desktop\mezzabot\write.lua
I don't get any errors but the log.txt file continues to stay empty. Am I missing a line in my code, or do i need to access log.txt differently?
edit: my new code is the following
file = io.open('log.txt')
file:write('hello', '\n')
file:close()
and it is not making a new line for each time with \n
edit B:
Ok, i found my problem, its creating a log.txt in my C:\Users\PC.
One other problem is when writing, its not making a new line with the \n. Can someone please help me?
Lua, by default, opens files in read mode. You need to explicitly open a file in write mode if you want to write to it (see manual)
file = io.open('log.txt', 'w')
file:write('hello', '\n')
file:close()
Should work :)

Reading a file line by line using bash, extracting some data. How?

I want to read a file a extract information from it based on certain tag. For example :
SCRIPT_NAME:mySimpleShell.sh
This is a simple shell. I would like to have this as
Description. I also want to create a txt file our of this.
SCRIPT_NAME:myComplexShell.sh
This is a complex shell. I would like to have this as
Description. I also want to create a txt file our of this.
So when I pass in this file to my shell script, my shell will read it line by line and
when it gets to SCRIPT_NAME, It extract it and save it in $FILE_NAME, then starts writing
the description to a file on disk with $FILE_NAME.txt name. And It does it until It reaches the end of file. If there is 3 SCRIPT_NAME tag, then it creates 3 description file.
Thanks for helping me in advance :)
Read the lines using a while loop. Use a regex to check if a line has SCRIPT_NAME and if so, extract the filename. This is shown below:
#! /bin/bash
while IFS= read -r line
do
if [[ $line =~ SCRIPT_NAME:(.*$) ]]
then
FILENAME="${BASH_REMATCH[1]}"
echo "Writing to $FILENAME.txt"
else
echo "$line" >> "$FILENAME.txt"
fi
done < inputFile
#!/bin/sh
awk '/^SCRIPT_NAME:/ { split( $0, a, ":" ); name=a[2]; next }
name { print > name ".txt" }' ${1?No input file specified}

In vim I want to run astyle on the file i'm on when I save

When I save in vim I want to be able to run 'astyle sourceCodeThatImCurrentlyIn.cpp'. So a combination of :w and :!astyle source.c ? Is this possible?
Thank you,
Ejay
Take a look at the Vim auto-commands:
:autocmd BufWritePost *.c execute '!astyle' shellescape(expand('%'), 1)
The % is replaced by the path of the current file. BufWritePost is called after the file is saved. There is also BufWritePre for doing stuff before the file is saved.
If you want to use this permanently then put it into your .vimrc configuration file.

Resources