I want to download or read part of a file from the FTP server instead of downloading the whole file, this is to just see the data that exists in the FTP server is correct.
We have so many clients and each file in the FTP server might be of any size, so instead of downloading or reading the complete file, I just want to download or read a part of the file, let's say I want only 5kb of file or if by line 100 lines from a file.
I have a function in PHP like below which does half of the work, but for larger files, it fails.
function readByBytes($path)
{
try
{
$handle = fopen($path, "rb");
if ($handle)
{
while (($buffer = fgets($handle, 4096)) !== false)
{
}
if (!feof($handle))
{
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
}
catch (Exception $e)
{
echo $e;
}
}
$filename = "ftp://username:password@127.0.0.1/prod/clientfeed.csv";
$iterator = readByBytes($filename);
foreach ($iterator as $key => $iteration)
{
/// if file read is 5kb or some 100 lines
break;
}
Can somebody help me or guide me on this in PHP or Python
Below warning errors getting
PHP Warning: fopen(ftp://...@127.0.0.1/prod/clientfeed.csv): failed
to open stream: FTP server reports 550 Could not get file size.
in /var/www/html/newLpplugins/ftp_read_line_line.php on line 80
PHP Warning: filesize(): stat failed for
ftp://...@127.0.0.1/prod/clientfeed.csv in
/var/www/html/newLpplugins/ftp_read_line_line.php on line 81
PHP Warning: fread() expects parameter 1 to be resource, bool given
in /var/www/html/newLpplugins/ftp_read_line_line.php on line 81
PHP Warning: fclose() expects parameter 1 to be resource, bool given
in /var/www/html/newLpplugins/ftp_read_line_line.php on line 82
PHP Warning:
file_get_contents(ftp://...@127.0.0.1/prod/clientfeed.csv): failed to
open stream: FTP server reports 550 Could not get file size.
in /var/www/html/newLpplugins/ftp_read_line_line.php on line 84
Thanks in advance.