PHP4 problems with include() within a file created by fwrite() - php

I have a file called generator.php that uses fwrite() to create a result.php on the server (Apache, PHP4).
One of the lines in result.php is a PHP include() statement.
So, in generator.php:
if (!is_file($fname)){
$resultfile = fopen($current_path . "/" . $fname, "w+");
}
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "'. '/inc/footer.php"); ?>' . "\n");
fclose($resultfile);
chmod($current_path . "/" . $fname, 0755);
And in result.php:
<h2>Sponsored Links</h2>
<!-- begin sidebar_top ad -->
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); ?>
<!-- end sidebar_top ad -->
But that include() statement doesn't work when I visit result.php in a browser. The echo statement does, so I know the path is correct.
Another test.php with the same code, which I uploaded using FTP into the same folder, works fine.
The code in the same in both files, when recovered via FTP.
In test.php: (works, echoes and includes correctly.)
<?php
echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php");
?>
Any idea why the include() is working in test.php (created manually) and not in result.php (created using fwrite()), when both are in the same folder?
The only differences I know of between the files:
Owner could be different (wouldn't result.php be created by user nobody?)
Permissions are originally different. FTP'd file (working) is 0775, while the ones created using fwrite() (include not working) had 664, and is chmoded by the generator.php to 0775.
Working test.php file was edited on a Mac with Smultron and uploaded via FTP, while result.php was created by fwrite() in generator.php on Linux, called from a browser.

fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");
you had an extra " in there i think

When PHP4 safe mode is on, the result.php, being written by another uid, cannot not access the included file, which belongs to another uid.
SAFE MODE Restriction in effect. The script whose uid is 48 is not allowed to access
/var/www/vhosts/example.com/httpdocs/ads/sidebar_top.php owned by uid 10010
in /var/www/vhosts/example.com/httpdocs/results/result.php on line 130
I resolved this by opening php.ini and changing to safe_mode_gid = On, and adding my includes directory to safe_mode_include_dir.
I also had to restart Apache to let the changes take effect.

Related

Link a js on a PHP

Hi I'm a Amateur programer and I need help for my web's programer subject. I'm trying to link this js but on the page show me this on the top Warning: file_get_contents(script404.js): Failed to open stream: No such file or directory in C:\xampp\apps\wordpress\htdocs\wp-content\themes\villanueva-s_theme-brown_nights-\404.php on line 58
<?php
$script = file_get_contents('script404.js');
echo "<script>".$script."</script>";
?>
You must have an invalid path.
You could use __DIR__.'/script404.js' if this file is in the same folder as script you running.
See Evert's answer in Why include __DIR__ in the require_once?
You can just use:
echo "<script src=\"script404.js\"></script>";
Or, you should point to the right directory, like:
$script = file_get_contents('C:/path/to/script404.js');
echo "<script>".$script."</script>";
Or, I wouldn't advice you to do this, but if it is in the same directory:
$script = file_get_contents('./script404.js');
echo "<script>".$script."</script>";

unlink doesn't work ... file is writable and exist

I have an image sharing website and recently I've noticed I can't delete some of my images via script.
My file is writable and exist (so it's not a permission issue), but why can't I unlink it?
echo $file = base_path('./files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg');
echo is_writable($file) ? ' #is_writable ' : ' !!is_writable ';
echo is_file($file) ? ' #is_file ' : ' !!is_file';
$res = unlink($file);
var_dump($res);
Here is the result:
./home/siteecom/domains/site.com/public_html/files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg
#is_writable #is_file bool(false)
I've also tried relative path ... didn't work
I recently run into an issue like this before.
Firstly you'll need to turn on error reporting as unlink() will tell you exactly what's wrong:
ini_set('display_errors', 1);
error_reporting(E_ALL);
You'll want to make sure the directory that contains the file you want to delete is writable (please supply the chmod permissions for us to help furthur).
You should look into using realpath() to get the absolute path to the file. (I don't think this is the issue as it doesn't throw a file not found error).
Your problem is almost certainly related to incorrect permissions for the directory you're trying to delete in and also the script thats trying to delete said files.
If you could supply those permissions of both with something like:
echo "Directory = ".substr(sprintf('%o', fileperms(DIRECTORY)), -4) . "<br />";
echo "PHP File = ".substr(sprintf('%o', fileperms(SCRIPT)), -4) . "<br />";
We can try and help you further.
You probably missed that in order to delete a file you'll need write permissions to the file and it's ancestor folder. Make sure that the directory ./files/images/2013/11 is writable by PHP.

Get directory of current php file, which might have changed since the script started to run

I hope you can help. I need to find to way to get the current directory of the currently running PHP script.
The location of the script itself might have changed since the script was started.
I cannot use any magic constants such as __FILE__ or __DIR__ as a basis as they appear to be defined by the PHP processor at the beginning of execution and so continue to point to the original location of the script.
To test out ideas, I'm doing the following (from the command line on linux):
Have a file named test.php in directory test_1 i.e. test_1/test.php
Run this file on the command line with: php test_1/test.php
Open another terminal session and rename the directory with mv test_1 test_2
Keep an eye on the original terminal and hope you can find that it is reporting the new test_2 dir not test_1
The script used for this test was as follows:
<?php
while (true) {
echo "__FILE__: " . __FILE__ . "\n";
echo "__DIR__: " . __DIR__ . "\n";
echo "realpath(__FILE__): " . realpath(__FILE__) . "\n";
echo "realpath(__DIR__): " . realpath(__DIR__) . "\n";
echo '-----'. "\n";
sleep(5);
}
So, the question: How do you find out the current directory of the script running now (not where it was when the script started)?
The inode of a script will not change, even if it is moved to a different location in the same filesystem.
You could get the inode and pathname of the file at the beginning of the script, using the built-in function getmyinode:
<?php
$myinode = getmyinode();
$mypath = realpath(__FILE__);
?>
Then whenever you want to get the file path, call a function which compares the two and updates the path if necessary:
<?php
function getMyPath() {
global $myinode, $mypath;
if (fileinode($mypath) != $myinode) {
// File moved!
$searchfrom = '/'; // Maybe you can change this to $_SERVER['DOCUMENT_ROOT']
$mypath = shell_exec("find " . $searchfrom . " -inum " . $myinode . " -print -quit 2>/dev/null");
if (fileinode($mypath) != $myinode) {
// error; not found
}
}
return $mypath;
}
?>
It will be very slow to search for the inode if/when the file is moved but I don't see any way around that. Also note that if there are hardlinks then there could be multiple paths to the same file; the -print -quit above simply stops after the first one.
This will only work on Linux due to the use of inodes and the find shell command.
dirname(__FILE__);
Maybe this will be helpful?

Where does file_put_contents save the files to?

I have read the documentation and it doesn't seem to indicate where can I expect the file to be created. I assumed that If I used file_put_contents on a server then a txt file would be created in the same place where the php file running it is. What am I missing here? I'm trying to save the url's in a text file as well.I just need them on my computer really not on the server.
while ($blekr<=$blekko_count)
{
echo '<a href='.$Blekko[$blekr]['url'].'><h4>'.$Blekko[$blekr]['url_title'].'</h4></a>';
echo '<p>'.$Blekko[$blekr]['snippet'].'<p>';
echo '<b>'.$Blekko[$blekr]['engine'].'</b>';
$file = 'Blekko.txt';
file_put_contents($file, $Blekko[$blekr]['url'], FILE_APPEND);
echo '<hr>';
$blekr++;
}
Unless you specify a different directory (using one or more slashes), the file is saved in the current working directory. getcwd() returns the current working directory; chdir() changes it.

php unlink file renovated

I want to delete with php (unlink function) file which is out of webroot. my web root is in
C:\server\webroot\project\... in webroot I have folder named project and in there I have .php files.
whats about files directory. it is situated C:\server\mp3_files...
Also I've created in httpd.conf Alias("mp3") of mp3_files directory
I am writing this script in C:\server\webroot\project\test.php
script is like so =>
function delete($filename){
if (unlink("/mp3/" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
this script gives me in php-errors => PHP-WARNING No such file or directory
also I have in (test.php) html form this =>
Download
And this works (It opens this file.txt)
So I'm wondered why can't delete with marked function "delete($filename)" ?
"/mp3/" . $filename is an absolute filepath, not relative to the webserver root, so it's assuming that you have an mp3 directory under your filesystem root when you should be looking under /server/mp3
EDIT
And is it /server/mp3 or /server/mp3_files
your post seems to contradict your code
File function in PHP go from the file system root.
You should write:
function delete($filename){
if (unlink("C:\\server\\mp3_files\\" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
To make sure the internal PHP file path cache gets the correct information, reset with it with clearstatcache() before and after the unlink. Normally the path cache is reseted after every PHP function which is related to file manipulation. Reseting the cache is required if you remove files with shell_exec('rm file.txt') or similar.
See http://php.net/manual/ini.core.php#ini.realpath-cache-size and http://php.net/manual/ini.core.php#ini.realpath-cache-ttl

Categories