Permission denied in creating and writing a file from PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dennislee85
Forum Newbie
Posts: 15
Joined: Mon Mar 12, 2007 9:16 pm

Permission denied in creating and writing a file from PHP

Post by dennislee85 »

Hi,

I have tried a simple PHP code to do logging in Linux + Apache2 + PHP 5. But i got error (warning) as below

Code: Select all

[error] [client 203.125.51.30] PHP Warning:  error_log(/var/log/mobile365/incoming/test.log) [<a href='function.error-log'>function.error-log</a>]: failed to open stream: Permission denied in /srv/www/htdocs/gateway/sms_incoming.php on line 7
Here is my code:

Code: Select all

<?php

        $s = "Hello there";

        $logfile = "/var/log/mobile365/incoming/test.log";

        error_log($s,3,$logfile);
        echo 'done';

?>
Please advise.

Thanks
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

does the file have 777 permissions (read and write for everyone) PHP requires it
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Double Posting

Post by feyd »

tsk, tsk.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:3. Do not make multiple, identical posts. This is viewed as spam and will be deleted.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Permission denied in creating and writing a file from PH

Post by tecktalkcm0391 »

Code: Select all

<?php
        $s = "Hello there";
        $location = "/var/log/mobile365/incoming";
        chmod($location, 0777); //edited read why below
        $file = "test.log";
        $logfile = $location.'/'.$test.log;

        error_log($s,3,$logfile);
        echo 'done';
?>
Edited because PHP Manual says:
PHP Manual wrote:To ensure the expected operation, you need to prefix mode with a zero (0):


You could try that
dennislee85
Forum Newbie
Posts: 15
Joined: Mon Mar 12, 2007 9:16 pm

Post by dennislee85 »

Hi,

Thats helped. Thanks for the solution and sorry for the multiple posts. :cry:

Anyway, this solution had give the permission to everyone to access the files. May I know is there any way I can assign an user and user group for php so i can chown the "/var/log/mobile365/incoming" folder which will only used by php only.

Many Thanks.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

dennislee85 wrote:Hi,

Thats helped. Thanks for the solution and sorry for the multiple posts. :cry:

Anyway, this solution had give the permission to everyone to access the files. May I know is there any way I can assign an user and user group for php so i can chown the "/var/log/mobile365/incoming" folder which will only used by php only.

Many Thanks.
I don't think that is possible, but if your server is /websitefiles/var/... for the log file and /websitefiles/public_html/.... for the actual files that are "live" on the internet, then there is no reason to do that because unless you have a "live" file that allows you to read the log, and internet user can't get to it.. unless they exploit you somehow on another page...freakn hackers...grrrr..
dennislee85
Forum Newbie
Posts: 15
Joined: Mon Mar 12, 2007 9:16 pm

Post by dennislee85 »

Hi,

Ok, i got it. Thanks a lot. :)
dennislee85
Forum Newbie
Posts: 15
Joined: Mon Mar 12, 2007 9:16 pm

Post by dennislee85 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I am trying to retrieve data from xml file POST from remote server. I am using SimpleXMLElement to do this. But when i try to use the SimpleXMLElement->getName() function, it gave me this error:

Code: Select all

[Thu Aug 23 13:46:34 2007] [error] [client 62.80.122.178] PHP Fatal error:  Call to undefined method SimpleXMLElement::getName() in /srv/www/htdocs/gateway/sms_incoming.php on line 10
My codes:

Code: Select all

<?php

	$files = $_POST['XmlMsg'];	
	$logfile = "/var/log/mobile365/incoming/test.log";
	$str1 = "Start \n";
	if($files){

		
		$xml = new SimpleXMLElement($files);
		$str1 = $xml->getName();

	}else{

		$str1 = $str1." file empty \n";

	}
		
	error_log($str1,3,$logfile);

?>
Here is my XML file:

Code: Select all

<?xml version="1.0" ?>
<SMS_MO>
	<MSISDN>+6593880590</MSISDN>
	<ORIGINATING_ADDRESS>+61416905690</ORIGINATING_ADDRESS>
	<MESSAGE>HTTP</MESSAGE>
	<PARAMETERS>
		<OPERATORID>43</OPERATORID>
		<ACCOUNTID>20745</ACCOUNTID>
		<MESSAGEID>541603206</MESSAGEID>
		<OPERATOR_INFORMATION>
			<OPERATOR_STANDARD>GSM</OPERATOR_STANDARD>
			<OPERATOR_CODE>
				<MCC>505</MCC>
				<MNC>03</MNC>
			</OPERATOR_CODE>
		</OPERATOR_INFORMATION>
		<DCS>7b</DCS>
		<CLASS>1</CLASS>
		<RECEIVED_SERVICENUMBER>61416905690</RECEIVED_SERVICENUMBER>
		<KEYWORD>HTTP</KEYWORD>
		<RECEIVEDTIME>
			<DATE>Thu, 23 Aug 2007</DATE>
			<TIME>06:34:16</TIME>
		</RECEIVEDTIME>
	</PARAMETERS>
</SMS_MO>
Please advise


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply