file exists but I cannot open it using php - php

I can open and write in file using vim command but i cant open it using PHP
$myFile = "v.txt";
if(!file_exists($myFile)){
print 'File not found';
}else if(!$fh = fopen($myFile, 'w+')){
print 'Can\'t open file \n';}.
else{
print 'Success open file';
}

Do a
ls -l
on the directory that contains the file. Make sure the user that's running your web server has access read rights to the file

Related

Cannot write a file to AWS EC2 instance - PHP

I am trying to create and write a file to ec2 instance. I am getting the error below despite the folder that i am writing possessing all the permissions required.
$handle = fopen("thumbnailFolder/testFile.txt", "r"); // line 4
if($handle != false){
echo 'File created!';
}else{
echo 'File create failed!';
}
The 'thumbnailFolder' has the following permissions:
drwxrwxrwx
Error message:
fopen(thumbnailFolder/testFile.txt): failed to open stream: No such file or directory in /var/www/html/book_aws/my_server/folder/web/thumbnailTest.php on line 4
File create failed!
As the error clearly say. System is failing to open the file which is not there.
$handle = fopen("thumbnailFolder/testFile.txt", "r");
Above code open files in read mode. if there is no files then throws an error.
If you want to open file to write then use try below code, this tries to open file and sets pointer at the begining if file is not there then creates the file in given name. for read and write use w+ instead of w
$handle = fopen("thumbnailFolder/testFile.txt", "w");
There are different modes with respect files. you can check below link for further details.
http://php.net/manual/en/function.fopen.php

file_put_contents append executes successfully but the file is not appended

I am trying to log Parsing errors to a log file. Here's the snippet of code that is used to write to the log file.
if(!array_key_exists(1,$match))
{
$result = file_put_contents("$mapdir/$log_fname","\n$link",FILE_APPEND | LOCK_EX);
if($result===False) echo "Write failed";
else echo "$result bytes written to $mapdir/$log_fname - ";
echo "Link error: $link\n";
return False;
}
This returns-
104 bytes written to configs/test/log - Link error: FR3.SYD - 10GigabitEthernet5/1 - TRDU PUBLICP|10GE|PIPE NETWORKS|18398|LLNW-00004034 [EQX: NETPROV-981]
Which means that the contents were successfully written but when I open the file written to by vi command I see the same file. No content has been added.
Notes-
The file I am writing to exists.
Permissions for all have been set to 777 using chmod -R
I am also writing to several config files in the same location with this script successfully using file_put_contents.
Then why do you think I am facing this problem now with the log file?

Linux Box using PHP writing file to Windows Server Web Share

We have a bunch of linuix and windows servers.
On my windows desktop I can see all the shares.
Using PHP I'm attempting to write a file to a directory on a windows share using the UNC path.
//ServerShare/directory/file.txt
Using fwrite says it successfully wrote the file but the file never exists on the server.
Using opendir function says the directory isn't accessible.
This is the source pretty simple.
$file_name = "\\SERVER2\Share\CPI\data.txt";
if (!$handle = fopen($file_name, 'w')) {
echo "Cannot open file ($file_name)";
exit;
}
// Write $somecontent to our opened file.
$somecontent = "this is a test";
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($file_name)";
fclose($handle);
Any thoughts on what types of permissions need to be set to let a linux box write files to a windows box?
You should be mounting the fileshare to a local directory:
mount -f smbfs //user#server2/Share/CPI/Data.txt /media/share
Then access /media/share/Share/CPI/Data.txt from your PHP script.
PHP needs to authenticate to the share, even if it is public, and using fopen or opendir does not do this.

Unable to write into a file using PHP in sourceforge

I want to HTTP POST data to a PHP file which will in-turn write the data into a file. The script I used is as follows,
<?php
#error_reporting(E_ERROR | E_PARSE);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
fwrite($fp, $msg);
fclose($fp);
echo "Data Written -> $msg";
?>
I am hosting this script file in sourceforge.
I have already just created the empty file dinesh.txt and placed the file in the same directory as that of the script file. But unfortunately its not written in the file.
What is the reason ?
Thanks in advance
Check that $_POST["msgarea"] is not empty and the file is writeable for the user who tries to write it.
And check the logs for errors of course.
<?php
error_reporting(E_ALL);
var_dump($_POST["msgarea"]);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
$result = fwrite($fp, $msg);
fclose($fp);
if ($result) {
echo "Data Written -> $result";
} else {
echo "Error";
}
I think you are writing in read-only webspace, check file permissions and path
From: http://sourceforge.net/apps/trac/sourceforge/ticket/2772
In generally it is not allowed to write to folders and files that reside in the project's web space. If you need to write to the file system you should use the folder named persistent that is on the same level as htdocs.
I use on my computer:
$ mkdir persistent
$ chmod 0777 persistent
$ scp -r persistent my_account#web.sourceforge.net:/home/project-web/my_project/
And set in PHP:
$fp = fopen("../persistent/dinesh.txt", 'w');
And it works!
Edit:
You can get access for SSH console for 4 hours (https://sourceforge.net/apps/trac/sourceforge/wiki/Shell%20service). And you can go to web directory of your project, make dirs, set privileges, remove files etc. Midnight Commander is available.
you can, check that folder permission, do they have 777 permission
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
fclose($fh);

how to create a txtfile with 777 permission

i want to create a .txt file with the 777 permission for file creation i am using the following code
if(file_exists($myFile) == true)
{
$err = "File Already Exist in The usrlogactity. Try Another Username";
}
else
{
$fh = fopen($myFile, 'w') or die("can't open file");
}
$file = "usrlogactity/$myFile";
$ftp_server="02.79.103.130";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, "use1234", "pass123");
// try to chmod $file to 644
if (ftp_chmod($conn_id, 0777, $file) !== false)
{
//echo "$file chmoded successfully to 777\n";
}
else
{
//echo "could not chmod $file\n";
}
// close the connection
ftp_close($conn_id);
if i execute this code in server it create the file with the 644 permission ,
but gives error. how to i create the .txt file with the 777 permissiom please guide me
The error is
No such file or directory in usrlogactity/$myFile on line 13
To change local file permissions you have to use PHP function chmod(), not using ftp for this
Doing it via FTP you're trying to access this file with ftp user, not www user, and, therefore, no success.
And, you see, how it's important to post your code, not to tell about it? ;-)
it depends on the server if your scripts are allowed to and why do you need 777 for a text file you don't execute a text file you read its 644 is all you need,
but permissions should not be changed by a script different configurations leads to completely different results,
Some servers are idiots and run Apache as root, which mean php writes a file that root owns,
Some use suExec and then your login to your account owns the file
others use Apache user to control your file
so your script could work on one and not on another if you moved the site due to the file becoming unreadable
Allso check safemode on your php install

Categories