I am using unlink() function to delete the folders under assets folder that yii automatically generate. but it gives me the file permission error.
I am using wamp on windows. and also set the folder permission settings in the folder properties and also un-check the read-only option in the properties.
but still have the issue.
error is unlink(C:\wamp\www\mysite\assets\8ccb2dcf): Permission denied
my code is
$files = glob('C:\wamp\www\mysite\assets\*');
foreach($files as $file){
if(file_exists($file)){
//here we need to give the permission.
chmod($file,0755);
unlink($file); // delete file
echo 'inside';
}
Related
I am working on a website that uploads images in sub dir, for example, every time the user uploads an image gets stored in storage/dropzone/upload/timestamp_date/image_name.jpg the problem I faced is the image is not showing up on the client side when I looked on my shared hosting I found that stores function to create a directory with no executable to world permission 700 and I wanted to be 755 or 777.
I tried to change the permissions of my shared hosting files but it works only once cuz when a new directory is created will create it with 700 permission
You can create a helper function for common use inside whole application.
use Illuminate\Support\Facades\File;
function checkFolderExists($folder, $permission)
{
if (!File::isDirectory(base_path('storage/' . $folder))) {
File::makeDirectory(base_path('storage/' . $folder), $permission , true, true);
}
return 'public/' . $folder;
}
where
$folder = 'dropzone/upload/timestamp_date'
$permission = 0777 or 0775 or your_choice
And after check the store folder and permission your file store path with store image in that specific folder :
$filePath = checkFolderExists('dropzone/upload/timestamp_date', 0775 or 0777 or your_choice);
$fileStoredPath = $request->file('form_name_field')->store($filePath);
I am creating a medium size application.
This application consists of a lot of products.
Now these products have many images (one product can have 5 - 6 images)
To try and make some sort of ordering I want to create one folder for each product this folder contains all images that is bound to the product.
Now so far I have tried the following:
move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name']);
However when I try this I get the following error:
Warning (2): move_uploaded_file(/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png): failed to open stream: No such file or directory [APP/Controller/ImagesController.php, line 56]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php472ci6' to '/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png' [APP/Controller/ImagesController.php, line 56]
Now I am not a complete noob and know that this means that I am missing permissions to the folder.
However the problem is that if the folder does not exist (i.e this is the first time an image for that product is uploaded) then a new folder should be created.
My question is two parted.
Does this automatically create a new folder if it doesn't already exist?
How can I give permission to a newly created folder so that I avoid this problem?
[I] know that this means that i am missing permission to the folder.
Actually no =). The error message reads:
failed to open stream: No such file or directory
Which makes no reference to permissions the problrm is: the containing-folder you're trying to write to doesn't exist.
Does this automatically create a new folder if it doesn't already exist?
No.
How can i give permission to a newly created folder?
It's not necessary to do so - anything created will have the correct permissions to permit the webserver user to read the files. However first it's necessary to try and create a folder, which in the question isn't the case.
Using CakePHP, the Folder class can be used to do that:
App::uses('Folder', 'Utility');
$dir = new Folder('/path/to/folder', 2);
The second parameter is used to create a new folder if it doesn't exist. In the context of the question that means the code would look something like this:
function whatever() {
if ($this->request->data) {
...
$unused = new Folder(APP.'product_images/'.$product_id, true);
if (move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name'])) {
...
} else {
...
}
}
}
The folder APP/product_images should already exist, and must have permissions such that the webserver user (e.g. apache) can write to it otherwise it will not be possible to create the sub-folders/upload files. Assuming APP/product_images exists and the webserver user has permissions to write to it, there is no need to modify permissions of uploaded files - files created by a user are by default readable by that user.
Try this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
1) Does this automaticly create a new folder if it doesnt already exist. => file_exists and mkdir
2) how can i give permission to a newly created folder so that i avoid this problem => 0777
No, it won't create the folder dynamically.
Use chmod() to change permissions.
For checking the existence of some folder you can use is_dir() too.
It's always a good idea too add the magic constant
__DIR__
to the file or directory path.
( __DIR__
gives out the path to the directory in which your script is located). In the errormessages "APP" is highlighted in a different colour than the path name. This could be a hint that the path cannot be located.
I'm frustrated about deleting file in ubuntu using PHP unlink().
I created a very simple simulation as follow:
create a folder named "files" beneath /var/www with 766 permission.
upload a file, let say "image.png" in that folder & set the permission into 666
create a php file named delete.php, set the permission to 644 and upload to /var/www directory
Call the file in browser (I use localhost)
The "image.png" still exists in "files" directory
Here is the php script of delete.php :
$filename = 'image.png';
$file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $filename;
unlink($file);
I also tried the following script :
$filename = 'image.png';
$dir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'files';
chdir($dir);
unlink($filename);
But still can't delete the file.
Unlink throws a warning on failure. Check if E_WARNING is visible for you to find out whats going on.
It usually boils down to user rights. Keep in mind if your script is executed by a browser, usually a user named wwwrun or wwwdata (or something similar) is executing it on your server.
Check if this user has permissions to delete, then try again.
The folder/owner of the directory could be a different user than the user being used to run php.
You should create a folder with the user php assigned. If you cannot do that yourself ask your ISP to do it. That is how I solved a similar problem.
One user cannot delete files of another user on a unix system.
If you would set it to 777 then you could delete it...
I have two files:
b.php and test.txt
<?php
$b = "test.txt";
unlink($b);
?>
and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied
why? b.php and test.txt is 777 and the same group/login
if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?
You (as in the process that runs b.php, either you through CLI or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.
Note that if you use the PHP chmod() function to set the mode of a file or folder to 777 you should use 0777 to make sure the number is correctly interpreted as an octal number.
You'll first require to close the file using fclose($handle); it's not deleting because the file is in use. So first close the file and then try.
in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file" , should take care that Folders must delete by php rmdir() function and if u want to delete a "Folder" by unlink() , u will encounter with a wrong Warning message that says "permission denied"
however u can make folders & files by mkdir() but the way u delete folders (rmdir()) is different from the way you delete files(unlink())
eventually as a fact:
in many programming languages, any permission related error may not
directly means an actual permission issue
for example, if you want to readSync a file that doesn't exist with node fs module you will encounter a wrong EPERM error
// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);
if($do=="1"){
echo "The file was deleted successfully.";
} else { echo "There was an error trying to delete the file."; }
Try this. Hope it helps.
The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use;
1. create a correct path to your file
// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder
That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.
In Windows and before PHP version 7.3.0, check that your file has been closed before unlinking it,
as said in https://www.php.net/manual/en/function.unlink.php :
On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.
As an exemple :
$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');
fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);
unlink(realpath($insertedLinesFilePath));
I'm getting the following error when trying to call mkdir() on a server...
Warning: mkdir() [function.mkdir]:
Permission denied in
/home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php
on line 373
The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.
How can I find out if the uploads folder is writable?
protected function category_images_base_dir()
{
// Where should the dir be? Get the base WP uploads dir
$wp_upload_dir = wp_upload_dir();
$base_dir = $wp_upload_dir[ 'basedir' ];
// Append our subdir
$dir = $base_dir . '/cat-images';
// Does the dir exist? (If not, then make it)
if ( ! file_exists( $dir ) ) {
mkdir( $dir ); //THIS IS LINE 373
}
// Now return it
return $dir;
}
is_writable() is probably the function you're looking for.
http://cz.php.net/manual/en/function.is-writable.php says:
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Also, the directly next line is relevant here:
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)
Make sure the parent folder is writable to the process that the web server runs as.
Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?