I had used this function-----> import_request_variables('p') in more than 50 php files with this function --> extract($_GET, EXTR_PREFIX_ALL, 'p'); is there and php script can do this job without opening each file
I want something like this :
//read the entire string
$str=implode("",file('../*.php'));
$fp=fopen('../*.php','w');
//replace something in the file string, here i am replacing import_request_variables('p') to extract($_GET, EXTR_PREFIX_ALL, 'p')
$str=str_replace('import_request_variables('p')','extract($_GET, EXTR_PREFIX_ALL, 'p')',$str);
//now, save the file
fwrite($fp,$str,strlen($str));
this code may help you!
foreach (glob("path/to/files/*.php") as $filename)
{
$file = file_get_contents($filename);
file_put_contents($filename, str_replace("import_request_variables('p')","extract($_GET, EXTR_PREFIX_ALL",$file));
}
Related
I am uploading files that I need to attach to email via office365 API.
What I need is the content of the file in a variable WITHOUT storing/saving the file, how can I do that?
foreach ($request->filesToUpload as $file) {
$originalName = $file->getClientOriginalName();//working
$content = $file->getContent();//<- I need this, but not working
//$this->addAttachment($content, $originalName) //logic for later
}
Access the contents of the file like so:
$content = file_get_contents(Input::file('nameAttribute')->getRealPath());
or in other words inside that loop
$contents = file_get_contents($file->getRealPath());
Get the real path with object methods, and you may interact with it like any other file.
Waqas Bukhary,
You get that result, beucause, getContent is not a method of uploadedFiles, check the documentation.
But you have the path, so you can always read the content, like:
$path = $file->path();
$handle = fopen($path, 'r');
$content = fread($handle, filesize($path);
fclose($handle);
You can also use the Request File method if you know the name of the file field, check it here.
$a = trim($allDataInSheet [$i]["A"]);
$b=trim($allDataInSheet [$i]["B"]);
/* copy the source file */
$fname = "edm.html";
copy("edm.html","hk/edm.html");
$fcopy="hk/edm.html";
/* read the duplicate file */
$fhandle = fopen($fcopy,"r");
$content = fread($fhandle,filesize($fcopy));
/* replace the string in the duplicate file */
$content = str_replace($a,$b, $content);
$fhandle = fopen($fcopy,"w");
fwrite($fhandle,$content);
fclose($fhandle);
Above code was not working properly,I dont want to replace the string inside the source file.I need to take the duplicate of the source file and change the strings.Thanks in advance.Pleaes help me to fix it..
I think this may be simpler if you just use file_get_contents and file_put_contents.
$fname = 'edm.html';
copy('edm.html', 'hk/edm.html');
$fcopy = 'hk/edm.html';
// read the file
$content = file_get_contents($fcopy);
// make the replacement
$content = str_replace($a, $b, $content);
// write the file
file_put_contents($fcopy, $content);
Also if you put
error_reporting(E_ALL);
ini_set('display_errors', 1);
at the start of your script you will make sure that you are seeing all PHP errors.
for($i=0;$i<$directoriesCount;$i++)
{
$fileName=$Config['path']['basePath']."language/".$directories[$i]."/"."commontest.conf";
$file = fopen($fileName,"a");
$data = "testcontent";
fwrite($file,"\n");
fwrite($file,$data);
fclose($file);
}
the $directories variable will have array values:
en_lang
fr_lang
it_lang, etc.,
in the every directory we should find the commongtest.conf file to write the content.
In my test file its writes only the first values of array for ex 1. en_lang folder file only get fwrite other files not affected.
You have an extra double quote here:
."/".commontest.conf"
should be:
."/.commontest.conf"
A foreach statement might work better here for you:
foreach($directories as $directory){
$fileName=$Config['path']['basePath']."language/".$directory."/.commontest.conf";
$file = fopen($fileName,"a");
$data = "testcontent"."\n";
fwrite($file,$data);
fclose($file);
}
This code is working for me.
I think using file_put_contents would be even better.
foreach($directories as $directory){
$fileName=$Config['path']['basePath']."language/".$directory."/.commontest.conf";
$data = "testcontent"."\n";
file_put_contents($fileName,$data);
}
I've started a small project trying to make an online text editor, it WAS going well until the system started overwriting files and adding spaces in unnecessarily. I have one file called editor.php where all the file loading, saving and editing is done.
So this is the opening/closing for the files:
<?php
if(isset($_POST['new'])){
$filer = substr(md5(microtime()),rand(0,26),6);
$file_create = $filer.".txt";
$handle = fopen("files/".$file_create,"w");
fclose($handle);
header("Location: editor.php?e=".$filer);
}
$file = $_GET['e'];
$file = basename($file);
$filename = "files/".$file.".txt";
$file_get = file_get_contents($filename);
if(isset($_POST['save'])){
file_put_contents($filename, $_POST['text']);
}
?>
further down the page I have this in a <textarea> tag:
<?php
echo $file_content;
?>
This uses the string from the file_get_contents();
But when I save, nothing happens, in fact it erases the file, when I load a file there are eight spaces but nothing else.
I know there is another way to do this with fopen() and if someone could give me a method to use that, it would be much appreciated.
You have to verify if the $_POST['text'] actually has a content in it.
if(isset($_GET['e'])){
$file = $_GET['e'];
$file = basename($file);
$filename = $_SERVER['DOCUMENT_ROOT']."/files/".$file.".txt";
$file_get = file_get_contents($filename);
if(isset($_POST['save'])){
if(!empty($_POST['text']) && isset($_POST['text']))
{
$length = strlen($_POST['text']);
if($length > 0)
file_put_contents($filename, trim($_POST['text']));
else
die("No content");
}
}
}
ALso check if the file exists and its writable. You can use chmod,mkdir and file_exists functions.
Have a look at PHP's file modes: http://php.net/manual/en/function.fopen.php
If you are opening all your files using fopen() in w mode then your files are being truncated as they are opened. This is how w mode operates. Try using a+ or c+ modes with fopen().
EDIT
Also, the file_put_contents() will also overwrite file contents unless you sett the FILE_APPEND flag, e.g. file_put_contents($file, $data, FILE_APPEND).
I'm creating a html template with notification under nav bar , and admin can change that notification from the system the text of notification bar will be from notetxt file from the same location path where index.html is located i ave tried
<?php
foreach (glob("note.txt") as $filename) {
readfile($filename);
}
?>
and many other way but nothing happens it still stay blank
You are not echoing out the content of the textfile.
do it like this:
$myFile = "note.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
This will output your content of the file.
i'm using this code in pure html file
You can't use PHP functions in plain HTML file. MUST be written in a PHP file.
You have now in your code:
<span>
<!--?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?-->
</span>
Try with the examples above in a proper PHP file... then must work.
you can use file_get_contents function,
try something like this :
<?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?>
It's very simple use file_get_contents();
<?= file_exists('note.txt') ? file_get_contents("note.txt") : "file doesn't exists"; ?>
That is all what you need. file_get_contents() get the content of file and returns it. I've also checked if file exists because it may be your problem. Also make sure you have proper rights to read the file(CHMOD) and file is not empty.