Skip to content

Examples for using the FTP and SFTP protocols with Laravel to manage and read files.

Notifications You must be signed in to change notification settings

cp6/laravel-ftp-sftp

Repository files navigation

Laravel Logo

Laravel - 11 PHP - 8.2

Example PHP Laravel File actions with FTP and SFTP connections

Laravel-ftp-sftp provides examples of using the FTP, SFTP and local storage protocols combined with Laravel to manage and action files.

Examples for SFTP, FTP and storage include these actions:

  • List directories
  • List files and sizes
  • List directories and files
  • Upload file
  • Download file
  • Rename file
  • Move file
  • Copy file
  • Delete file
  • Writing to File
  • Read files without downloading all of it
  • Read files from the last line read
  • Compare files
  • Blade files to view/create/edit Connections

Uses phpseclib library for SFTP and PHP-FTP for FTP.

Connection

Used for creating SFTP and FTP connections

SFTP connection:

Connection::makeSftpConnection(string $host, int $port, string $user, ?string $password = '', int $timeout = 8, ?string $key = ''): ?SFTP

FTP connection:

Connection::makeFtpConnection(string $host, int $port, string $user, ?string $password = '', int $timeout = 8): ?\FTP\Connection

SFTP file and directory methods:

Connection::listSftpCurrentDirectorySize(Connection $connection, string $path = ''): ?array
Connection::listSftpDirectories(Connection $connection, string $path = ''): ?array
Connection::listSftpFiles(Connection $connection, string $path = ''): ?array
Connection::listSftpFilesDirectories(Connection $connection, string $path = ''): ?array
File::uploadFile(Connection $connection, string $local_disk, string $local_filepath, string $upload_as): bool
File::outputSftpFileToBrowser(Connection $connection, string $file_path)
File::deleteSftpFile(Connection $connection, string $file_to_delete): bool
File::renameSftpFile(Connection $connection, string $current_path, string $new_path): bool

File::renameSftpFile($connection, 'files/images/dog.jpg', 'files/images/cat.jpg');
File::compareModifiedTimeSftp(Connection $connection, File $file): array

FTP file and directory methods:

Connection::listFtpDirectories(Connection $connection, string $path = ''): ?array
Connection::listFtpFiles(Connection $connection, string $path = ''): ?array
Connection::listFtpCurrentDirectorySize(Connection $connection, string $path = ''): ?array
Connection::listFtpFilesDirectories(Connection $connection, string $path = ''): ?array
File::readLinesFtp(Connection $connection, string $file_path, int $start = 0, int $num_lines = 100): ?array
File::renameFtpFile(Connection $connection, string $current_path, string $new_path): bool

File::renameFtpFile($connection, 'files/images/dog.jpg', 'files/images/cat.jpg');
File::deleteFtpFile(Connection $connection, string $file_to_delete): bool

File::deleteFtpFile($connection, '/files/logs.txt');
File::compareModifiedTimeFtp(Connection $connection, File $file): array

File

File actions such as downloading, uploading, deleting, moving and reading.

Download a file and create a File entry in the DB. This uses teh Laravel Storage Facade:

File::downloadFtpFile(Connection $connection, string $file_to_download, string $disk, string $save_to, string $save_as): bool

File::downloadFtpFile($connection, '/files/logs.txt', 'public'. '/downloaded', 'logs.txt');
File::downloadSftpFile(Connection $connection, string $file_to_download, string $disk, string $save_to, string $save_as): bool

File::downloadSftpFile($connection, '/files/logs.txt', 'public'. '/downloaded', 'logs.txt');

Local file and directory actions

File::fileExists(File $file): bool
File::moveFile(File $file, string $move_to, string $disk = ''): bool

File::moveFile($file, '/archived', 'public');
File::copyFile(File $file, string $copy_to, string $disk = ''): bool
File::renameFile(File $file, string $new_name): bool

File::renameFile($file, 'new.txt');
File::deleteFile(File $file): bool
File::downloadFileInBrowser(File $file, string $save_as = '')
//Prompts to download the file in the browser
File::displayFileInBrowser(File $file)
//Displays the file in the browser
File::readFileFromStorage(File $file, int $start = 0, int $end = 100): ?array
File::listFilesInDirectory(string $disk, string $path): array
File::listAllFilesInDirectory(string $disk, string $path): array
File::listDirectoriesInDirectory(string $disk, string $path): array
File::createDirectory(string $disk, string $path): bool
File::deleteDirectory(string $disk, string $path): bool

Reading a large local file

Uses SplFileObject, memory efficient it does not read through the whole file.

File::readLines(File $file, int $number_of_lines = 100): ?array

readLines() will start from last line read in the DB and then update last line read and total lines.

File::readLinesFromTo(File $file, int $from = 0, int $to = 100): ?array

readLinesFromTo() is for when you want to read specific lines rather than the sequential readLines().

File::readLastLines(File $file, int $amount = 20): ?array

readLastLines($file, 20) reads the last 20 lines in the file.

File::readOneLine(File $file, int $line = 1): ?array
File::readAllLines(File $file): ?array

Close the file pointer (If needed).

File::closeSplFile();

Writing to local file

File::writeToFile(File $file, string $data, array $options => []): ?bool
File::appendToFile(File $file, string $data): ?bool
File::prependToFile(File $file, string $data): ?bool
File::setFilePublic(File $file): bool
File::setFilePrivate(File $file): bool
File::getFileVisibility(File $file): string
File::getHash(File $file): string

About

Examples for using the FTP and SFTP protocols with Laravel to manage and read files.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages