Please explain to me why the function file_get_contents returns the contents of the file, while fread does not. How can I read file content after fputcsv?
$file = fopen('test.txt', 'r+');
fputcsv($file, [1, 2, 4]);
var_dump(fread($file, 1024));
var_dump(file_get_contents('test.txt'));
fread() reads from the current position in the file stream, which is after what you just wrote. You need to seek to the beginning to read the file.
$file = fopen('test.txt', 'r+');
fputcsv($file, [1, 2, 4]);
fseek($file, 0);
var_dump(fread($file, 1024));
Related
Trying to download files via the URL, rename from the .ADM extension to .txt
then put contents of each file into a single txt file
However its saying the fputs param 2 is a resource
The $logfile['name'] is the filename thats stored in the array
Heres my code
foreach($items as $logfile)
{
$getfile = $logfile['Download'];
$newfile = file_put_contents(str_replace('ADM','txt',$logfile['name']),
file_get_contents($getfile));
$name = str_replace('ADM','txt',$logfile['name']);
$newfile = $name;
$file = fopen($newfile, 'rb');
$output = fopen('tmp/test.txt', 'wb');
fputs($output, $file);
fclose($output);
fclose($file);
}
Its downloading each and renaming however its not moving the content & giving me this error
Warning: fputs() expects parameter 2 to be string, resource given in
The second parameter of fputs is the content of a file. Not a file resource.
Instead of
$file = fopen($newfile, 'rb');
You‘ll need to get the contents of the file
$content = file_get_contents($newfile);
Or, if you like to use fopen, you can use fread:
$file = fopen($newfile, 'rb');
$content = fread($fp, filesize($newfile));
Then you can put this content into another file:
fputs($output, $content);
How can I have php return just some bytes of a file? Like, it I wanted to load byte 7 through 15 into a string, without reading any other part of the file? Its important that I don't need to load all of the file into memory, as the file could be quite large.
Could use file_get_contents() using the offset and maxlen parameters.
$data = file_get_contents('somefile.txt', false, NULL, 6, 8);
Use fseek() and fread()
$fp = fopen('somefile.txt', 'r');
// move to the 7th byte
fseek($fp, 7);
$data = fread($fp, 8); // read 8 bytes from byte 7
fclose($fp);
Using Pear:
<?php
require_once 'File.php';
//read and output first 15 bytes of file myFile
echo File::read("/path/to/myFile", 15);
?>
Or:
<?php
// get contents of a file into a string
$filename = "/path/to/myFile";
$handle = fopen($filename, "r");
$contents = fread($handle, 15);
fclose($handle);
?>
Either method you can use byte 7-15 to do what you want. I don't think you can go after certain bytes without starting from the beginning of the file.
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);
}
How can I have php return just some bytes of a file? Like, it I wanted to load byte 7 through 15 into a string, without reading any other part of the file? Its important that I don't need to load all of the file into memory, as the file could be quite large.
Could use file_get_contents() using the offset and maxlen parameters.
$data = file_get_contents('somefile.txt', false, NULL, 6, 8);
Use fseek() and fread()
$fp = fopen('somefile.txt', 'r');
// move to the 7th byte
fseek($fp, 7);
$data = fread($fp, 8); // read 8 bytes from byte 7
fclose($fp);
Using Pear:
<?php
require_once 'File.php';
//read and output first 15 bytes of file myFile
echo File::read("/path/to/myFile", 15);
?>
Or:
<?php
// get contents of a file into a string
$filename = "/path/to/myFile";
$handle = fopen($filename, "r");
$contents = fread($handle, 15);
fclose($handle);
?>
Either method you can use byte 7-15 to do what you want. I don't think you can go after certain bytes without starting from the beginning of the file.
I need to scan through a 30MB text file - it's a list of world cities - How can I access this file, I feel like a File_Get_Contents will give my server a stroke
Just fopen it and then use fgets.
Filesystem functions come handy in this situation.
Example
$filename = "your_file_path";
// to open file
$fp = fopen($filename, 'r'); // use 'rw' to open file in read/write mode
// to output entire file
echo fread($fp, filesize($filename));
// to close file
fclose($fp);
References
(some handy functions)
All Filesystem Functions
fopen() - open file
fread() - read file content
fgets() - to get line
fwrite() - write content to file
fseek() - change file pointer's position
rewind() - rewind file pointer to pos 0
fclose() - close file
...
<?php
$fh = #fopen("inputfile.txt", "r");
if ($fh) {
while (($line = fgets($fh)) !== false) {
echo $line;
// do something with $line..
}
fclose($fh);
}
?>
More information/examples on http://pt.php.net/manual/en/function.fgets.php