after i have installed windows 8 on my desktop and reinstalled aptana and xampp, i somehow can't use !feof($handle). i want to get the symbols of nasdaq stored in my $symb arra.here is an example and my error:
$symb = array();
$url = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
$handle = fopen("$url","r");
while( !feof($handle) ){
$line = fgetcsv($handle, 1024);
if($line!="Symbol" && isset($line[0]) && $line[0] != null ){
$symb[] = trim($line[0]);
}
fclose($handle);
}
And my Errors :
Warning: feof(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 61
Warning: fgetcsv(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 62
Warning: fclose(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 66
.......
Is there a setting i have to change on the php.ini file or what could it be ?
thanks.
.....
$url = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
$handle = fopen("$url","r");
$txt = fread( $handle, 8 );
print_r($txt);
.....
prints out :
"Symbol"
so my fopen() is fine ....
The reinstallation and the fopen() are red herrings. You're closing the file handle inside the while loop, before the file has been read.
while( !feof($handle) ){
$line = fgetcsv($handle, 1024);
if($line!="Symbol" && isset($line[0]) && $line[0] != null ){
$symb[] = trim($line[0]);
}
// fclose($handle); // Move this outside the while loop
}
fclose($handle); // Moved this outside the while loop
I had the same problem in my code. I had an extra file close function in the code. In other words I was trying to write to the file for a second time and it was not open for use. I had this coded in the top of my program:
$newFile = $local_directory . $tenant.'-'.$namespace.'_Stats.xml';
$newDoc = fopen($newFile, "w");
fwrite($newDoc, $urlStats);
fwrite($newDoc, $response);
fclose($newDoc);
Then later I had:
fwrite($newDoc, $response);
fclose($newDoc);
Check to make sure the file is open before you write more content.
Related
I was editing WordPress and the error message below came out.
" Notice: fread(): read of 8192 bytes failed with errno=21 Is a directory in /home/c7006248/public_html/ondine199918.com/wp-includes/functions.php on line 6574"
The code is as follows.
*/
function get_file_data( $file, $default_headers, $context = '' ) {
// We don't need to write to the file, so just open for reading.
$fp = fopen( $file, 'r' );
if ( $fp ) {
// Pull only the first 8 KB of the file in.
$file_data = fread( $fp, 8 * KB_IN_BYTES );
// PHP will close file handle, but we are good citizens.
fclose( $fp );
} else {
$file_data = '';
}
Does anyone have any idea of a potential fix for that issue?
I would be more than grateful if you help me with a fix. Thanks in advance! :)
Hitomi
For the additional information.
The error location I'm getting is
" Notice: fread(): read of 8192 bytes failed with errno=21 Is a directory in /home/c7006248/public_html/ondine199918.com/wp-includes/functions.php on line 6574 "
And the line 6574 is
$file_data = fread( $fp, 8 * KB_IN_BYTES );
from the extracted code above.
Sorry for the incomplete information.
I guess you are trying to open a directory instead of file
So you need to check if it's a file or not before trying to read the first 8kb
function get_file_data( $file, $default_headers, $context = '' ) {
if (!is_file($path) === false) {
return;
}
I am trying to read the first 5 line code-block in txt file, please how do i do this
I have this php code to get only the first line
<?php
$file = 'example.txt';
$f = fopen($file, 'r');
$line = fgets($f);
while (($line = fgets( $f)) !== false) {
for ($list = 1; $list < 6; $list++){
$codeline= htmlentities($line );
}
}
fclose($f);
?>
You can use a for loop:
for ($x = 1; $x < 6; $x++) {
$line = fgets($f);
}
To open and read a file line by line:
$file = fopen( "/path/to/file.txt", "r" );
$index=0;
while ((( $line = fgets( $file )) !== false) && ( $index++ < 5 )) {
echo $line;
}
fclose( $file );
Here, I am initializing a variable index to 0.
In the while loop, we will use fgets to read the next line of the file, and assign it to the variable line. We will also check that the value of index is less then 5, our desired line count, in addition to incrementing the value of the index, after we have read the line.
Once the value of index has reached > 5, the loop will exit, and the file stream will be closed.
The advantage of using fopen and fgets over something like file, is that the latter will load the entire contents of the file into memory - even if you do not plan on using the whole thing.
With a multi line file, the above code will print out the first five lines.
This is a multi line
Even more simple:
<?php
$file_data = array_slice(file('file.txt'), 0, 5);
print_r($file_data);
source: get the first 3 lines of a text file in php from #paul-denisevich
while (($line = fgets($handle)) !== false) {
//look for the first payor block
if(strpos($line, 'N1*PR*') !== false || $block_start) {
$header_end = true; $block_start = true;
//see if the block finished
if(strpos($line, 'CAS*CO*45*20.43**253*1.27~') !== false) {
$block_start = false;
$payor_blocks[$count] .= $line;
$count++;
}
$payor_blocks[$count] .= $line;
} else {
//append to the header
if($header_end) {
$footer .= $line."\n";
} else {
$header .= $line."\n";
}
}
}
//get payor blocks and create a file foreach payor
$new_files = array();
foreach($payor_blocks as $block) {
$filename = $file . "_" . $count;
$count++;
$new_files[] = array(
'name' => $filename,
'content' => $header."\n".$block."\n".$footer
);
//loop through new files and create them
foreach($new_files as $new_file) {
$myfile = fopen($file, "x");
fwrite($myfile, $new_file['content']);
//close the file
fclose($myfile);
I have the code above, it's suppose to be able to open an original file called "$file" and create a new file then close it, However its not creating and when I run it, i get this warning error:
Warning: fopen(362931550.1a): failed to open stream:
File exists in /script2.php on line 90 Warning:
fwrite() expects parameter 1 to be resource,
boolean given in /script2.php on line 94 Warning:
fclose() expects parameter 1 to be resource, boolean
given in /script2.php on line 96
Any help is kindly appreciated.
I have one file named: 362931550.1a
I did a code that splits them at certain areas, (its pretty long to post), when i run the script I see it on my browser but it doesn't create 2 new files in the folder.
Your file open mode is incorrect.
From php.net documentation:
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING [...]
You should probably use 'w' mode:
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
The script failed to open a stream with the fopen() function and return a boolean. The function fwrite() become the boolean value but need a resource.
The reason is that you only create files with the x-modifier in the stream.
Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
You see in the PHP manual more informations about the stream-modes (PHP manual).
To prevent this message check if the value isn't false.
$stream = fopen("file.txt", "x");
if($stream === false) {
echo "Error while open stream";
}
//here your code
EDIT after all the answers, i updated the function, and it works
I read out a importfolder. In this folder are many different files available.
Step: I read the folder and add the files to a array
Step: I open every file and try to import
When i cant import a file, then this happens, when another file in this row have to be imported first.
Example: If I open a file "message to a address", this could not be imported, when the address are not added into the database. But in some other file of this filelist is the "create address"-file. When this is created, then it is good, when the "message to a address" will be added to the filelistarray on the end.
My Code give me an offset problem:
function importData( $path, $db, $mail )
{
//Get available Importfiles
$filelist = getFilelist( $path );
for ($i = 0; $i < count($filelist); $i++)
{
$filename = $path . "/" . $filelist[$i];
$file = fopen( $filename,"r" );
while(!feof( $file )) {
$items = explode( ";", fgets( $file ) );
//Get messagetyp
if( strtolower(trim($items[0])) == "nachrichtentyp" )
{
$messagetyp = $items[1];
break;
}
}
fclose($file);
if ( $messagetyp )
{
$f = "import" . $messagetyp;
if( !$f($filename, $db, $mail) )
{
array_push($filelist, $filelist[$i]);
}
}
}
}
This my error, when I push the element to the the filelist-array
PHP Warning: feof() expects parameter 1 to be resource, boolean given in /var/www/symfony/importscript/import.php on line 37
PHP Warning: fgets() expects parameter 1 to be resource, boolean given in /var/www/symfony/importscript/import.php on line 38
According to your errors, problem lies not in array_push but in fopen():
$file = fopen( $filename,"r" );
If php fails to open that file, variable $file will be set to false and because of that feof() and fgets() will give you errors.
You definitely should check if fopen returns another value than FALSE, maybe one of the files does not exist or you are restricted.
I am a new PHP developer and I just started working with files in PHP.
I have the following code to count number of txt files in the directory and store their names in an array and then using a loop display the total lines in each of the files!
here is the code, help me where I have gone wrong!
$dir = opendir('directory/');
$num_files = 0;
$dir_files[] = array();
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..','Thumbs.db')) and !is_dir($file)){
$num_files++;
echo $file;
array_push($dir_files,$file);
echo "<br />";
}
}
echo "--------------------------------------<br />";
echo "Number of files in this directory: ".$num_files."<br />";
echo "--------------------------------------<br />";
foreach($dir_files as $dir_file=>$value){
$file='directory/'.$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
I get the following errors:
Notice: Array to string conversion in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 19
Warning: fopen(directory/Array): failed to open stream: No such file or directory in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 21
Warning: feof() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 22
Your code is toooooooo lengthy. Try this : This will do whole functionality for you, let me know if any issues.
foreach(glob('directory/*.txt',GLOB_BRACE) as $value){
$file =$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
change:
$dir_files[] = array();
to
$dir_files = array();
And:
fopen() returns a file pointer resource on success, or FALSE on error.As it is throwing an error opening the file, feof() is receiving FALSE instead of a file pointer resource: so you get the error "expects parameter 1 to be resource, boolean given in"...