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);
Related
I'm testing php scripts on my web server.
I'm using a basic fopen / fwrite to write data to file.
The following scripts works perfectly in public_html/ but fails to work in folders above "I'm trying in /test/"
centOS with WHM/cPanel, latest.
<?php
$fp = fopen('lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
This works fine. However:
<?php
$fp = fopen('/test/lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
doesn't write anything to file.
I need php to output data to a ".sh" file, so having it inside public_html would, I think, be a gigantic security flaw.
The goal is to securely output data to a ".sh" via PHP
You should check the test folder permissions.
I'm trying to simply write to a text file with PHP and every time I try it doesn't return an error but just doesn't write. I'm doing...
$fp = fopen('file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
And very time it returns "error". file.txt is definitely in the same directory as the PHP file. I figured PHP couldn't get access to the file. I'm using Windows Server 2008. Does anyone know what the problem could be?
Two things can be happening.
One, consider setting the full path to the file within the directory like this; change /full/path/to/the/file/ to match the actual full path to the file:
$fp = fopen('/full/path/to/the/file/file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
Next, does the file itself have permissions that allow the server itself to access it. Remember, the Apache server will run as another user other than you. So need to make sucre the ownership & permissions match the Apache user.
I like to test my Javascript by running on my local drive to avoid having to upload to my web page each time I make changes. In my HTML file I just use full paths to point to PHP files on my web page. Haven't had a problem until I ran into this. The PHP file below runs fine if I run the HTML file from my web page. But if I run the HTML file from my local drive, only the first part of the PHP file works. The foreach block does not - or it's just not echoing the strings.
I've tried using the full path to the folder rather than just ".txt" below even though it seems that shouldn't be necessary as the PHP is run at the server end.
I tried other methods of reading files in a directory in case the "glob" useage was the problem when running on my local drive.
I even did away with all the commands within the foreach block and just put an echo statement there, but nothing appeared.
I'm guessing an echo statement doesn't work when the PHP file is called from an HTML file on a local drive?
Tried innerHTML with a but that didn't work - or I'm not doing it correctly.
<?php
$MSG=$_GET["myFile"];
$fh = fopen($myFile, "w") or die("can't open file");
$MSG=$_GET["jsVar"];
fwrite($fh, $jsVar);
fclose($fh);
foreach (glob("*.txt") as $filename) {
echo $filename;
echo "<br />";
$file = fopen($filename, "r");
while(!feof($file)){
$line = fgets($file);
echo $line . "<br />";
}
fclose($file);
}
?>
Windows doesn't support PHP, you must install XAMPP to run it through localhost.
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.
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);