Unable to write into a file using PHP in sourceforge - php

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

Related

cannot edit any php files using specific functions

I cannot update any txt files using php. When I write a simple code like the following:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
Both files do exist in the folder. I just cannot see any updates on bob.txt.
Is this a permission error in windows? It works fine on my laptop at home. I also cannot change the php files on my website, using filezilla.
It may very well be a file permissions issue.
Try your code using a direct pointer to the file instead of a path to it, using the following code:
I added a chmod directive. See the comments above chmod ($file, 0644);
Tested succesfully on my hosted WWW website:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
Perhaps you have to set the permission from bob.txt to 0777 (or something else). In FileZilla it is very easy, because you can just check the permissions you want.

not writing in text file on server side using php

I'm trying to put the results from the database into a text file after fetching it, and if the text file doesnt exist it creates one and put the data into it. after that i showed the the data by extracting from those txt files in another page. the problem is it is working fine in my local xampp server. but i dont know why its not working in my aws account. the result page shows blank in aws elastic ip.but its connecting to the database and if there is no results found it also shows the message. so its just not putting the datas into the txt file nt even creating it. i tried by creating the txt file manually. still not working. but its working fine in my local server. Here is the code i used-
$wineFile ="WineName.txt";
file_put_contents($wineFile, $W);
From your comments , seems like there is not enough permission. You need to CHMOD 777 on the file.
So make use of CHMOD command on PHP
Something like this
$file ="WineName.txt";
chmod($file, 0777);
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
try
$wineFile = "WineName.txt";
$fh = fopen($wineFile, 'w') or die("can't open file");
$stringData = "Tasty Wine\n";
fwrite($fh, $stringData);
fclose($fh);

fopen won't open file on Ubuntu server

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.

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-"

writing to log file causes error 704

Does anyone know what this error means
FATAL:
Autorisation no longer valid.704
It happens when I try to write to this file, but the permissions are set to 755 and 0644
The temp folder is in the rootfolder of this subdomain.
if ($handle = fopen( 'temp/mylog.log'"a+") )
{
if( !fwrite( $handle, $json ) )
{
throw new Exception("can't write to ...");
}
fclose( $handle );
}
thanks, Richard
Does the user who run that script own that folder/file?
do a list
# ls -l /rootfolder/temp/
to get the user who has privileges to modify the file, I suppose it is root
do from your shell the following to allow your user to access the file (change user with your username)
# chown user /rootfolder/temp/mylog.log
also use the full path in fopen.
UPDATE:
use this simple steps to write file, if you get errors then it may be something related to permissions
$myFile = "/home/woonbel/public_html/tsa.nl/temp/tsa.log";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Some of your text...bla bla\n";
fwrite($fh, $stringData);
fclose($fh);

Categories