change file permmissons after copy them failed - php

I tried to to the following:
Copy files and folder from folder .../myfolder/subfolder etc.
to folder .../anotherfolder/subfolder etc.
Copy this works fine.
The Folder has permissions 0755 and, if I copy that, the new folder has the same permissions.
So I want to change the permission to 0644, I tried to do that with
if(!chmod($path, 0644)){ die("Error"); }
it failed with the message
chmod(): Permission denied
my path is relative, the same as I used to copy.
I don't understand why it doesn't work.
Thank for helping and explain

Related

Running a PHP program in Ubuntu - fopen("filename", "w"): Failed to open stream: Permission denied

I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:
fopen("colors_new.csv", "w");
From the manual,
'w' Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
But instead of the file being created, I am getting
Warning: fopen(colors_new.csv): failed to open stream: Permission denied...
And when I look into the project folder, the colors_new.csv file is NOT created.
When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.
How do I fix this problem in Ubuntu?
WHAT I TRIED:
I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.
NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.
This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work
give filewriting permissions to the directory.
Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.
create a subdirectory inside your directory and give that sufficient rights (755) to do what you want.
F.i. Fopen('./files/colors_new.csv)
Provide the write permission on the folder in which you want to write the file. Your problem will be solved.

Can't copy file with php, permission denied

I'm trying to copy an uploaded file through php to a folder in the home directory. I gave every permission to the Apache user (www-data) but when I'm trying to copy it I get a warning "failed to open stream: Permission denied".
copy("$target_file", "/home/pap-x/meshes/Part_A.dae");
What's wrong?
In addition to www-data having permissions to the directory of $target, it must have at least execute permissions to every directory above the target. If any of those directories deny access then www-data will not be able to find the target directory.

move_uploaded_file() failed to open stream: no such file or directory

I've done your standard checks (is the directory there, are lax enough permissions set), and I'm pretty sure I've covered your standard stupid human tricks. Here's the code that's failing:
move_uploaded_file($_FILES['image1']['tmp_name'], "/public_html/flashsale/assets/img/products/T".$_FILES['image1']['name']);
The directory is there - I copied the path from FileZilla. I even set the permissions to 777, both in FileZilla and in the file manager on the HostGator control panel. This code generates two warnings:
Message:
move_uploaded_file(/public_html/flashsale/assets/img/products/Tsirloin.jpg)
[function.move-uploaded-file]: failed to open stream: No such file or
directory
Message: move_uploaded_file() [function.move-uploaded-file]: Unable to
move '/tmp/phpI5GZ3S' to
'/public_html/flashsale/assets/img/products/Tsirloin.jpg'
In that order. So, the file is being uploaded, the directory exists and is set to 777, what else could I be missing?
you do not need to put the full directory to the file. try to remove /public_html/flashsale/ from your link and see if that will work.
In addition, the file does not need to have 777 permission, I myself upload files to folders with 755 permissions.
also, you can use getcwd(); in the directory your aiming to. the function will give you the directory that you need to use for moving your file. source
The Problem
$dirpath = dirname(getcwd())
This is what I used initially to get the directory path to my /public_html/upload folder. $dirpath will contain
/public_html/upload
The Solution(On server)
$dirpath = realpath(dirname(getcwd()))
Since I’m on a shared hosting environment, the right way of getting move_uploaded_file to work is using this as the destination:
realpath(dirname(getcwd())) returns something like:
/home/cpanelusername/public_html/upload
Make sure the path you are traversing. public_html is not required
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{
echo "success";
}
else
{
echo "fail";
}
Hope this helps
For Ubuntu 14.04 with XAMPP, I also have problem with upload but after I have fixed with sudo chmod -R 777 destination, it works well.
Here is what I did:
Temporary folder to upload in my Ubuntu 14.04 XAMPP is /opt/lampp/temp/. If I want my upload files to /opt/lampp/temp/testupload as destination folder, then I need config bellow.
Go to temp folder
cd /opt/lampp/temp/
Create 'testupload' folder under /opt/lampp/temp/
sudo mkdir testupload
Change permission 777
sudo chmod -R 777 /opt/lampp/temp/testupload/
PHP code
move_uploaded_file($_FILES["file"]["tmp_name"], "/opt/lampp/temp/testupload/" . $_FILES["file"]["name"])
Solution for Windows and ISS:
The IUSR account needed permissions in the destination directory. Not the ISS_IUSR account, just the IUSR account.

PHP Function Rename Permission denied

In server, script create new folder, set chmod to 0777, but then it tryes to move files to that folder i get error: Permission denied.
mkdir("../".$new_1, 0777);
chmod("../".$new_1, 0777);
mkdir("../".$new_1."/".$new_2, 0777);
chmod("../".$new_1."/".$new_2, 0777);
rename("files/".$failai[$i].".jpg", "../".$new_1.'/'.$new_2."/".$failai[$i].".jpg");
Warning: rename(files/new_file.jpg,../112a/112b/Tech_diz_1.jpg) [function.rename]: Permission denied in ..code/Jpg&Html.php on line 82
Any solutions?
you'll need to have read and write permissions in the source folder, too.
only having permissions for the target-folder isn't enough as the file is removed from it's source.
Do you have the write access to the file? If not, make sure you chmod the file to 777 or at least to 644.
Also, check the existence of the file by giving a file_exists() on the file name before you rename. :)
Also, after moving file, you might need to set the permissions using chmod() to make it available for renaming. You can do it this way:
<?php
chmod($uploadedFile, 0755);
?>
You should also have permission to change the file "files/".$failai[$i].".jpg". I would guess that that is going wrong

How to create or edit file content on root

I have one website, not i want when i upload this site on live server it should ask for database username, password, site url..etc. Something like website installer. It is working fine in LOCAL system.
Now my problem is: i need to change RewriteBase in .htaccess file. But this file is located at root of site. So i am getting permission issue to create new file or edit/copy file which is located at root.
Warning: rename(generate.htaccess,.htaccess): Permission denied
Warning: copy(.htaccess): failed to open stream: Permission denied
What should i do. Please help.
Thanks.
To Create/Modify file on root directory you must have to give 0777 permission to your root directory. This is not a good practice.
I suggest create empty .htaccess or other required file with 777 permission initially. Before writing content in that file first check write permission after that copy content from source file (in your case from where you trying to copy).
Hope this may help.

Categories