?
Solved

PHP File creation chmod 644 ?

Posted on 2011-10-30
11
Medium Priority
?
458 Views
Last Modified: 2012-05-12
Hi all,

I am using this code:

<?php
$filename = 'test.php';
$Content = "Add this to the file\r\n";
 
echo "open";
$handle = fopen($filename, 'x+');
echo " write";
fwrite($handle, $Content);
echo " close";
fclose($handle);

?>

Open in new window


It works well but the file it creates has chmod 644 and I cannot even change it :o/

Any ideas why?

0
Comment
Question by:error77
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37052722
Hello error77,

As far as I know 644 is default permission that used by php.

You can use following to give permission you want

chmod("/somedir/somefile", 0777);

Hope this will help

Thank You

Amar.
0
 

Author Comment

by:error77
ID: 37052762
I'm getting:

Warning: chmod() [function.chmod]: Operation not permitted in ...

0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37052767
Hello error77,

Can you please paste whole error here?

Thank you.

Amar.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:error77
ID: 37052827
Sure, there is the code:

<?php

$source = $_SERVER['DOCUMENT_ROOT']."/sourcefolder/index.php";
$filename = $_SERVER['DOCUMENT_ROOT']."/targetfolder/index.php";

$Content = file_get_contents($source, true);

 
echo "open";
$handle = fopen($filename, 'x+');
echo " write";
fwrite($handle, $Content);
echo " close";
fclose($handle);   

chmod($_SERVER['DOCUMENT_ROOT']."/targetfolder/index.php", 0777);

?>

Open in new window


Hope this helps
0
 

Author Comment

by:error77
ID: 37052831
BTW...that was the whole error except for the path...the path is right btw.

0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37052910
Hello error77,

It seems strange but might be one of the following reasons

1. targetfolder might not have permission 777. You might need to set its permission to 777 before you set permission of file

Try to run following first
chmod($_SERVER['DOCUMENT_ROOT']."/targetfolder", 0777);

2. user who is trying to change permission does not have proper permission on that folder.

Hope this will help.

Thank You.

Amar
0
 

Author Comment

by:error77
ID: 37052919
OK, I've set the source file to 777 and then also added the mod ... and as soon as I run the code the created file is 644 :o(

Any other ideas?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 37053044
chmod may be disallowed on your server or your PHP server may have SAFE MODE enabled which restricts what you can do with chmod. Run

<?php
phpinfo();
?>

and scan for disabled_function

"When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits."

http://www.php.net/chmod

and

http://uk3.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode
0
 

Author Comment

by:error77
ID: 37053084
I can't use phpinfo(); .. it's disallowed on the host :o(
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37053307
I can't use phpinfo(); .. it's disallowed on the host :o(

Get a new host.  Your scripts are almost certainly running in safe mode or under Suhosin.  That will create a devil of a time for you.  I use and recommend ChiHost.com.  I have also had good experiences with LiquidWeb and Hostgator.  I have had very poor support experiences with GoDaddy.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37054186
Thanks for the points, and if you choose ChiHost, please feel free to tell them I sent you (ask for Nick Gilbert when you contact them).  I'm not paid to endorse them or anything; I just use their services and find that whenever I have needed support they have been ready to listen and help.  On the rare occasion when I have had troubles, they have been great about helping me fix things.  And almost all of the time there are no troubles at all - just a stable, reliable hosting company.  BTW, I have never been there.  They are in Chicago and I am in Washington, DC.  All the best, ~Ray
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question