fopen creates files owned by root in php - php

I'm writing the code for converting disqus comments to HashOver system, and I have code like this:
// $threads is list of threads from disqus api that I cached on disk
foreach ($threads as $thread) {
$dir_name = getSafeThreadName(preg_replace("%^https?://[^/]+%", "", $thread['link']));
$dir = 'threads/' . $dir_name;
if (!is_dir($dir)) {
mkdir($dir);
chmod($dir, 0774);
}
// ...
// $thread_posts are $post that belong to $thread
foreach($thread_posts as $post) {
$xml = new SimpleXMLElement('<comment/>');
// ...
// $name_arr is array of numbers for HashOver comments
$fname = $dir . '/' . implode('-', $name_arr) . ".xml";
$f = fopen($fname, 'w');
fwrite($f, $xml->asXML());
fclose($f);
chmod($fname, 0664);
}
}
it created directory for each of my posts in threads that that is read/write with owner apache:apache and inside there are files like 1.xml with owner root:root
why root:root? How can I make it apache:apache?
EDIT:
This is no a duplicate, I don't want to change permission to apache:apache from root:root, which can be done using chown, but I want to make it so it don't change it to root:root in first place, I also want to know why it changed to root:root. This looks like a bug in php or apache for me or some wrong configuration in apache on my side. I don't think is the code since it just open, write and close file.

Don't know why and I would like to know, but this solve the issue:
I've use this:
chmod($dir, 0775);
instead of:
chmod($dir, 0774);
directory was not executable for others and it make files in that directory owned by root when created. Very weird.

Related

How to save file in subfolder using php

I have a script with a mysql query which saves a file called invoice.xml every day automatically by running a cron job. In case no data is found a no_orders.txt is saved.
I would like this file not be saved to the same folder as the script.php file is in but to a subfolder called invoices.
The renaming of the old invoice.xml is done with the following code
// rename old file
$nowshort = date("Y-m-d");
if(file_exists('invoice.xml')) {
rename('invoice.xml','invoice_'.$nowshort.'.xml');
}
The saving is done with the following code:
if($xml1 !='') {
$File = "invoice.xml";
$Handle = fopen($File, 'w');
fwrite($Handle, $xml1);
print "Data Written - ".$nowMysql;
fclose($Handle);
#print $xml;
die();
} else {
print "No new orders - ".$nowMysql;
$File = "no_orders_".$nowshort.".txt";
$Handle = fopen($File, 'w');
fclose($Handle);
die();
}
Could I please get assistance how to save this file to a subfolder. Also the renaming of the existing file would need to be within the subfolder then. I have already tried with possibilities like ../invoice/invoice.xml but unfortunately without any success.
Thank you
Just give the path of file 'invoice.xml' to $File.
Otherwise create some $Dir object which will point to Folder named 'invoice', then use accordingly
Use __DIR__ magic constant to retrieve your script.php directory, then you can append /invoice/invoice.xml .
Example if path to your script php something like this:
/var/www/path/to/script.php
$currentDir = __DIR__; //this wil return /var/www/path/to
$invoicePath = $currentDir.'/invoice/invoice.xml';

PHP - Function to read and write a TXT file

I'm making a function on WordPress to get the content of the robots.txt file. If the file doesn't exist, create it with default content. I will use it for my options page. Well, this is my code, it should work almost creating the file, but it doesn't:
function get_robots($robots_file) {
$robots_file = get_home_path() . 'robots.txt'; //The robots file.
$dir = get_home_path(); //The root directory
if(is_file($robots_file)){
$handle = fopen($robots_file, "r");
$robots_content = fread($handle, filesize($robots_file));
fclose($handle);
} else {
$default_content = "User-agent: *\nDisallow:";
chmod($dir, 0777);
$handle = fopen($robots_file, "w+");
$robots_content = fwrite($handle, $default_content);
fclose($handle);
}
chmod($dir, 0744);
return $robots_content;
}
I'm not sure if the problem is is_file, or the fopen($robots_file, "w+" (should it be "r"?) after the else. And I'm not sure about the permissions. Is the 777 needed? Is the 744 the default for the root directory of WordPress?
And I use the return to use it as variable later; I suppose the fopen is already creating the file. Am I right?
Thanks in advance.
The first thing, I would use completely different functions, you have file_put_contents() and file_get_contents() for such simple operations.
So possible simpler solution is:
function get_robots() {
$robots_file = get_home_path() . 'robots.txt'; //The robots file.
if(file_exists($robots_file)){
return file_get_contents($robots_file);
} else {
$default_content = "User-agent: *\nDisallow:";
file_put_contents($robots_file, $default_content);
return $default_content;
}
}
I don't see any point to pass $robots_file as function argument so I removed it. You should check if this code simple works.
I also don't see any reason to change $dir permissions as you showed in your code. It should be rather set manually and you definitely shouldn't change your root directory permission in such function.
EDIT
Because this function uses get_home_path() and this one is available probably only on admin panel you have to do it in different way. You may add the following code to the end of your index.php file:
function get_robots($path)
{
$robots_file = $path . DIRECTORY_SEPARATOR . 'robots.txt'; //The robots file.
if(file_exists($robots_file)){
return file_get_contents($robots_file);
} else {
$default_content = "User-agent: *\nDisallow:";
file_put_contents($robots_file, $default_content);
return $default_content;
}
}
get_robots(getcwd());
(Of course if you want, you may move get_robots() function to some other files.
However you should consider if this is the best approach. You will run this function each time your site will be viewed and it's tiny waste (in fact you will probably want to create robots.txt file just once). You could for example create robots.php file and if you want to run it you can run http://yourwordpressurl/robots.php. It's of course your call.

php mkdir folder tree from array

I'm trying to create a folder tree from an array, taken from a string.
$folders = str_split(564);
564 can actually be any number. The goal is to create a folder structure like /5/6/4
I've managed to create all folders in a single location, using code inspired from another thread -
for ($i=0;$i<count($folders);$i++) {
for ($j=0;$j<count($folders[$i]);$j++) {
$path .= $folders[$i][$j] . "/";
mkdir("$path");
}
unset($path);
}
but this way I get all folders in the same containing path.
Furthermore, how can I create these folders in a specific location on disk? Not that familiar with advanced php, sorry :(
Thank you.
This is pretty simple.
Do a for each loop through the folder array and create a string which appends on each loop the next sub-folder:
<?php
$folders = str_split(564);
$pathToCreateFolder = '';
foreach($folders as $folder) {
$pathToCreateFolder .= DIRECTORY_SEPARATOR . $folder;
mkdir($folder);
}
You may also add the base path, where the folders should be created to initial $pathToCreateFolder.
Here you'll find a demo: http://codepad.org/aUerytTd
Or you do it as Michael mentioned in comments, with just one line:
mkdir(implode(DIRECTORY_SEPARATOR, $folders), 0777, TRUE);
The TRUE flag allows mkdir to create folders recursivley. And the implode put the directory parts together like 5/6/4. The DIRECTORY_SEPARATOR is a PHP constant for the slash (/) on unix machines or backslash (\) on windows.
Why not just do:
<?php
$directories = str_split(564);
$path = implode(DIRECTORY_SEPARATOR, $directories);
mkdir($path, 0777, true);
Don't know what you're really trying to do, but here are some hints.
There are recursive mkdir:
if(!file_exists($dir)) // check if directory is not created
{
#mkdir($dir, 0755, true); // create it recursively
}
Path you want can be made in two function calls and prefixed by some start path:
$path = 'some/path/to/cache';
$cache_node_id = 4515;
$path = $path.'/'.join('/', str_split($cache_node_id));
Resulting path can be used to create folder with the code above
So here we come to a pair of functions/methods
function getPath($node_id, $path = 'default_path')
{
return $path.'/'.join('/', str_split($node_id))
}
function createPath($node_id, $path = 'default_path');
{
$path = getPath($node_id, $path);
if(!file_exists($path)) // check if directory is not created
{
#mkdir($path, 0755, true); // create it recursively
}
}
With these you can easily create such folders everywhere you desire and get them by your number.
As mentioned earlier, the solution I got from a friend was
$folders = str_split(564);
mkdir(implode('/',$folders),0777,true);
Also, to add a location defined in a variable, I used
$folders = str_split($idimg);
mkdir($path_defined_earlier. implode('/',$folders),0777,true);
So thanks for all the answers, seems like this was the correct way to handle this.
Now the issue is that I need to the created path, so how can I store it in a variable? Sorry if this breaches any rules, if I need to create a new thread I'll do it...

How to implement the creation of folders structure for uploaded files on php

Say I want my uploaded files to be stored in a structure of subdirectories as shown below:
/uploads/0/0/kitten.jpg;
/uploads/0/1/kitten1.jpg;
/uploads/1/10/pic.jpg;
So that each subfolder can contain 999 files at max i.e. when /uploads/0/ reaches its limit, the folder /uploads/1/ is created automaticaly and next file goes there. Each folder also can contain 999 subfolders.
The question is - How do I do that? My main concern is how to determine where to put the newly uploaded file. Could you describe the algorithm? I cannot think of anything better than performing these steps each time:
Looking for the latest created folder in /uploads/ and if it's empty - creating one, like so:
$contents = $scandir('/uploads');
$dirs = array();
foreach ($contents as $path) {
if (is_dir($path)) {
$dirs[] = $path;
}
}
if (empty($dirs)) {
//create new dir and save file there subsequently
$saveTo = $parentDir . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR . "1";
mkdir($saveTo, 0777, true);
} else {
$last = array_pop($dirs);
}
We do basically the same for this subfolder and so on(and I guess I may want to use recursion here somehow). Am I moving in right direction here or maybe I am overcomplicating things? Is there a better way to do this? Anything, please. I am only beginner in php and coding.
One approach is to keep a running total of all uploads, (atomically) increasing it for each upload.
Then the directory can be created like so:
$dir = sprintf('/uploads/%d/%d', floor($total / 1000), $total % 1000);
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}

Change folder permission to 777 using PHP temporarily

I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.
If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.
Is there any way in PHP to temporary change the permission to 777 while uploading video?
PHP provides a function, chmod() for the task.
Attempts to change the mode of the specified file to that given in mode.
You can put it in an if statement, and if it returns false, you can skip the upload file part.
The usage will be like
if( chmod($path, 0777) ) {
// more code
chmod($path, 0755);
}
else
echo "Couldn't do it.";
As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)
There is a way (PHP provides chmod function) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.
You have to initialize the config for the upload, like this:
$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);
You can use chmod() function.
For more information, try here
Warning: You cannot undo the file permissions that are changed by the script below. Proceed with extreme caution.
Important: this code should only be used if you remember to delete it immediately after use. As above, its use may put your site into an insecure state.
//replace dirname(__FILE__) with desired folder.
file_fix_directory(dirname(__FILE__));
function file_fix_directory($dir, $nomask = array('.', '..')) {
if (is_dir($dir)) {
// Try to make each directory world writable.
if (#chmod($dir, 0777)) {
echo "Made writable: " . $dir . "";
}
}
if (is_dir($dir) && $handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file")) {
// Recurse into subdirectories
file_fix_directory("$dir/$file", $nomask);
}
else {
$filename = "$dir/$file";
// Try to make each file world writable.
if (#chmod($filename, 0666)) {
echo "Made writable: " . $filename . "";
}
}
}
}
closedir($handle);
}
}
or you can use terminal for this
chmod -R 755 public_html/test

Categories