Errno=9 bad file descriptor in php - php

Im trying to write a file in php with the following code
$filename = "uploads/stories/".$row['s_source'];
$handle = fopen($filename, "r");
if (filesize($filename) == 0) {
$txt = "{====views====}{====likes====}{====chapters====}{====comments====}";
fwrite($handle, $txt);
}
$content = fread($handle, filesize($filename));
fclose($handle);
And I get:
Notice: fwrite(): write of 66 bytes failed with errno=9 Bad file descriptor in E:\xampp\htdocs\host\host\storyedit.php on line 20
Warning: fread(): Length parameter must be greater than 0 in E:\xampp\htdocs\host\host\storyedit.php on line 22
Notice: Undefined offset: 1 in E:\xampp\htdocs\host\host\storyedit.php on line 27
But there is no error (I think) in the code. Could someone help me?
I've tried:
Deleting opcache.enable_cli=1 in php.ini
Deleting cache
none worked.

You open with reading access r and then you give a write command >. You need to open with read/write access:
$handle = fopen($filename, "r+");
In relation to error in fread, try:
if (filesize($filename) > 0) {
$content = fread($handle, filesize($filename));
}

Related

How to use SplFileObject() with FTP?

This code runs very well with fopen:
<?php
$filename = "ftp://$ftp_user_name:$ftp_user_pass#$ftp_server/testes/TJ_IDA.TXT";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
But when I try with SplFileObject:
$file = new SplFileObject($filename,"r");
I got this error:
Warning: Unknown: FTP server error 426:426 Failure writing network
stream. in Unknown on line 0
Is there another way to use SplFileObject with FTP?

Can't fgets() a 1 gig file?

Code:
$filename = 'Master_List_DeDuped.csv';
$fp = fopen($filename, "r");
while (false !== ($line = fgets($fp))) {
echo $line;
die(); // For Debugging only
}
fclose($fp);
The resulting error:
Warning: fgets(): 3 is not a valid stream resource in /home3/public_html/index.php on line 288
Line 288 is the while statement. The same commands work fine with a smaller file. My file is about 1.1 gigs. Is it just a file size limitation?
Edit: I've tried adding the length parameter to fgets, but the same error shows. http://us2.php.net/fgets
Changed the code a bit based on the example at php.net http://us2.php.net/fgets . The code that works is:
$filename = 'Master_List_DeDuped.csv';
$fp = #fopen($filename, "r");
if ($fp) {
while (($line = fgets($fp, 4096)) !== false) {
echo $line;
die(); // For Debugging only
}
}
fclose($fp);

Lightweight fopen technique in PHP

I'm trying to write data to a simple txt file in PHP and open it so the user can download it. I'd like to keep it very lightweight, when I run this I get:
Warning: fopen(testFile.txt) [function.fopen]: failed to open stream: File exists in /home/user/script.php on line 9
Here is my sample code:
<?php
$File = "sample.txt";
$write = fopen($File, 'w') or die();
$stringData = "asdfjkl;";
fwrite($write, $stringData);
fclose($write);
$open = fopen($File, 'x') or die();
fclose($open);
?>
Thank you!
Use file_put_contents to write to the file:
$filename = "sample.txt";
$stringData = "asdfjkl;";
file_put_contents($filename, $stringData);
Then when you want to output the contents of the file use:
$filename = "sample.txt";
readfile($filename);
If you want the file contents to be returned as a string instead of output immediately use file_get_contents instead of readfile

Extracting text between values in txt file using php

Im trying to extract the values between eng_tid
and eng_data for http://fdguirhgeruih.x10.mx/html.txt and I keep getting T string errors.
why do I keep getting errors
<? php
//First, open the file. Change your filename
$file = "http://fdguirhgeruih.x10.mx/html.txt";
$word1='tid';
$word2='data';
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
$between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));
echo $between;
?>
UPDATE after seeing error messages...
Instead of fread() and attempting to use the size in bytes of your target file, you may simply use file_get_contents() to retrieve the remote file. Your error is because PHP wants to read the filesize of the file as though it is local, but it is a remote file over HTTP. filesize() reports 0 and an error. Instead do
// Don't do this...
//$handle = fopen($file, "r");
//$contents = fread($handle, filesize($file));
//fclose($handle);
// Instead do this...
$contents = file_get_contents($file);

Working with huge text files in PHP?

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

Categories