Unable to open and read file using fopen and fread function - php

The following script bring up a "programming error". I am logged in as the owner of the file so I am not sure why I get the error. I have tried both relative and absolute paths.
Any help very much appreciated.
<?php
$myfile = fopen("test.txt", "r") or
die("Unable to open file!");
echo fread($myfile,filesize("test.txt"));
fclose($myfile);
?>

what means "I am logged in as the owner"? Do you run the script via the command line, or from a webspace?
If you run the script from a webpage, most of the time, the "www-data" user runs the process. Change the permissions for your file "test.txt", that the user which executes the script, e.g: "www-data" has at least read permissions.
It would be interresting to see your current permissions of "test.txt". I would bet that you have an issue with your permissions.

Related

Windows Server: PHP Script unable to open and write file in Windows Server 2012

Is my first time working with Windows server 2012 and I am having a hard time trying to get a PHP script working. I have a PHP script that opens a JSON file, write the file and close it. Everything runs well but when I try to open a file in 'writing' mode an error is generated.
Here is the line where it fails:
$myfile = fopen("master.json", "r+") or die("Unable to open file!");
When I open the files using "r" it opens without any issues, but when I try to open the file using "r+", "w", "w+", "a+", "x+" or "c+" is "unable to open file".
The PHP version on my server is 5.5.112.0.
Is there any permission issue I should be aware of? Something that I am missing?
Any help will be appreciated.
Thank you!
The user running the webserver should need access to the file folder to be able to create the file.
Can you create a file with global write permissions and then write to it?

PHP Permissions Denied fopen

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);
}

Trouble with writing to file with php and my home server

So, I have this simple little php script. It runs and compiles fine and works the way I want it to on the machine that I coded it. I'm running it on a personal home web-server running Debian 6.0.6, 32bit. It's apache with php. And I know for a fact that php is working on the server.
<?php
$hitsfile = "hits.txt"; #name of file
$filehandle = fopen($hitsfile, 'r') or die ("Couldn't read file."); #Opens file, 'hitsfile' to be read.
$hits = fread($filehandle, 5); #reads file to the introduced variable, 'hits'
fclose($filehandle); #closes file
$hits++; #increments the variable that it read.
$filehandle = fopen($hitsfile, 'w') or die ("Couldn't write to file."); #opens file to be read.
fwrite($filehandle, $hits); #writes the hits variable to file.
fclose($filehandle); #closes file.
echo $hits; #outputs the hits variable.
?>
When I access the file from the server, via a web browser, I get the "Couldn't write to file." error. So then, it's opening the file properly, and reading it. And when it opens it to write, it fails. I'm assuming this is some sort of problem with permissions or something. I'm sort of at a loss as to how to solve the issue. Any ideas? Assistance would be greatly appreciated! I've googled for a couple days now, and I can't solve the issue. I'm a php 'noob' and I'm very new to running a linux-based web-server, but hey, you gotta learn somehow. :*l
tried to check the permissions to the file? The Linux file system have a very strict permission system. Write on terminal:
ls -la /path/to/my/file.txt
This would give you your permissions on the left column. Please read this article to be sure, and check if Apache have the "write" permissions to the file. If not, use the chmod command to give Apache access to the file (or the chown command, to change the owner of this file to apache, if the owner of this file have writing permissions).

fopen() not working

I'm want to read a simple string from a text file which is around 3-4 mb but fopen() fails ("can't open file" from die() is called). Here's the code:
clearstatcache();
$fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
$sql = fread($fh,filesize("/my/path/to/file.txt"));
Have you firstly checked to see if the file exists?
if (!file_exists("/my/path/to/file.txt") {
die('File does not exist');
}
clearstatcache();
$fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
$sql = fread($fh,filesize("/my/path/to/file.txt"));
you have to add to your code this line
error_reporting(E_ALL);
and ALWAYS keep this line in ALL your codes
and also this line
ini_set('display_errors',1);
and keep this line only on development server.
while on the production it should be changed to
ini_set('display_errors',0);
ini_set('log_errors',1);
By doing this you will not need Stackoverflow assistance in reading the now obvious error messages.
Change that second line to:
$fh = fopen("/my/path/to/file.txt", "r") or die($php_errormsg);
and see what it outputs as the cause.
Try to output system errors in die or try use try…catch. Also turn on php errors while development. Also check if file is readable before open it.
Most common issues are: file does not exists (or just incorrect path provided?), there is not enough permissions to read this file.
In your FTP file permissions tend to need to be 646 (or -rw-r--rw-), not 777 (always ignore those kind of comments). You want to give a key to someone you trust, setting permissions to 777 is like giving a copy of your key to everyone.
You should check that the specified file directory is inside the working directory. You can do this with 'getcwd'
echo getcwd();
If you still get the error, you should check the file permissions.
ls -l /my/path/to/file.txt
If you get this output "-rw-r--r--" you will see the file is writable for admin only.
To make the file writable for everyone use chmod command:
chmod 666 file.txt
You can check again with "ls -l", the z output should be "-rw-rw-rw-"

fopen unable to open a video file located on server

I am trying to read a video file uploaded on server using fopen and fread in php but fopen returns "unable to open file".
//test.php
<?php
$file=fopen("abc.mov","r") or exit("Unable to open file!");
?>
abc.mov exists in the same folder where test.php is located on the server i.e, at the same hierarchy.
I don't why it isn't able to read the file.
Please help.
This probably isn't a real problem with PHP or your file. This is most likely a problem with the permissions of the file. There are three things you can try here(probably more I don't know of). One, do this somewhere before the fopen in your script:
chmod("abc.mov", 0777);
Then echo fileperms(), just to check(take out after debug):
echo fileperms("abc.mov");
And lastly, before calling fopen, make sure that is_readable and file_exists return true:
if(file_exists("abc.mov") and is_readable("abc.mov")) {
$file = fopen("abc.mov","r") or exit("Unable to open file!");
}
else die("File isn't readable, or maybe doesn't even exist!");
Note: I would be using file_get_contents() and file_put_contents() rather than fopen.
Hope this helps!
Check your file access permissions to make sure you've got access to the file from PHP.

Categories