unlink() function not working with variables? [duplicate] - php

This question already has answers here:
Can I echo a variable with single quotes?
(11 answers)
Closed 9 years ago.
Im having some troubles unlinking files from a directory. I'll try to explain it as good as i can.
Im trying to delete a file from a directory, first i get the file name from my database, where is stored in field "avatar".
these are my involved vars:
$avatar1=mysqli_query($con,"Select avatar from users where user='$_SESSION[Username]'");
$avatar2=mysqli_fetch_array($avatar1);
$avatardirectory = $avatar2['avatar']; //(missunderstanding name, its actually a file).
So far, when i print $avatardirectory when my user is hodor, it shows "hodor.png"
Ok, here comes the nasty part, i try to go with this:
unlink('/var/www/html/test/img-gallery/$avatardirectory'); //This wont work.
Then I just do the same exact thing but with filename:
unlink('/var/www/html/prueba/img-gallery/hodor.png'); //This actually works.
And now I'm totally lost.

Variables arn't expanded inside single quote strings.
You might want to see the difference in
echo '/var/www/html/test/img-gallery/$avatardirectory'
and
echo "/var/www/html/test/img-gallery/$avatardirectory"
Note that unlink() removes files, not directories. use rmdir() to remove (an empty) directory. (though it sounds like your $avatardirectory holds the name of a file, not a directory(?) )

Related

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

Using PHP to write query string values to a .txt file and save [duplicate]

This question already has answers here:
Create or write/append in text file
(7 answers)
Closed 6 years ago.
I am able to take the values from a URL's query string and store them in some variables, however I would also like to print or write these values to a single line in a .txt document, and save that document in a given directory on the server.
Each time the page is loaded with a new query string, a new line will be added to that .txt file with the string values, and the file re-saved.
Can this be done? Struggling to find answers to this in my searches.
Many thanks in advance.
As simple as:
$url_value = "This is some value\n";
file_put_contents("/your/filename/here.txt", $url_value, FILE_APPEND );
See file_put_contents() for more information.

Read ini file in PHP, not parse [duplicate]

This question already has answers here:
PHP: Escape illegal chars in .ini-files
(2 answers)
Closed 1 year ago.
When using parse_ini_file, it will attempt to read and understand the ini file. I'm looking for a solution that will read the file, place it into arrays much like parse_ini_file does, however I don't want it to get to the special characters in my ini file and spit an error that it can't parse them. Could anyone point me in the right direction?
ini file for reference:
[IMPORT]
email=email#thisisanemail.com
location=
Description=Order Form
name=*.xls*
matrixfile=
matrix_field=
matrix_disc_type_column=
matrix_disc_percentage_column=
fixed=XLS
separator=|
RowTerminator=
headerrows=13
footerrows=0
maxexcelcolumns=7
If I parse this, it gets stuck on the seperator "|" but I need that there.
If you use parse_ini_file with the scanner_mode parameter to specify INI_SCANNER_RAW, then option values will not be parsed (see
https://www.php.net/manual/en/function.parse-ini-file.php).

Bug encountered while Executing .exe file from a Php script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is PHP not replacing the variable in string?
I have been trying to execute this line echo exec('hi.exe $file',$results,$status); from Php. where the value assigned to $file is the filename hi.txt (i.e. $file = hi.txt).
But each time i try to run my code with the same line, its showing error as $file file not found where as if i run the same hi.exe hi.txt in a command prompt its working.
And also if i try to run the same line with the filename instead of a variable from php i.e.exec('hi.exe hi.txt',$results,$status), the browser keeps executing for long time without giving the output.
Please someone tell me where i am going wrong!
You are using single quotes, instead of double quotes. Change echo exec('hi.exe $file',$results,$status); to:
echo exec("hi.exe $file",$results,$status);
or use a dot, like this:
echo exec('hi.exe '.$file,$results,$status);
In PHP, using single quotes won't turn $file into hi.txt; it just stays as the literal string, "$file". Use double quotes or dot concatenation to actually expand $file into hi.txt
Single quotes don't expand variables. You probably mean:
echo exec("hi.exe $file",$results,$status);

php: how to know what file has contains a php function that is called [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out where a function is defined?
in a client's website I have a php code:
return userHasActivePurchase($iId);
The website is full of icludes, requires.
I have made a full text search on "userHasActivePurchase" to find out what php file does contain this function without success.
My question is : I have a function name and I would like to know what php files contains that function.
I tried with:
print_r(userHasActivePurchase);
without success.
Please help !
As per this stackoverflow thread:
$reflFunc = new ReflectionFunction('function_name');
echo $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
You can search for text "function userHasActivePurchase" in the whole code base using grep command or by any editor's native search in directory function.
If you find this function defined at more than one place you can recognize it by putting a statement inside the function in all the files
echo __FILE__
get a decent ide (phpstorm, eclipse pdt) which will allow you to follow code - or even worst case allow you to search an entire project for a given term.

Categories