JFolder::create: Could not create directory Joomla - php

I'm using below PHP code here to create a particular folder if it didn't exist. I'm using joomla 2.5
$path = my/path/goes/here;
$folder_permissions = "0755";
$folder_permissions = octdec((int)$folder_permissions);
//create folder if not exists
if (!JFolder::exists($path)){
JFolder::create($path, $folder_permissions);
}
But this code throws below error
JFolder::create: Could not create directory
What could be the reason for this?

2 things I think might be causing the problem:
You forgot a ; on the end of octdec((int)$folder_permissions)
Try removing the whole line $folder_permissions = octdec((int)$folder_permissions)
Update:
This is what I used to create a simple folder:
$destination = JPATH_SITE.'/'."modules/mod_login";
$folder_name = "new_folder";
JFolder::create($destination .'/'. $folder_name, 0755);

What could the reason be?
Simple: The user that tries to create that directory doesn't have permission (or your Joomla is broken).
The user that runs the code is probably www-data (on most *nix/Apache). ALso 'nobody' or 'apache' are possible.
If you have rootpermission try this:
1) become the webuser (eg www-data)
2) Jump to the directory my/path/goes/here
3) type: mkdir mynewdir
And you will probably find out that the user doesn't have sufficient permissions to do so.

edit your configuration.php file under Joomla home direcory, change:
From:
var $tmp_path = '/home/public_html/your_name/tmp';
To:
var $tmp_path = 'tmp';
and tmp should have 777 permission

Related

PHP Change default User on fopen

I'm having problems when trying to write a file with a different user rather than www-data.
I need to leave the file on a mapped unit that it's NOT mine so i cannot change permissions on that one. Insetead i have a username and a group, let's call them 'myself':'myself'
I'm using Symfony 5 Filesystem to move files around but i still use PHP's fopen to create those files. This is what i have so far.
private function moveAndDeleteFile($url_origin, $url_destiny)
{
$filesystem = new Filesystem();
$a = fopen($url_origin, 'wa+');
fwrite($a, 'Test');
fclose($a);
$filesystem->copy($url_origin, $url_destino, true);
if (!$filesystem->exists($url_destiny)) {
return false;
}
$filesystem->remove($url_origin);
return true;
}
This is just a test, so i'm creating a file (name included in $url_origin) and try to copy the file to $url_destiny and then remove the original (again, just a test).
The ting is that the file is always created by www-data and for my target directory i need to set the file owner to 'myself'.
Is there any way i can change the file owner with sudo?
$filesystem->chown($url_origin, 'myself', false);
This returns an error -> Failed to chown file "my/route/file.xml"
Same thing happens when trying
$filesystem->chgrp($url_origin, 'myself', false);
Then i tried to use the default PHP chown() function like this:
chown($url_origin, 'myself');
And get this other Error: Operation not permitted.
I guess that i need to specify somewhere the user's properties but i'm clueless right now. Any ideas on how to pass this through? i'm sure i'm missing something obvious.
Thanks-.
You cannot use "chown" because only "root" can use "chown".
The way is to set "chmod" or change configuration in your server with php-fpm running with myself user.
Regards

File does not exist though the path seems right

I can't figure out why file_exists() don't find my file though the path seem right.
Here is my script :
if($_POST['delete-avatar'] == 'on') {
$q = execute_query("SELECT avatar FROM gj_customers WHERE id_customer = '$_GET[id]'");
$customer = $q->fetch_assoc();
$img_path = $_SERVER['DOCUMENT_ROOT'] . WEBSITE_ROOT . $customer['avatar'];
var_dump($img_path);
if(!empty($customer['avatar']) && file_exists($img_path)){
unlink($img_path);
}
}
I'm on MAMP, my website is in htdocs/gamejutsu/www/ , WEBSITE_ROOT contains '/gamejutsu/www/' and avatars are in img/avatars/ .
var_dump on $img_path returns me : /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg.
If I go on localhost:8888/gamejutsu/www/img/avatars/Sonik_avatar.jpg the image is displayed.
I never enter in the if bloc. If I remove the file_exists test I have a warning : unlink(/Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg): No such file or directory in /Applications/MAMP/htdocs/gamejutsu/www/admin/members.php on line 67
I'm pretty sure i'm missing something but everything seems ok for me and I use the same method somewhere else on my website without any problem.
Thank you for your help.
It is complicated debug your application without access but you can try what return realpath();
var_dump(realpath($img_path))
If returns null file doesn't exists or your application haven't acces to it.
Try open your console and this command:
cd /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/
If you have error you have bad path and must manualy check it (easiest will be by console) if you can cd in this directory. Please add here your ls of this directory for check permissions:
ls -la /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/
And if everything look good try change your console user to webserver user (usually www-data) and try acces to file
sudo su
su www-data
cat /Applications/MAMP/htdocs/gamejutsu/www/img/avatars/Sonik_avatar.jpg
If you can acces to this file (it will be binary data) permissions are ok, in other case check if you have right permission to file and if you good owner and group of file.
Try just use website root and do not use $_SERVER['DOCUMENT_ROOT'].
Another way is by identifying the path.
if($_POST['delete-avatar'] == 'on') {
$q = execute_query("SELECT avatar FROM gj_customers WHERE id_customer = '$_GET[id]'");
$customer = $q->fetch_assoc();
$img_path =' filename' . WEBSITE_ROOT . $customer['avatar'];
var_dump($img_path);
if(!empty($customer['avatar']) && file_exists($img_path)) {
unlink($img_path);
}
}

create directory in parent root with php in laravel

my site is in /var/www/mysite directory .i use laravel and want to create directory in /uploads directory .so /uploads is besides of /var .
/var/www/mysite
/uploads
i set 0777 permission for /uploads but permission denied happened.
I try with this code
<?php mkdir('/uploads', 0777 , true) ; ?>
Your web user (www-data, apache or whichever) is restricted by the operating system to use /var/www. You can't read,write,delete any files outside /var/www. So that's not a laravel issue.
Function that create all directories (folders) of given path. No need to write code create each directories (folders) of given path. it will create all directories (folders).
Like : If you want to create directory structure like
organizations / 1 / users / 1 /
So you only need to call this function with directories path like
$directories_path = 'organizations/1/users/1/';
createUploadDirectories($directories_path);
/*
* Method Name : createUploadDirectories
* Parameter : null
* Task : Loading view for create directries for upload
*/
if ( ! function_exists('createUploadDirectories')){
function createUploadDirectories($upload_path=null){
if($upload_path==null) return false;
$upload_directories = explode('/',$upload_path);
$createDirectory = array();
foreach ($upload_directories as $upload_directory){
$createDirectory[] = $upload_directory;
$createDirectoryPath = implode('/',$createDirectory);
if(!is_dir($createDirectoryPath)){
$old = umask(0);
mkdir($createDirectoryPath,0777);// Create the folde if not exist and give permission
umask($old);
}
}
return true;
}
}
Do this:
Use File;
File::makeDirectory(base_path() . '/path/to/directory', 0775, true);
This will attempt to create /path if it doesn't exist. Then /path/to if it doesn't exist. And finally /path/to/directory if it doesn't exist. If all created directories are successfully created then true will be returned.
If you want to place the directory in your public path; use:
File::makeDirectory(public_path() . '/path/to/directory', 0775, true);
Source: Laravel Recipes

PHP mkdir() failing to work

Here's my code; I've renamed the directories, obviously. ;)
$thepath = "/var/www/vhosts/sub.domain.co.uk/web/apps/storage/".$userclient."/evidence/".$scid."/".$doctype."/";
$testdir = is_dir($thepath);
if ($testdir == false) {
mkdir($thepath, 0777);
}
In this case, the following variables apply;
$userclient = '000';
$scid = '9263';
$doctype = 'Insurance Policy';
So, the path should be;
/var/www/vhosts/sub.domain.co.uk/web/apps/storage/000/evidence/9263/Insurance Policy/
I know this works, EVERYWHERE else in my code, I have other applications using an almost identical setup. But the one above, appears to be tripping up on /evidence/ - it sets the permissions to 755, but will then create the directories per time I run the code, if I set evidence to 777 (Octal).
I get the following error message using;
if (!#mkdir($thepath)) {
$error = error_get_last();
echo $error['message'];
}
mkdir(): No such file or directory
Any help would be greatly appreciated, I have to finish this application by Thursday - and this file upload part is the last bit!
Thank you!
PHP can't find directory in which you want to create other directory.
You need to set $recursive param as true:
mkdir($thepath, 0777, true);
Maybe you want to try mkdir($path,$mode,true) to create missing links as well?

Counter won't work when I move it to another server

I set up a hitcounter so that each time someone goes to my site, one of 7 different sidebars loads up. There is a file called counter.php that writes to a text file called hitcounter.txt. All the references to files seem to be relative but when I moved them to a new directory at my new host I got this error instead of a happy hit counter:
Warning: fopen(hitcounter.txt) [function.fopen]: failed to open stream: Permission denied in /usr/local/apache/sites/MY-SITE/counter.php on line 5
Counter.php is pasted in its entirety below, line 5 is the first reference to fopen, both counter.php and hitcounter.txt have 775 permissions, same as they did on the old host.
What am I doing wrong? I'm obviously missing something really simple and embarrassing, so feel free to give me any scorn or abuse with while helping me out.
counter.php:
<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0]++;
$fp = fopen($count_my_page , "w");
if ($fp) {
fputs($fp , "$hits[0]");
fclose($fp);
}
if($hits[0]<=1)
$random_number=0;
else if($hits[0]>1 && $hits[0]<=2)
$random_number=1;
else if($hits[0]>2 && $hits[0]<=3)
$random_number=2;
else if($hits[0]>3 && $hits[0]<=4)
$random_number=3;
else if($hits[0]>4 && $hits[0]<=5)
$random_number=4;
else if($hits[0]>5 && $hits[0]<=6)
$random_number=5;
else if($hits[0]>6 && $hits[0]<=7)
$random_number=6;
else if($hits[0]>7 && $hits[0]<=8)
$random_number=7;
else if($hits[0]>8 && $hits[0]<=9) {
$random_number=8;
if($hits[0]==9) {
$count_my_page=("hitcounter.txt");
$fp = fopen($count_my_page , "w");
$hits[0]=0;
fputs($fp , "$hits[0]");
fclose($fp);
}
}
?>
It probably has something to do with permissions. Set it to 777 and see what happens. If apache runs with own permissions and is not part of your this might be the reason, but I have a couple of suggestions:
use file_put_contents/file_ get_ contents for simple read/writes!
please use $random_number = rand(0,8) OR mt_rand(0,8) if possible instead of these countless lines and as a bonus get rid of all the file reading/writing
Good luck!
Update
Nothing beats a nice example:
<?php
$random_number = mt_rand(0,8);
file_put_contents("hitcounter.txt", $random_number); /* dont know if you still need it */
?>
If you really want to (btw. NOT random!):
<?php
$file = "hitcounter.txt";
$number = (int)file_get_contents($file);
$number = ++$number % 9;
file_put_contents($file, $number);
?>
First, you don't need 775 permissions. You need 666 permissions on the hitcounter.txt. The PHP file can be 644.
The web server probably isn't a member of the group, depending on the host, so you'd need to give the 'Everyone' group write permissions.
The "Execute" bit is needed for folders but not for individual files, since they are not being executed by the OS.
So you know, the 775 is : Owner, Group, Everyone
Owner = Read + Write + Execute
Group = Read + Write + Execute
Everyone = Read + Execute
666 means
Owner = Read + Write
Group = Read + Write
Everyone = Read + Write
644 means
Owner = Read + Write
Group = Read
Everyone = Read
Try setting permissions to 777. If that doesnt work, could be your host doesnt allow file manipulation on the server
Check your include_path to make sure it has a period (current directory) in it. Not sure why it wouldn't, but you never know if your host is wonkey. You can print out your include path from a PHP script using ini_get:
ini_get('include_path');
Other than that, you probably need different file permissions. Here's a useful site describing a lot of information on file permissions.

Categories