Can someone explain me why when i POST RAW Data for example "test.txt" in the below script
<?php
echo file_get_contents("php://input");
?>
it only prints the text "test.txt" instead of the file contents of that file?
Thank you
Your code reads the contents of the raw POST data and echoes it back.
Whereas what you want is this:
// retrieve the requested filename
$fileName = file_get_contents("php://input");
// echo the contents of the requested file
echo file_get_contents($fileName);
Depending on what you're trying to, you may wish to sanitize the $fileName input (not shown: too broad) and restrict access to a specific local directory:
$path = $myLocalDirectory . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($path) {
echo file_get_conents($path);
}
Try like this ..
$input = "abc.txt";
echo file_get_contents($input);
It gives the content of the text file abc.txt
Related
I have a task to do in which i have to list the directories with it's files which i did, but i don't understand how to delete file or edit specific file in the directories any help will be appreciated Thanks.
<?php
error_reporting(0);
if(isset($_GET['dir']))
{
// /$path = 'E:\xampp\\'.$_GET['dir'];
$path = $_GET['dir'];
}
else
{
$path = 'E:\xampp\\';
}
if(is_dir($path))
{
$arrDir = scandir($path);
echo "<ul>";
foreach ($arrDir as $key => $value)
{
echo "<a href='http://localhost/vishrut/FileUpload/filelist.php?
dir=".$path.'/'.$value."'>".$value.'</a><br>';
}
echo "</ul>";
}
else
{
echo "<textarea>";
echo file_get_contents($path);
echo "</textarea>"."<br>";
}
?>
There are lots of PHP's functions to handle files: https://www.php.net/manual/en/ref.filesystem.php
For your needs see these:
file_get_contents to read the entire file contents
file_put_contents to write the content in a file
unlink to delete a file
So, the steps to modify a file may be:
get the complete contents with file_get_contents:
$contents = file_get_contents($filePath);
apply your edits to the $contents content:
$newContents = ...
overwrite the file content:
file_put_contents($filePath, $newContents);
To delete a file is simple:
unlink($filePath);
It's important to note that your code is subjected to injection because you don't check the user data passed with $_GET.
If your script will be used only by you it's ok, instead you must check all user input: the first rule of Web programming is NEVER TRUST YOUR USERS! Also trusted users may write wrong characters in the url and that may have unexpected results (e.g. delete the wrong file!)
Read https://www.php.net/manual/en/mongodb.security.script_injection.php
I'm using the following code to display the name of a CSV file before the contents of the CSV file are displayed on a PHP page. It works well and displays the name of the file as it should:
CODE:
#$result .= '<h3>'.$prog.'</h3>'.PHP_EOL; // OLD STYLE
$result .= '<h3>'.$prog. ' '.$_POST['filename'].'</h3>'.PHP_EOL; // NEW STYLE
$result .= '</div>'.PHP_EOL;
$result .= $contents.PHP_EOL;
Problem is, I don't want the file name to display the extension (in my code example, it is .csv)
Can anyone assist in the right code needed to remove the extension (.CSV)...?
// Use this code I hope it's useful ..
=> The basename() function returns the filename
<?php
$path = "filename.CSV"; // set your file name
//Show filename with file extension
echo basename($path) ."<br/>";
//Show filename without file extension
echo basename($path,".CSV");
?>
OutPut :-
filename.CSV
filename
OR
// you also use pathinfo() function .
$file_name = pathinfo('filename');
echo $file_name['dirname'], "\n";
echo $file_name['basename'], "\n";
echo $file_name['extension'], "\n";
echo $file_name['filename'], "\n"
==> Check this Demo Link :-
https://eval.in/930790
I have a set of functions like this:
<?php
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img.png', $unencodedData);
?>
This saves a file called img.png to the server. Now I need the last file_put_contents function to return the path/absolute URL of the file it just created. I can't seem to find an option for this in the php documentation.
Is there an option for this or alternatively any other method for returning the path/absolute URL?
Thanks
Use:
$abs_path = __DIR__.'/img.png';
file_put_contents($abs_path, $unencodedData);
echo $abs_path;
If you're writing into the same folder as the script, as in your example:
dirname($_SERVER['PHP_SELF']) . '/' . 'img.png'
To embed a executable file I have written this code
<?php
$file = "C:/embed";
$output = exec($file);
echo $output;
?>
Now how can I give the input command, where embed.in is the file name?
"embed >embed.in"
$file = "C:/embed > embed.in";
What's wrong with above?
i am trying to use this bit of code to first retrieve a URL that is stored in a txt file on my server and save it as a variable, then run file_get_contents a second time using the URL i just retrieved and saved as a variable.
the code works for the first file_get_contents and echoes the URL that is stored, but fails to then use that URL in the second file_get_contents to echo the contents of the URL.
<?php
$files = file_get_contents('http://example.com/txtfile.txt');
echo $files;
$file = file_get_contents($files);
echo $file;
?>
Well the direct solution to your problem is:
<?php
$files = file_get_contents('http://example.com/txtfile.txt');
echo $files;
$files = trim($files);
$file = file_get_contents($files);
echo $file;
?>
But this is huge security risk. Running file_get_contents to open a variable file is risky.