Asked  7 Months ago    Answers:  5   Viewed   34 times

I was wondering how to save PHP variables to a txt file and then retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in the input box will be saved to a text file. Later on the results need to be brought back as a variable. So lets say the variable is $text I need that to be saved to a text file and be able to retrieve it back again.

 Answers

32

This should do what you want, but without more context I can't tell for sure.

Writing $text to a file:

$text = "Anything";
$var_str = var_export($text, true);
$var = "<?phpnn$text = $var_str;nn?>";
file_put_contents('filename.php', $var);

Retrieving it again:

include 'filename.php';
echo $text;
Wednesday, March 31, 2021
 
Sidarta
answered 7 Months ago
56

It happens because PHP CS Fixer modifies the file, as @LazyOne said, but you have empty value of "Output paths to refresh" so IDE cannot know about these changes.

Set the value of "Output paths to refresh" to $FileName$ - same as in arguments - to make PhpStorm aware about the changes (it depends on the "Working directory" value which has been set in Other Options - if it is set to $FileDir$ then you don't need to mention it in the paths to refresh).

Wednesday, March 31, 2021
 
Terry
answered 7 Months ago
37

I've started project called Big File Tools. It is proven to work on Linux, Mac and Windows (even 32-bit variants). It provides byte-precise results even for huge files (>4GB). Internally it uses brick/math - arbitrary-precision arithmetic library.

Install it using composer.

composer install jkuchar/BigFileTools

and use it:

<?php
$file = BigFileToolsBigFileTools::createDefault()->getFile(__FILE__);
echo $file->getSize() . " bytesn";

Result is BigInteger so you can compute with results

$sizeInBytes = $file->getSize();
$sizeInMegabytes = $sizeInBytes->toBigDecimal()->dividedBy(1024*1024, 2, BrickMathRoundingMode::HALF_DOWN);    
echo "Size is $sizeInMegabytes megabytesn";

Big File Tools internally uses drivers to reliably determine exact file size on all platforms. Here is list of available drivers (updated 2016-02-05)

| Driver           | Time (s) ?          | Runtime requirements | Platform 
| ---------------  | ------------------- | --------------       | ---------
| CurlDriver       | 0.00045299530029297 | CURL extension       | -
| NativeSeekDriver | 0.00052094459533691 | -                    | -
| ComDriver        | 0.0031449794769287  | COM+.NET extension   | Windows only
| ExecDriver       | 0.042937040328979   | exec() enabled       | Windows, Linux, OS X
| NativeRead       | 2.7670161724091     | -                    | -

You can use BigFileTools with any of these or fastest available is chosen by default (BigFileTools::createDefault())

 use BigFileToolsBigFileTools;
 use BigFileToolsDriver;
 $bigFileTools = new BigFileTools(new DriverCurlDriver());
Wednesday, June 2, 2021
 
qitch
answered 5 Months ago
45

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

Thursday, August 5, 2021
 
Nil
answered 3 Months ago
Nil
42

Excavated and adapted from the guts of utils:::print.help_files_with_topic:

file <- help("reshape")
pkgname <- basename(dirname(dirname(file)))
temp <- tools::Rd2txt(utils:::.getHelpFile(file), out = tempfile("Rtxt"), 
                  package = pkgname)
file.copy(temp,"~/test.txt")
Sunday, August 29, 2021
 
Bart Kiers
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share