I'm creating a tool like Pastebin for train myself in PHP (I'm a beginner yet).
Everyone without log in/sign up can create .txt file and share it easily.
Anyway, I'm facing a problem:
How can a guest edit the .txt file when he created it?
<?php
$titleFile = $_POST['title'];
$textFile = $_POST['text'];
$newFile = fopen($titleFile . ".txt", "w") or die("Unable to open file!");
fwrite($newFile, $textFile);
$url = $titleFile . ".txt";
header("Location: $url");
?>
http://mattewdev.altervista.org/
Related
I'm making a simple setup form where you are asked to enter your database credentials which are stored in another PHP file but when the user submits it the contents in the database credentials file are deleted and the file is just empty. I have tried debugging my code but still can't figure out what is causing the problem.
My database credentials file:
<?php
define("DATABASE_HOST", "{DB_HOST}");
define("DATABASE_USER", "{DB_USER}");
define("DATABASE_PASSWORD", "{DB_PASSWORD}");
define("DATABASE_DATABASE", "{DB_NAME}");
My code:
$databasehost = $_POST['databasehost'];
$databaseuser = $_POST['databaseuser'];
$databasepassword = $_POST['databasepassword'];
$databasename = $_POST['databasename'];
$searchF = array('{DB_HOST}','{DB_USER}','{DB_PASSWORD}','{DB_NAME}');
$replaceW = array($databasehost, $databaseuser, $databasepassword, $databasename);
$fh = fopen("../static/database.php", 'w');
$file = file_get_contents('../static/database.php');
$file = str_replace($searchF, $replaceW, $file);
fwrite($fh, $file);
fclose($fh, $file);
Thanks,
Nimetu.
You read the file with the call
$file = file_get_contents('../static/database.php');
after you have opened the file using w. Opening it for write will automatically blank the file. So change the order to
$file = file_get_contents('../static/database.php');
$fh = fopen("../static/database.php", 'w');
When a user clicks submit I am trying to create a file.
Centos 7
php 7.2
I have tried to modify the code a couple different ways also tried file_put_contents does not seem to work.
Code:
index.php
<form action="sendmail.php" method="post">
<input type="text" placeholder="fname" name="fname">
<button type="submit">submit</button>
</form>
sendmail.php:
<?php
$fname = $_POST["fname"];
echo "bef: " . $fname;
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
echo "aft: " . $fname;
$txt = $fname;
fwrite($myfile, $txt);
fclose($myfile);
?>
Directory to create file
[root#webserver webdir]# ll -d /signupemails/
drwxrwxrwx. 2 apache apache 51 Aug 25 20:41 /signupemails/
If i change sendmail.php and hardcode the $fname and run php sendmail.php it will create the file fine
Creating it from the browser I get "Unable to open file!"
Your code should work as is, i just tested it and it seemed to work fine.
Is selinux disabled?
Try changing this line:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
PHP doesn't have the same logical "or" semantics as JavaScript. That expression returns a boolean, so $myfile will equal true instead of the file handle.
Write it this way:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w");
if (!$myfile) die("Unable to open file!");
I suggest do with js
For example
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");
Hi I am using fopen php function to maintain log. I can write to the file within my own computer. But how to make PHP able to write file over the local network. Other computer in the network access my computer PHP application through IP but fails with permission when writing to the log file. My code is:
function hotellog($txt) {
date_default_timezone_set("Asia/Kathmandu");
$newfile = '\\localhost\gandaki/wp-content/hotel_log.json';
if (file_exists($newfile)) {
$myfile = fopen($newfile, "a") or die("Unable to open file!");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
} else {
$myfile = fopen($newfile, 'w') or die("Can't create file");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
Other computers over networks get the error as 'permission denied'.
PHP can not (well, should not) be able to write files over the network onto your computer under normal circumstances. You would be better off writing the log file on your local computer and then scripting a file transfer via sftp, or something similar, from your local computer to the remote one.
I recently installed Apache, PHP and started working on a small project.
I have the following code.
<?php
$tim=time();
$ip=$_SERVER['REMOTE_ADDR'];
$ipadd=$tim."IPaddress".$ip;
$fp="user_log.txt";// file address
$myfilea = fopen($fp,"a");//open file
fwrite($myfilea,$ipadd.PHP_EOL);//add data to file
echo fread($myfilea,filesize($fp));//read file
fclose($myfilea);//close file
?>
Here is what I can do... I can either use "a" mode to add text or I can use "r" mode to read text. I cant do both. I tried using "a+","r+","ar" etc.
Did I miss something during my setup ???
I am running this on windows 8.1.
Thanks for your help.
You need to rewind the file pointer.
$tim = time();
$ip = $_SERVER['REMOTE_ADDR'];
$ipadd = $tim.'IPaddress'.$ip;
// file address
$fp = 'user_log.txt';
//open file
$myfilea = fopen($fp, 'a+');
//add data to file
fwrite($myfilea, $ipadd.PHP_EOL);
// your file pointer is at the end of the file now
// so rewind before you read
rewind($myfilea);
//read file
echo fread($myfilea, filesize($fp));
//close file
fclose($myfilea);
Try this code, use file_put_contents
file_put_contents = Write a string to a file
$fp="user_log.txt";
$tim=time();
$ip=$_SERVER['REMOTE_ADDR'];
$ipadd=$tim."IPaddress".$ip;
$myfile = file_put_contents($fp, $ipadd.PHP_EOL , FILE_APPEND | LOCK_EX);
And for your code try this, it will check able to open file or not
fopen("logs.txt", "a") or die("Unable to open file!");
how do i open/view for editing an uploaded file in php?
i have tried this but it doesn't open the file.
$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
i've also tried this but it wont work.
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);
what i have in mind is like when you want to view an uploaded resume,documents or any other ms office files like .docx,.xls,.pptx and be able to edit them, save and close the said file.
edit: latest tried code...
<?php
// Connects to your Database
include "configdb.php";
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
//Echo "<img src=localhost/uploadfile/images".$info['photo'] ."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Email:</b> ".$info['email'] . " <br>";
Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
//$file=fopen("uploadfile/images/".$info['photo'],"r+");
$file=fopen("Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt","r") or exit("unable to open file");;
}
?>
i am getting the error:
Warning: fopen(Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/uploadfile/view.php on line 17
unable to open file
the file is in that folder, i don't know it wont find it.
It might be either:
A permissions issue on the server. If it's a linux machine, try chmod 754 Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt (you may need root).
An issue with apache (or whatever webserver you might be using). Make sure you've defined a directory entry in the config file for that site. Documentation here.
Although, if I recall properly, an odt file will just be binary data rather than the text information. That might be what you're looking for, I don't know. If you just want to read the actual text, and you aren't interested in using some library to extract it, you need to save it as plain text. If you're actually trying to edit these files in the browser, you're gonna need a lot more than just fopen.
For text based files you can use file_get_contents and file_put_contents
However, ODT files are zip compressed XML files so you need to decompress them first:
$zip = new ZipArchive;
$res = $zip->open('Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt');
if ($res === TRUE) {
$zip->extractTo('/tmp/myOdt.xml');
$zip->close();
}
$data = file_get_contents('/tmp/myOdt.xml');
$data .= 'add this to the end';
file_put_contents('path/to/file.txt', $data);
However, it seems your problem regards the actual path of the file. Try using a relative path instead: "images/file.odt"