Fread() : Le paramètre de longueur doit être supérieur à 0
Solved
mikesunshine59
Posted messages
75
Status
Member
-
mikesunshine59 Posted messages 75 Status Member -
mikesunshine59 Posted messages 75 Status Member -
Bonjour,
I get the message: fread(): Length parameter must be greater than 0
When I run my PHP below:
Can you help me? (The error message occurs when the file is sometimes empty)
I get the message: fread(): Length parameter must be greater than 0
When I run my PHP below:
<?php // FTP connection parameters $ftp_server = "*************"; // Address of FTP server. $ftp_user_name = "*************"; // Username $ftp_user_pass = "*************"; // Password // Starting FTP connection page $conn_id = ftp_connect($ftp_server); // Connecting to FTP $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); ftp_pasv($conn_id, true);ftp_pasv($conn_id, true); $file = './Pomme.csv'; if ($config = fopen($file, 'r+')) // Opening the file { if ($content = fread($config, filesize($file))) // Reading the file (the pointer ends up at the end) { // Modifying the file structure $content = str_replace('",', ',', $content); $content = str_replace(',"', ',', $content); $content = str_replace('"', '', $content); rewind($config); // resetting the pointer to the beginning of the file ftruncate($config, 0); // clearing the content fwrite($config, $content); // writing the new content fclose($config); } } // Checking modifications if (!$content) { echo "KO"; } else { echo "OK"; } // Closing the FTP connection. ftp_quit($conn_id); ?> Can you help me? (The error message occurs when the file is sometimes empty)
1 answer
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention Ambassadeur 1 588
Good evening, please always indicate on which line an error message occurs.
Why do you want to read the content of an empty file?
It is not allowed to use fread() with a length of less than 1 byte.
I suggest you test filesize($file) before calling fread().-
Good evening,
The file comes from the result of an SQL query.
And sometimes the query is empty.
The generated file is therefore empty in that case.
Can you tell me how to avoid this message when the file is empty in the above PHP, please?
Thank you for your help.if ( filesize($file)>0){ if ($content = fread($config, filesize($file))) // Lecture du fichier (le pointeur se retrouve à la fin) { // Modification de la structure du fichier $content = str_replace('",', ',', $content); $content = str_replace(',"', ',', $content); $content = str_replace('"', '', $content); rewind($config); // on remet le pointeur au début du fichier ftruncate($config, 0); // on efface le contenu fwrite($config, $content); // on écrit le nouveau contenu fclose($config); } } else{ echo "Fichier vide."; }
-
-