简体   繁体   中英

Warning: fread() [function.fread]: Length parameter must be greater than 0

I've got a site hosted with 1and1 - who are useless! For some reason the backup scripts they provide no longer work and they can't provide me with an answer! So I thought I'd write my own, this is my code:

  if (file_exists('backupfile.sql')){

 $FP = fopen ( 'backupfile.sql', 'r' );
 $READ = fread ( $FP, filesize ( 'backupfile.sql') );
 $READ = explode ( ";\n", $READ );

 foreach ( $READ AS $RED )
 {
  mysql_query ( $RED );
 }

  echo 'Done'; 
 }else{
  echo "no file";
 }

however this gives me the following error

Warning: fread() [function.fread]: Length parameter must be greater than 0 

if I put an @ infront of the fread the script doesn't error but it doesn't perform the backup.

Well... My guess from the error message would be that backupfile.sql is empty. Therefore, fread() can't read anything from it.

filesize ( 'backupfile.sql')

is not giving you a filesize, try putting in the full path to the file, also make sure PHP has permission to read the file.

edit: you can use is_readable() to test if the file can be read

you have to specify a max read size when using fread. that indicates that it thinks your filesize is 0. try using FILE instead since it's a local file and all...

if (file_exists('backupfile.sql')){

 $READ = file( 'backupfile.sql');

 foreach ( $READ AS $RED )
 {
  mysql_query ( $RED );
 }

  echo 'Done'; 
 }else{
  echo "no file";
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM