Absolute path (www.example.com) in file_put_contents [duplicate] - php

Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = #file_get_contents($url);
$hit_count++;
#file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
#file_get_contents is working fine and the header location change to the downloaded file also works, but either the hit_count increase or #file_put_contents isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."

You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.

try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>

Related

failed to open stream, permission denied even tho permissions are set

I'm working with the bing-ads api and try to download a report to a local folder.
I already found out the user that is executing php using
<?php echo exec('whoami'); ?>
which resulted in "dl-dominikl-pc\dolo"
I gave that user full control of the "reports" folder.
The code-part that is failing is:
fopen($downloadPath, 'wb');
with $downloadPath being "C:\\xampp\\htdocs\\crm\\bingAds\\examples\\reports"
Thanks to Saitama for reminiding me to check my path which didn't point to a file!

PHP: Why is chmod() setting my file/folder to 555 or 444?

I created a web application with a file browser. I'm trying to add a functionality where the user can change the chmod/permissions via an ajax request which is handled via PHP on the back-end.
(Side Note: I'm running my local with WAMP)
So initially, I'm reading the permissions with this
substr(sprintf('%o', fileperms($relativePath)), -4)
to get this format (0777, 0644, etc), if not it returns something like 32726. This info is used to be displayed in the UI for the user to know whats current.
However, when I run the script, I set it to 0777 and it seems to run fine. But then when I read the file again, it returns 0555 or 0444. Anyone know what I'm missing?
Does your web server own the files it is trying to change the permissions on? You can check whether chmod ran correctly or failed by testing its return value. It will return FALSE if the webserver did not have permissions. For more information you can read: http://php.net/manual/en/function.chmod.php
<?php
$is_success = chmod("myfile.pdf", 777);
if($is_sucess) {
echo "success<br />\n";
}
I realized this was not an issue, but rather the chmod command does not work properly on a windows/apache setup.

PHP Write a variable to a txt file

I am trying to get a variable from the query string and write it to a text file.
I have tried like this:
<?php
$content=( $_GET['var'] );
echo $content;
$file = fopen("etlLOG.txt","w+");
echo fwrite($file,$content);
fclose($file);
?>
I get the following errors:
Warning: fopen(etlLOG.txt) [function.fopen]: failed to open stream: Permission denied in E:\Users\george\listener.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 10
You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that etlLOG.txt is located in. Then type:
sudo chmod 777 etlLOG.txt
You may be prompted for a password. Also, it could be the directories that don't allow full access.
OR
PHP
chmod : Attempts to change the mode of the specified file to that given in mode.
chmod
Well it appears that the web server does not have access to that file. So used ftp fopen provided the correct credentials and paths and finally i did managed to access and edit the file
If you are running WAMP on windows
1) login as an administrator
2) Install wamp
3) Download subinacl.exe from microsoft:
4) grant permissions to the regular user account to manage the WAMP services: (subinacl.exe is in C:\Program Files (x86)\Windows Resource Kits\Tools)
subinacl /SERVICE \MachineName\wampapache /GRANT=domainname.com\username=F
subinacl /SERVICE \MachineName\wampmysql /GRANT=domainname.com\username=F
5) logout and log back in as the user. They should now be able to launch the WAMP taskbar application and control the WAMP service.
My guess is that PHP is trying to create the file at 'C:\WINDOWS', so the solution wouldn't be to set permissions there.
I would suggest you use a specific location, example: create a folder at the same location as your php script and use it to create the files:
$filePath = dirname(_FILE_) . "/temp_vars/etILOG.txt";
$file = fopen($filePath ,"w+");
Kind regards,
The code you are running is trying to create the etlLOG.txt in the same folder as your PHP script. You need to give Full Permission to your IUSER_ account on the E:\Users\george folder.
If you do not know how you can browse the following links
http://technet.microsoft.com/en-us/library/bb727008.aspx
http://www.wikihow.com/Change-File-Permissions-on-Windows-7
How to set 777 permission on a particular folder?

PHP SVN Checkout via HTTPS

I want to create an SVN checkout PHP script. All you need is to call a function and pass two parameters: the SVN URL and the output path.
My problem is, that our SVN server can only be accessed via https. But through https, the function doesn't work. Normally the function should return a boolean, but I just get nothing. My first thought was, that I have no permission to write into the output path folder, but I changed the permission to 777 (temporarily). Still doesn't work. I also tried to get some files from another SVN trunk. Behold, this is working. I get the files. Any idea how to get this to work?
Aah, and yes, I set the svn trunk permission to read-write for everyone.
Here's is my code:
<?php
$result = svn_checkout('https://{LINK_TO_SVN_TRUNK}', dirname(__FILE__) . '/tmp');
echo "Result: ".$result;
?>
This is what I have used successfully in the past:
svn_auth_set_parameter(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true);
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, "username");
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, "password");
$changeLog = svn_log($path, $start_revision, $end_revision);
Please confirm if the extension is enabled. php.ini should consist of extension=svn.so or php.d folder should consist svn.ini with the line extension=svn.so. You can check for extension in phpinfo();

PHP File_put_contents not working

Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = #file_get_contents($url);
$hit_count++;
#file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
#file_get_contents is working fine and the header location change to the downloaded file also works, but either the hit_count increase or #file_put_contents isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."
You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.
try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>

Categories