PHP mkdir not working as expected - php

I'm working on some PHP that will create a folder every time a request is made to my server. I can't seem to get my syntax proper. If I just used the $date variable it works no problem, however when I add the "clean" folder before it, it won't create the folder.
<?php
$time = $_SERVER['REQUEST_TIME'];
$date = date(Ymd_Hms, $time);
$new = "clean/".$date;
echo $new;
if(!is_dir ($new))
{
mkdir($new);
}
?>

This is scenario is happened to me when I recently configure server
mkdir() function was working and it was giving permission error constantly.
So I found out the solution that folder (in this case clean folder) in which you are creating another folder must have 0777 / 0755 permission and its user:group must be same as that of user and group given in httpd.conf file in apache or httpd folder .
chown -R root:root clean/
when I gave that to that folder it was working like magic.
Please check with this , if all above solutions failed then this will definitely help you.

Put the in ' quotes and give the mkdir() appropriate params and it should work fine:
<?php
$time = $_SERVER['REQUEST_TIME'];
$date = date('Ymd_His', $time);
$new = "clean/".$date;
echo $new;
if(!is_dir ($new))
{
mkdir($new, 0777, true);
}
?>
mode:
The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod()
Note: mode is ignored on Windows.
Return Values:
Returns TRUE on success or FALSE on failure.
Note: You don't have to create the folder clean first IF you're using the recursive = true option, Otherwise a folder named clean must be created first.
PHP Manual: mkdir
PS. I've created a directory using this code before posting so it should not
have any problem.

Related

PHP file permissions, chmod & mkdir not working

I created a php function that allows me to add images to a folder within my web application project. Right now it only works when I manually set the permissions of the folder to write enabled. Unfortunately that is not exactly what I need, because when I push my code to my group members with git, they would still have to manually set the permissions of the folder to enable writing as well, in order for my pushed code to work. all my group members and I are each running this project locally via an apache server, and I believe the problem is that apache does not have access to write within any of the folders within the project unless the folders are somehow set to write enable for everyone. I feel like it's not feasible to tell all my group members to change their folder permissions just so my one function can write to a folder.
I am looking for a way to set file permissions within my php code, I have already tried php functions such as chmod and mkdir to set permissions, however it gives me errors saying that I don't have permission to use those functions, which I guess makes sense, but is there a possible work around?
$structure = 'path';
mkdir($structure, 0777, true)
or
$structure = 'path';
mkdir($structure)
chmod($structure, 0777);
<?php $result = mkdir ("/path/to/directory", "0777");
?>
Try this it will working
Try using this (if shell_exec() is allowed):
(list of issues: only works on linux, has a security issue with potential solutions at bottom of post)
<?php
function readFile($file) { return shell_exec("cat $file"); }
function writeFile($file,$cont) { return shell_exec("echo $cont > $file"); }
function makeFile($filename) { return shell_exec("touch $filename"); }
function makeFolder($filename) { return shell_exec("mkdir -pv $filename"); }
// the -pv was used so you could do makeFolder("1/2/3/4")
function appendFile($file,$cont) { fileWrite($file,readFile($file).$cont); }
function appendToTopOfFile($file,$cont) { fileWrite($file,$cont.readFile($file);); }
?>
Use escapeshellcmd or escapeshellargs to escape user input.
ex.
$file=escapeshellcmd($_GET['file']);
echo readFile($file);

PHP: Why is chmod() setting my file/folder to 555 or 444?

I created a web application with a file browser. I'm trying to add a functionality where the user can change the chmod/permissions via an ajax request which is handled via PHP on the back-end.
(Side Note: I'm running my local with WAMP)
So initially, I'm reading the permissions with this
substr(sprintf('%o', fileperms($relativePath)), -4)
to get this format (0777, 0644, etc), if not it returns something like 32726. This info is used to be displayed in the UI for the user to know whats current.
However, when I run the script, I set it to 0777 and it seems to run fine. But then when I read the file again, it returns 0555 or 0444. Anyone know what I'm missing?
Does your web server own the files it is trying to change the permissions on? You can check whether chmod ran correctly or failed by testing its return value. It will return FALSE if the webserver did not have permissions. For more information you can read: http://php.net/manual/en/function.chmod.php
<?php
$is_success = chmod("myfile.pdf", 777);
if($is_sucess) {
echo "success<br />\n";
}
I realized this was not an issue, but rather the chmod command does not work properly on a windows/apache setup.

PHP SVN Checkout via HTTPS

I want to create an SVN checkout PHP script. All you need is to call a function and pass two parameters: the SVN URL and the output path.
My problem is, that our SVN server can only be accessed via https. But through https, the function doesn't work. Normally the function should return a boolean, but I just get nothing. My first thought was, that I have no permission to write into the output path folder, but I changed the permission to 777 (temporarily). Still doesn't work. I also tried to get some files from another SVN trunk. Behold, this is working. I get the files. Any idea how to get this to work?
Aah, and yes, I set the svn trunk permission to read-write for everyone.
Here's is my code:
<?php
$result = svn_checkout('https://{LINK_TO_SVN_TRUNK}', dirname(__FILE__) . '/tmp');
echo "Result: ".$result;
?>
This is what I have used successfully in the past:
svn_auth_set_parameter(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, "username");
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, "password");
$changeLog = svn_log($path, $start_revision, $end_revision);
Please confirm if the extension is enabled. php.ini should consist of extension=svn.so or php.d folder should consist svn.ini with the line extension=svn.so. You can check for extension in phpinfo();

php unlink. returns no error message and doesn't delete my file

I'm simply trying to delete a file using PHP's unlink. I found an answer on here that told me the best way to do this and I implemented it. Heres the code I am using.
$log_path = realpath($log_file);
if (is_readable($log_path)){
print file_exists($log_path);
$deleted = unlink($log_path);
print "Deleted:".$deleted;
print "Exists:".file_exists($log_path);
}
This just prints 1Deleted:exists:1. I also tried to add in print_r(error_get_last()); under the unlink, but this also returned nothing. All files in the directory are chmod 777 * and there are no other file handlers open. ....what the heck...
The code you used is needlessly cumbersome. A simple call to unlink should do the trick:
unlink( $log_file );
But let's find out what is going wrong. The file exists, because you enter the loop where the print statements are done. The unlink call probably returns false, because the output is "11" and not "111".
So my gut says, it must be a file permissions issue. Are you sure the web user has permission to remove the file? Can you show us the folder permissions, for instance by running ls -la on the command line and pasting the output?
unlink returns either true or false. If you try to print false (print $deleted;), this will print an empty string.
Try this instead:
print $deleted ? "The file was deleted" : "The file was not deleted";
I think you need to check if $log_path is file. Use:
if( is_file( $log_path ) AND is_readable( $log_path ) )
Also, add next line to begin of your script to show all errors and warnings:
ini_set( 'error_reporting', E_ALL );
Its not that your files need to be 0777. It also necessary that your directory can be accessed. What's the mod of your directory?
Next: print $deleted; apparently print false, which is shown as nothing. Try this: echo $deleted ? 1 : 0;
I don't really know the problem, but you should check, if the file is writable and not if it is readable:
<?php
$log_file = "/tmp/test.log";
$log_path = realpath($log_file);
echo "Testing: ". $log_path."\n";
if (is_writable($log_path)) {
echo "exists? ";
echo (file_exists($log_path))?"yes\n":"no\n";
$deleted = unlink($log_path);
echo "deleted? ";
echo ($deleted)?"yes\n":"no\n";
echo "exists? ";
echo (file_exists($log_path))?"yes\n":"no\n";
} else {
echo "file unwritable\n";
}
This code works fine for me (yes, it's also messy ;) ).
file_exists caches the result of the operation, you should call clearstatcache() before the second call
for me it was promises issue solve it with
chown -R www-data:www-data /var/www/
chmod -R g+rwx /var/www/
chmod -R 0755 /var/www/
make sure that php run under user www-datain www.conf e.g I'm using PHP 7.2
nano /etc/php/7.2/fpm/pool.d/www.conf
Change these values :-
listen.owner = www-data
listen.group = www-data
unlink function in my case just returned false everytime. first, thanks to Viktor for this:
Also, add next line to begin of your script to show all errors and warnings: ini_set( 'error_reporting', E_ALL );
It helped me to see warning:
unlink(): http does not allow unlinking in PHP.
And i finally found the solution, described here https://a1websitepro.com/warning-unlink-http-not-allow-unlinking-php/.
The case is that i user http path to the file, but unlink function requires the absolute path to the file from your server.

file_put_contents and file_get_contents not work other directory

although file_put_contents and file_get_contents works for /var/www/html/ they not work for any other directories that have same owner(apache) and chmod(644). What may be the reason? Thanks for helps...
edit:
working code:
$contents = file_get_contents("/var/www/html/osman");
$contents = str_replace("mehmet", '', $contents);
file_put_contents("/var/www/html/osman", $contents);
not working code:
$contents = file_get_contents("/opt/blaris/etc/webfilter/lists/osman");
$contents = str_replace("mehmet", '', $contents);
file_put_contents("/opt/blaris/etc/webfilter/lists/osman", $contents);
and as I sad before two file have same owner and chmod...
Ibrahim
as you mentioned both function (get and put) return false. I am pretty sure this is caused by improper set file/directory permissions (had a same issue some time ago ...).
I solved it by checking the path (in your case "/opt/blaris/etc/webfilter/lists/") if all permissions are set as they should. This also includes read/write permissions for the lists directory itself and ALL directories above.
So make sure all directories in your path are (at least) executable by the apache user (e.g.
Is /opt executable for apache user?
Is /opt/blaris executable ...
Is /opt/blaris/etc executable ...
)
Also make sure the PHP script runs really under the apache user (<?php echo shell_exec('whoami'); ?> should give you the information)
If you want to debug it on shell level you may try the following command and see what the output says (in case you have the rights to perform the command on the server ...):
sudo -u apache touch /opt/blaris/etc/webfilter/lists/osman
Hope that helps ;)

Categories