Helping a friend with an issue he's having. His site is using a fopen function to open a file and save the changes to it but the fopen is failing with a "Permission Denied" error. The file is on a remote server and permissions seem to be correct.
The code he is using is...
if (isset($_POST['save'])) {
$filepath = "string.txt";
if (file_exists($filepath)) {
$file = fopen($filepath, "w+");
fwrite($file, $_POST['save']);
fclose($file);
}
}
I used VolkerK's code here php fopen returns false but file is readable/writable to ascertain the read/write permissions and get the following.
PHP Version: 5.4.9
Uname: Windows NT
{File} exists
{File} is readable
{File} is writable
Last error: array(4){["type']=>int(2)["message"]=>string(131 "fopen({file}):failed to open stream: Permission denied.
At first I thought it was a file permissions problem but I think if the file is being seen by PHP as writable this should not be the issue, do I read that correctly?
Tried FTP and got there. Had to properly set context options for FTP connection though.
Thanks for pointing me in the right direction.
if (isset($_POST['save'])) {
//Setup FTP connection
$ftp = "ftp://user:password#example.com/filepath/filename.txt";
//Set FTP Options
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
//Open file and write changes
if (file_exists($ftp)) {
$file = fopen($ftp, "w", 0 , $stream_context);
fwrite($file, $_POST['save']);
fclose($file);
}
}
Related
Good evening,
I am fairly new to PHP, right now I am getting permissions errors. I am using a Windows 10 enviroment with PHP, and composer installed. I am trying to pull files from an array of url's, but fopen gives me a permission error. Here is the code:
$fp = fopen($result['url'], 'r');
$out= fopen($filePath, 'w');
while ($buf = fread($fp, 5e+9)) {
fwrite($out, $buf);
usleep(500);
}
fclose($out);
fclose($fp);
$finished = true;
where $filePath is defined as 'D:\PST'. Is there any massive errors I am missing? D:\PST has full control permissions for the user group 'Everyone'. I am running the powershell window that I prompt PHP from in Administrator. I am using an Administrator account.
Thanks,
Please follow link http://forums.iis.net/t/1167645.aspx
And check with this link http://www.addictivetips.com/windows-tips/windows-7-access-denied-permission-ownership/
I want to open a ZIP file by passing a remote URL (http://www.example.com/file.zip or http://localhost/wordpress/wp-content/uploads/file.zip) instead of a file location (C:\wamp\www\wordpress\wp-content\uploads\file.zip)
This constructor works fine for a file location but not for a remote url of a file. How does one open a file using a remote URL for this scenario?
public function __construct($file = false)
{
if ($file && is_file($file)) {
//$file="C:\wamp\www\wordpress\wp-content\uploads\file.zip" here
$this->open($file);
$this->fileName = basename($this->filePath = $file);
} else {
throw new \Exception($file . " not a regular file");
}
}
The safest way is to
download the file
This is super easy if allow_url_fopen is enabled: file_get_contents() accepts remote URLs. If that's not enabled, use cURL or a Wordpress HTTP helper to download it.
save it locally
Also super easy, with file_put_contents(). The /tmp folder is probably writable for you. On Windows, I don't know where the tmp folder lives.
open it like any other
As you would a local ZIP archive, with ZipArchive::open() or your nameless class
just use php fopen function
http://php.net/manual/en/function.fopen.php
$handle = fopen("http://www.example.com/", "r");
I have used this to get the contents of a webpage but not a zip file - not sure if that will work but it did well for me. $contents definitely worked for text.
// For PHP 5 and up
$handle = fopen("https://www.thesiteyouwant.com/the_target_file.ext", "r");
$contents = stream_get_contents($handle);
http://php.net/manual/en/function.stream-get-contents.php
I ran into a really bizarre problem. I am trying to perform writing to file using fopen().
This is what I tried in writetofile.php:
$fw = fopen('/test.txt', 'w');
fwrite($fw, 'hello world' . "\r\n");
fclose($fw);
This is the error I keep getting:
Warning: fopen(/test.txt):
failed to open stream: Permission denied in C:\inetpub\wwwroot\writetofile.php on line 41
Warning: fwrite() expects parameter 1 to be resource, boolean given...
I am 100% sure I have permissions to the server. I am the Administrator. Furthermore, I temporarily gave full permissions to everyone. I even tried running the php script locally, directly from the server using localhost. I am not using apache, I am using IIS. I tried restarting IIS after modifying permissions. I am not running php in safe mode.
Any idea on what might be causing this issue?
/test.txt would be a file in the ROOT directory of your filesystem, where user accounts generally do NOT have write privileges (unless you're running this code as root). This is especially true of PHP running under the webserver's user account.
You probably want just test.txt (no leading slash)` which will try to put the file into the script's "current working directory" - usually the same directory the script itself is in.
1- when you rollout website, delete all logs folder names
2- inside the code create folder name as below and create the logs insides
3- write at top of file. (during init the web)
$ClientUserName = gethostbyaddr($_SERVER['REMOTE_ADDR']);
function Data_Log($dataline)
{
global $ClientUserName;
$dir = 'UserInputLog' ;
$fileName = $ClientUserName. '_ServerWebLog.txt';
if(is_dir($dir) === false)
mkdir($dir);
$fileName = $dir. '\\'.$fileName;
$myfile = fopen($fileName, "a") or die("Unable to open file!");
fwrite($myfile, "$dataline\r\n");
fclose($myfile);
}
I have a zip file into a directory like this:
drwxr-xr-x 2 salome salome 4096 Dec 16 17:41 staff.zip
When I unzip the file with ZipArchive class, all the upzipped files are owner by nobody user. Is there a way to avoid this owner change?
I am able to use ftp if it is required (only as salome user).
This script eventually will be shared at multiple hosts, so the idea is keep it as generic as possible.
You might consider extending the zipArchive class and override the extractTo method to also perform a chown() on the files in the directory.
Based on the use case you discussed in the comments, you also might want to consider the use of the Phar archive format. php.net/manual/en/intro.phar.php
Phar's would allow your module submitters to submit a file file of executable PHP code that you would not need to extract at all.
Ok, I have resolved the problem of nobody user. I will try to explain all my workaround.
#Mike Brant's answer
Mike suggest to me make use of chown() function overriding extractTo() method. Well, before to willing with it, I tested the chown() function standalone constantly it printed the error:
failed to create stream: Permission denied in ...
Looks like chown will not work for the major shared hostings
FTP functions
So, keeping going I though that FTP functions I made a script that works fine, at least for now xD. This is a resume what the script does for one zipped file:
Create a temp file using tmpfile().
Using ftp_fput() to put the temp file in the current directory with the zipped file.
Give write permissions using ftp_site and CHMOD 0777.
Read the zipped file content with $content = $zip->getFromName('zipped-file.txt');.
Put the content to the new file using fputs($fp, $content);.
Close connections
The code below illustrates the complete process
$zip = new ZipArchive;
$ftp_path_to_unzip = '/public_html/ejemplos/php/ftp/upload/';
$local_path_to_unzip = '/home/user/public_html/ejemplos/php/ftp/upload/';
if ($zip->open('test.zip') == TRUE) {
//connect to the ftp server
$conn_id = ftp_connect('ftp.example.com');
$login_result = ftp_login($conn_id, 'user', 'password');
//if the connection is ok, then...
if ($login_result) {
//iterate each zipped file
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
//create a "local" temp file in order to put it "remotely" in the same machine
$temp = tmpfile();
//create the new file with the same name from the extracted file in question
ftp_fput($conn_id, $ftp_path_to_unzip . $filename, $temp, FTP_ASCII);
//set write permissions, eventually we will put its content
ftp_site($conn_id, "CHMOD 0777 " . $ftp_path_to_unzip . $filename);
//open the new file that we have created
$fp = fopen($local_path_to_unzip . $filename, 'w');
//put the content from zipped file
$content = $zip->getFromName($filename);
fputs($fp, $content);
//close the file
fclose($fp);
//now only the owner can write the file
ftp_site($conn_id, "CHMOD 0644 " . $ftp_path_to_unzip . $filename);
}
}
// close the connection and the file handler
ftp_close($conn_id);
//close the zip file
$zip->close();
}
This is the firt step to start a more complex customization, because the code above is not able to know if the zipped file is a "directory" or "file".
The following function is normally able to open a file on xampp with no problems
/* converts string to xml*/
public function stringToXMLFile($string){
$file = __DIR__."/xml/feed.xml";
$fh = fopen($file, 'w+') or die("can't open file: ".$file);
fwrite($fh, $string);
fclose($fh);
}
But since uploading all my files onto an Ubuntu server I can not get fopen() to open and edit any files, is there something that I have to do on a newly installed Ubuntu Apache server which will give permission to perform such tasks?
After having discussion in the chat, the problems were:
Permissions on /var/www/xml directory.
Permissions on /var/www/xml/feed.xml file.
After adjusting them to proper values, problem was solved.