I have this problem, I want to execute a cronjob but when I run the cron manually for testing I get permission issues.
I am using the Yii framework and I call the cronjob using Yiic. I want to create a directory structure where every directory contains an image. So we get like this:
/dir/id/
/dir/id/imgsize-1
/dir/id/imgsize-2
/dir/id/imgsize-3
It get's more complex because it could be that imgsize-3 exists while imgsize-1 doesn't. And it could be possible that /dir/id/ is not writeable (0755 perms) so I first need to check if the parent directory (/dir/id/) is writable. If so I should be able to use mkdir to create dir 'imgsize-1' or whatever is my thought so far.
But now the problem occurs that if I want to use chmod to make a the parentdirectory writable I get the error 'chmod: 'path/to/dir' operation not permitted' and after that ofcourse that mkdir results in 'Permission denied'.
How can I solve this. When I use ls -la on the specific directorie I want to make writable I get the following:
4 drwxr-xr-x 9 nobody nobody
Is there someone who can help me with this?
b.t.w. I execute the CLI-commands in PHP with shell_exec.
Kind regards,
Pim
in your command action you should have something like this:
$dir = '/your/path';
if (!is_dir($dir))
mkdir($dir, 0777, true);
Then, running the cron (what's the user you're running the command with?) with the command php yiic yourCommand, the directory should be created in /your/path
Related
The following works in windows:
mkdir('../my/folder/somewhere/on/the/server', 0777, true);
I am talking about PHP mkdir.
It works perfectly, and creates the subfolders recursively. However, if I run the same command on a linux server, the folders aren't created.
Previously I solved this by breaking up the path and creating each folder one by one. But I don't want to do that because it should work with the "resurive" flag set to true. Why isn't it working?
Sorry, but there must be some problem apart from the mkdir command itself.
This tiny example works as expected and recursively creates the directories for me when executed on Linux:
#!/usr/bin/php
<?php
mkdir ('testdir/testdir2/testdir3',0777,TRUE);
?>
This are the thing have discovered
Make sure the root path exists
Make sure the root path is writable
Don't use .. always use real path ...
Example
$fixedRoot = __DIR__;
$recusivePath = 'my/folder/somewhere/on/the/server';
if (is_writable($fixedRoot) && is_dir($fixedRoot)) {
mkdir($fixedRoot . DIRECTORY_SEPARATOR . $recusivePath, 0, true);
} else {
trigger_error("can write to that path");
}
Make sure that your PHP user (eg www-data) has permission to write to the parent folders of any folder it is trying to create. PHP needs to be able to write to the lowest one that already exists.
For example, in the case of ../my/folder/somewhere/on/the/server, if ../my already exists and PHP is able to write to .. but not to my, mkdir will fail.
If your user is www-data, you could use sudo chown -R www-data:www-data ../my to give write permission for my and all its subfolders.
I'm trying to work with php system (exec) functions to run some commands on linux, but it just doesn't work!
I've logged into the system with root user. I created "/1.txt" file and then I ran this php script through apache server: system("sudo cp /1.txt /2.txt", $out); but it doesn't copy the file.
Can you explain why it doesn't work? (I'm new to linux os please explain)
Drop the sudo and give permissions to apache's user on /1.txt and /2.txt, it should work.
If /2.txt doesn't exist, create a directory, put 1.txt on that directory and give permissions to apache user both on the directory and on the file.
/1.txt and /2.txt are at the very top level of the filesystem - is that what you're aiming to do? Either way it's not a good idea.
I'd recommend having an area which your files are in, for example /var/webapp-files/ that way you can do something like:
define('WEBAPP_FILES_LOCATION', '/var/webapp-files');
system("cp ".WEBAPP_FILES_LOCATION."/1.txt ".WEBAPP_FILES_LOCATION."/2.txt", $out);
NOTE: I've defined a constant for the location because it'll be needed in a number of different places and you don't wanna start repeating yourself in case you want to change it any time.
What this'll do is run the system command:
cp /var/webapp-files/1.txt /var/webapp-files/2.txt
Which should succeed if your /var/webapp-files directory has write access for your apache user.
However... PHP does have a built-in function for copying which I'd recommend using instead of the more generic and dangerous system(). You can read about copy() at http://uk3.php.net/manual/en/function.copy.php This will need the full paths as cp does so you can refactor my previous example to:
define('WEBAPP_FILES_LOCATION', '/var/webapp-files');
$copySucceeded = copy(WEBAPP_FILES_LOCATION."/1.txt", WEBAPP_FILES_LOCATION."/2.txt");
echo "The copy attempt ". ($copySucceeded ? "succeeded" : "failed");
Hope that helps.
I'm trying to figure out a cron job which will copy a file (timthumb.php) from the root of ftp (above our public_html directory) and recursively replace all existing instances of the file throughout the FTP. I'm also open to other ideas if someone has another method for doing this.
The master file would live at:
/timthumb.php
A cron would run daily to replace instances such as:
/public_html/wp-content/themes/xxxxxx/scripts/timthumb.php
or
/public_html/mysite1/tools/timthumb.php
(We're assuming the instance will always be named timthumb.php, so there's no need to look into the content of the file to match code)
The goal of this cron is to prevent old instances of timthumb from existing on our server.
Any help or ideas would be greatly appreciated
Thanks
-Sam
PS You can read about the timthumb exploit here:
http://markmaunder.com/2011/08/01/zero-day-vulnerability-in-many-wordpress-themes/
This would do it:
0 0 * * * find /path/to/ftp-root -iname timthumb.php -exec cp /path/to/good-timthumb.php {} \;
I have my web application hosted in /var/www folder. I am creating a folder from one of the PHP scripts of the web application. The default permission of the created folder is drwx------, i.e. 700. But I want that folder to have at least 755 permission.
Up to now I tried: mkdir($path, 0755) and chmod($path, 0755) PHP functions but without any success.
Does anybody know how to solve my problem please?
Millions of thanks beforehand.
Have you tried changing the umask ?
Have a look here: http://nl3.php.net/manual/en/function.umask.php
The easiest way it to:
$oldmask = umask(0);
chmod($path, 0755);
umask($oldmask)
Since you have default permission of 700, which means the parent directory (the directory in which you are trying to create the folder) do not have rw permission for group owner or other users. Most often the running demon(httpd) is not the owner of the parent folder and hence cannot modify the directory.
In simple terms, the php script do not have access to modify or add new directory. You need to change the permission of the parent folder to at least drwxrw-rw- (or 0755).
Use ssh, cpanel or ftp client to do this. If you do it using php script you will end with the same problem again, as parent of parent will have again 0700. ;)
i was using this basic script:
$folderPath = "../path/to/$folder/";
mkdir("$folderPath");
i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser
The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos
I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all
All the older folders were being created with: -w- rwx r-x r-x
and I can't get this recreated
I've tried adding a chmod line into my script:
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
but I can't recreate the same permissions, I'm trying to understand how chmod works, but I can't figure out how to get this very basic function working properly again
Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-
In the folder "root_folder/sales/inventory/$folder/", where "$folder" is as mentioned in your code.
If not found, try in the folder "root_folder/sales/inventory/".
If not found, try in the folder "root_folder/sales/".
If not found, try in the folder "root_folder/".
When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder", where the code will be "Options Indexes".
Also in the PHP page, the logic must be like this:-
<?php
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
// Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable.
chmod($targetFile, 0755);
?>
This will help you when you will be unlinking / deleting the uploaded files from the "$folder" folder.
Hope it helps.
If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.
Imagine that $folder = '/store1/ally23/shelf42'; so your final directory structure is
../sales/inventory/store1/ally23/shelf42, and you want 0777 permisions.
You do:
mkdir($folderPath, 0777, true) || chmod($folderPath, 0777);
Only the final directory shelf42 is chmoded to 0777. The intermediary directories are created with default permissions (in my case 0744).
There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.
If you're in a shared environment, you may also want to chown after upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.
Simply umask means the default permissions for new files/directories:
<?php
umask(022);
?>
This sets the default permissions for user, groups, and others respectively:
0 - read, write and execute
1 - read and write
2 - read and execute
3 - read only
4 - write and execute
5 - write only
6 - execute only
7 - no permissions