I have some code that create a tmp file using php://temp ...
For debugging purpose and learning as well, I would like to save/copy into a defined file.
So there is any easy way to do that ?
You can use stream_get_contents or fwrite when working with large files
//Somecode wrting to temp
$tmp = fopen('php://temp', 'r+');
fwrite($tmp, 'test');
rewind($tmp);
// Read and save to log.txt
file_put_contents("log.txt",stream_get_contents($tmp));
Large File Implementation
set_time_limit(0);
$file = "log.txt";
$final = fopen($file, "w+");
while ( ! feof($tmp) ) {
fwrite($final, fgets($tmp));
}
fclose($tmp);
fclose($final);
Related
I am trying to write to a file and then read the data from the same file. But sometimes I am facing this issue that the file reading process is getting started even before the file writing gets finished. How can I solve this issue ? How can i make file writing process finish before moving ahead?
// writing to file
$string= <12 kb of specific data which i need>;
$filename.="/ttc/";
$filename.="datasave.html";
if($fp = fopen($filename, 'w'))
{
fwrite($fp, $string);
fclose($fp);
}
// writing to the file
$handle = fopen($filename, "r") ;
$datatnc = fread($handle, filesize($filename));
$datatnc = addslashes($datatnc);
fclose($handle);
The reason it does not work is because when you are done writing a string to the file the file pointer points to the end of the file so later when you try to read the same file with the same file pointer there is nothing more to read. All you have to do is rewind the pointer to the beginning of the file. Here is an example:
<?php
$fileName = 'test_file';
$savePath = "tmp/tests/" . $fileName;
//create file pointer handle
$fp = fopen($savePath, 'r+');
fwrite($fp, "Writing and Reading with same fopen handle!");
//Now rewind file pointer to start reading
rewind($fp);
//this will output "Writing and Reading with same fopen handle!"
echo fread($fp, filesize($savePath));
fclose($fp);
?>
Here is more info on the rewind() method http://php.net/manual/en/function.rewind.php
I have mentioned the URL through which i got the solution. I implemented the same. If you want me to copy the text from that link then here it is :
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX))
{
fwrite($file,"Write something");
// release lock
flock($file,LOCK_UN);
}
else
{
echo "Error locking file!";
}
fclose($file);
Use fclose after writing to close the file pointer and then fopen again to open it.
below is the code which i want to modify
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
if ($realSize != $this->getSize()){
return false;
}
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
I want to save the contents into the memory and transfer it accross to other server without saving it on apache server.
when i try to output the contents i only see resource id# 5. Any suggestion, comments are highly apprecited . thanks
The code you have opens file handles, which in themselves are not the content. To get the content into a variable, just read it like any other file:
$put = file_get_contents('php://input');
To get the contents of the stream:
rewind($temp); // rewind the stream to the beginning
$contents = stream_get_contents($temp);
var_dump($contents);
Or, use file_get_contents as #deceze mentions.
UPDATE
I noticed you're also opening a temp file on disk. You might want to consider simplifying your code like so:
$put = stream_get_contents(STDIN); // STDIN is an open handle to php://input
if ($put) {
$target = fopen('/storage/put.txt', "w");
fwrite($target, $put);
fclose($target);
}
I have 100 txt files on my webserver. I have to insert a string e.g. abcdef on the beginning of all of them using php and save them back again. How is this possible ?
Well, google found this.
function prepend($string, $filename) {
$context = stream_context_create();
$tmpname = tempnam(".");
$fp = fopen($filename, "r", 1, $context);
file_put_contents($tmpname, $string);
file_put_contents($tmpname, $fp, FILE_APPEND);
fclose($fp);
unlink($filename);
rename($tmpname, $filename);
}
So you call prepend($string, $filename) for every file and you're done.
Look at opendir, glob, fopen, and fwrite. For example:
foreach (glob("*.txt") as $file) {
$fh = fopen($file, 'c'); //Open file for writing, place pointer at start of file.
fwrite($fh, 'abcdef');
fclose($fh);
}
Hi I do have a text file with size of upto 30MB I would like to read this file using PHP loop script
$lines = file('data.txt');
//loop through each line
foreach ($lines as $line) { \\some function }
Is there any way? I want open it for reading php doesnt allow me to open a 30MB file.
You could read it line by line like this:
$file = fopen("data.txt", "r") or exit("Unable to open file!");
while(!feof($file)) {
// do what you need to do with it - just echoing it out for this example
echo fgets($file). "<br />";
}
fclose($file);
Read line by line using:
$handle = fopen ( "data.txt", "r" );
while ( ( $buffer = fgets ( $handle, 4096 ) ) !== false ) {
// your function on line;
}
If it is suitable for you to read the file piece-by-piece you can try something like this
$fd = fopen("fileName", "r");
while (!feof($fd)) {
$buffer = fread($fd, 16384); //You can change the size of the buffer according to the memory you can youse
//Process here the buffer, piece by piece
}
fclose($fd);
I'm using file() to read through a file like an array with tabs. I want to lock the file but I cannot seem to get flock() working on the file. Is it possible to do this? If so, how? If not, does the file() lock the file from the start and alleviate any potential sharing problems?
According to the documentation (specifically the comments), it won't read a file that was locked via flock.
You have 2 alternatives.
Read the file with fgets (without checks for errors):
$f = fopen($file, 'r');
flock($f, LOCK_SH);
$data = array();
while ($row = fgets($f)) {
$data[] = $row;
}
flock($f, LOCK_UN);
fclose($f);
Read the file with file() and using a separate "lockfile":
$f = fopen($file . '.lock', 'w');
flock($f, LOCK_SH);
$data = file($file);
flock($f, LOCK_UN);
fclose($f);
unlink($file . '.lock');