Basically, I can write html tags/text etc etc to the file. All that is fine. However, I need to be able to write some actual php into the file and I'm completely oblivious of how I should go about doing this.
Anyway here is what I have got to help better demonstrate my situation:
$path = substr(md5(time() . md5(rand())), 0, 8);
$dir = substr(md5($_SERVER['REMOTE_ADDR']), 0, 4);
$fpath = "b/" . $dir "/" . $path;
$file = 'index.php';
$handle = fopen($fpath . '/' . $file, 'w') or die ('cannot create the file');
include template.php;
fwrite($handle, $pt1 . $pt2 . $pt3);
Inside the template.php I have variables holding a lot of html in $pt1,$pt2,$pt3
But, inbetween each 'pt' I need to have some actuall php.
So for example sake say I need $this inbetween each 'pt' and the $this being:
<?php
if(isset($_SESSION['user'])){
echo "hello";
}else{
echo "bye";
}
?>
How would I do it?
*The actual creating of the file and paths is fine though but, I know that it may look very unusual however, I like it that way. It's simply adding the php scripts to the php file.
also I have tried sticking the scripts inside variables but, it seems to just print them out as plain text in the file rather than executing them.
Does inbetween each pt means $p1 . $var . $pt2?
Try something like this:
<?PHP
$var = '<?php
if(isset($_SESSION["user"])){
echo "hello";
}else{
echo "bye";
}
?>';
echo $pt1 . $var . $pt2;
?>
Edit:
Also, you can wrap long content into a variable using
<?PHP
$var = <<< 'PHP'
<?php
if(isset($_SESSION["user"])){
echo "hello";
}else{
echo "bye";
}
?>
PHP;
$pt1 = 'before';
$pt2 = 'after';
echo "$pt1 $var $pt2";
?>
Related
I am trying to write some code to look through all of the files in my server and return files that contain a certain string. The problem is that I only know the comment in the files I'm looking for is the key and I feel like this may be messing this up.
I have a function that recursively searches for files in all directories which works fine, but the reading of the file and searching of the string is not working properly.
<?php
$mal = "//###=CACHE START=###";
function getDirContents($dir) {
$files = scandir($dir);
foreach($files as $file) {
if($file == "." || $file == "..") continue;
if(!is_file($dir . $file)){
echo "Folder: " . $dir . $file . "<br />";
getDirContents($dir.$file."/");
} else {
echo "File: " . $dir . $file . "<br />";
$content = file_get_contents($dir . $file);
if (strpos($content, $mal) !== false) {
echo "FOUND" . $dir.$file . "<br>";
}
}
}
}
$dir = "./";
getDirContents($dir);
?>
For some reason, this is returning .png and .jpg files as "FOUND" and I'm not sure why. I have many files that have the $mal string in them, but it's a comment and I'm not sure if that matters. Either way, it is not working properly and not finding the files that I'm looking for.
This fails because the thing you're searching for is not actually in scope - in the function scope $mal is actually NULL and thus always found. This is outlined in the documentation at http://php.net/manual/en/language.variables.scope.php
<?php
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test();
This script will not produce any output because the echo statement
refers to a local version of the $a variable, and it has not been
assigned a value within this scope. You may notice that this is a
little bit different from the C language in that global variables in C
are automatically available to functions unless specifically
overridden by a local definition. This can cause some problems in that
people may inadvertently change a global variable. In PHP global
variables must be declared global inside a function if they are going
to be used in that function.
A quick and dirty way to fix this is to declare $mal a global. Saner is to pass it in as a parameter to your function, along with the dir.
I am trying to configure the Eclipse PHP formatter to keep the opening and closing php tags on 1 line if there is only 1 line of code between them (while keeping the default new line formatting if there are more lines of code).
Example:
<td class="main"><?php
echo drm_draw_input_field('fax') . ' ' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>' : '');
?></td>
Should be formatted to:
<td class="main"><?php echo drm_draw_input_field('fax') . ' ' . (drm_not_null(ENTRY_FAX_NUMBER_TEXT) ? '<span class="inputRequirement">' . ENTRY_FAX_NUMBER_TEXT . '</span>': ''); ?></td>
Is there any way to achieve this with Eclipse? Or another suggestion/formatter?
EDIT:
It seems Eclipse does not have such a formatting option, as explained in the comments below. Any existing alternatives that can do this?
As i already mentioned under question comment "As i know you can't do such thing in eclipse as eclipse has only options to format code part i mean the text part inside php tags <?php ...code text... ?>"
But you can achieve it with this php script
Very important before start: Backup your php project which you are going to mention in dirToArray() function
// Recursive function to read directory and sub directories
// it build based on php's scandir - http://php.net/manual/en/function.scandir.php
function dirToArray($dir) {
$result = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value){
if (!in_array($value,array(".",".."))){
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){
$result = array_merge(
$result,
dirToArray($dir . DIRECTORY_SEPARATOR . $value)
);
}else{
$result[] = $dir . DIRECTORY_SEPARATOR . $value;
}
}
}
return $result;
}
// Scanning project files
$files = dirToArray("/home/project"); // or C:/project... for windows
// Reading and converting to single line php blocks which contain 3 or less lines
foreach ($files as $file){
// Reading file content
$content = file_get_contents($file);
// RegExp will return 2 arrays
// first will contain all php code with php tags
// second one will contain only php code
// UPDATED based on Michael's provided regexp in this answer comments
preg_match_all( '/<\?php\s*\r?\n?(.*?)\r?\n?\s*\?>/i', $content, $blocks );
$codeWithTags = $blocks[0];
$code = $blocks[1];
// Loop over matches and formatting code
foreach ($codeWithTags as $k => $block){
$content = str_replace($block, '<?php '.trim($code[$k]).' ?>', $content );
}
// Overwriting file content with formatted one
file_put_contents($file, $content);
}
NOTE: This is just simple example and of course this script can be improved
// Result will be that this
text text text<?php
echo "11111";
?>
text text text<?php
echo "22222"; ?>
text text text<?php echo "33333";
?>
<?php
echo "44444";
echo "44444";
?>
// will be formated to this
text text text<?php echo "11111"; ?>
text text text<?php echo "22222"; ?>
text text text<?php echo "33333"; ?>
<?php
echo "44444";
echo "44444";
?>
In sublimetext, the manual command is ctrl + J.
Automagically, might i suggest you take a look at this:
https://github.com/heroheman/Singleline
Hello I'm having problems getting my file_get_contents(); to return a string, if I use this function for a file on the actual web server it works fine but if that file which worked on the web server is now moved to my personal computer c:/ drive then file_get_contents(); does not work anymore.... I've tried adding a ini_set(); include path with no avail.
Bit flustered with the lack of absolute accurate documentation on php side unless I've missed something.
I've read the documentation here --> http://www.php.net//manual/en/function.file-get-contents.php and here --> http://www.php.net//manual/en/function.set-include-path.php too.
Here is my code :
<?php
ini_set("auto_detect_line_endings", true);
$db = mysqli_connect("localhost", "root","pass", "DB");
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_errno();
}
$SQL_query = "SELECT text FROM table";
$query = mysqli_query($db, $SQL_query);
echo getcwd();
$file_path = "C:\folder\file.txt";
echo file_get_contents($file_path);
while($result = mysqli_fetch_array($query))
{
echo $result["username"] . " ";
if(strstr($file, $result["username"]))
{
echo "Hello this is equal!";
}else {
echo"this is not equal!"
}
}
Since the backslash is the escape character, you are escaping the f character in that string. The result is C:folderfile.txt.
You have to double the backslash each time you want to use it (with another words, you have to escape the backslash with backslash):
$file_path = "C:\\folder\\file.txt";
chandlermania's suggestion is even better to use the DIRECTORY_SEPARATOR constant
$file_path = "C:" . DIRECTORY_SEPARATOR . "folder" . DIRECTORY_SEPARATOR . "file.txt";
im trying to do this :
$filename = "/destination/destination2/file_*";
" * " = anything
destination2 files :
file_somethingrandom
and since it $filename contains * so it should be selecting file_somethingrandom
how to do it?
Use the glob() function like this:
$filename = "/destination/destination2/file_*";
foreach (glob($filename) as $filefound)
{
echo "$filefound size " . filesize($filefound) . "\n";
}
Try this
foreach (glob("/destination/destination2/file_*") as $filename) {
echo $filename;
}
Cheers!
You can use different functions other than echo, but it does what it does, randomly picks a filename in a group of files inside "/destination/destination2/" whose name contains "file_".
<?php
$filea = glob('/destination/destination2/file_*');
echo $filea[rand(0,count($filea)-1];
?>
I am trying to create a directory using PHP this works:
<?php
$uid = "user_615";
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
but this does not work
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
Yes the session variable is populated with the exact same thing as above 'user_615' so why would the second one be failing?
EDIT:
So I took the suggestion of #stefgosselin and re-designed the code to be
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd() . "/userpics" . $uid;
if(mkdir($thisdir , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
echo "Your thisdir Variable is:'" . $thisdir . "'" ;
}
?>
And the output is
Failed to create directory...Your thisdir Variable is:'/unified/b/bis/www.mysite.com/jou/userpics/user_615
Any other ideas on what would cause the a Session variable not to be able to used in creating a directory?
As a small tip, I would simply put all of $thisdir in a variable and check if the output of that adds up to the result you are expecting.
IE: Having $thisdir ."/userpics/" . $uid defined in a variable would give you the possibility to easily output and validate the argument value you are passing to mkdir.
Edit: Adjusted minor phrasing for better english translation. Sorry above wasn't clear, Wesley understood the simple point I was clumsily trying to make.