Laravel-FTP
A simple Laravel 5/6/7 ftp service provider., (*1)
, (*2)
Installation
Add the package to your composer.json
and run composer update
., (*3)
{
"require": {
"anchu/ftp": "~2.0"
}
}
If you're using Laravel 5.5+ skip the next step, as Laravel auto discover packages., (*4)
Add the service provider in config/app.php
:, (*5)
'Anchu\Ftp\FtpServiceProvider',
Configuration
Run php artisan vendor:publish --tag=config
and modify the config file(config/ftp.php
) with your ftp connections., (*6)
You can add dynamic FTP connections with following syntax, (*7)
Config::set('ftp.connections.key', array(
'host' => '',
'username' => '',
'password' => '',
'passive' => false,
'secure' => false,
));
Accessing connections
You can access default FTP connection via the FTP::connection
method:, (*8)
FTP::connection()->getDirListing(...);
When using multiple connections you can access each specific ftp connection by passing connection name:, (*9)
FTP::connection('foo')->getDirListing(...);
Sometimes you may need to reconnect to a given ftp:, (*10)
FTP::reconnect('foo');
If you need to disconnect from a given ftp use the disconnect method:, (*11)
FTP::disconnect('foo');
Basic usage examples
// With custom connection
$listing = FTP::connection('my-ftp-connection')->getDirListing();
// with default connection
$listing = FTP::connection()->getDirListing();
$status = FTP::connection()->makeDir('directory-name');
Supported methods
getDirListing($directory, $parameters ), (*12)
Returns a list of files in the given directory, (*13)
-
$directory
: The directory to be listed. Default value: .
.
-
$parameters
: Optional parameters to prefix with directory. For example: -la
. Default: null
.
getDirListingDetailed($directory), (*14)
Returns a list of files in the given directory as an associative array with the following keys:
rights, number, user, group, size, month, day and time, (*15)
-
$directory
: The directory to be listed. Default value: .
.
makeDir($directory), (*16)
Creates the specified directory on the FTP server., (*17)
-
$directory
: The name of the directory that will be created.
changeDir($directory), (*18)
Changes the current directory on a FTP server., (*19)
-
$directory
: The target directory.
uploadFile($fileFrom, $fileTo, $mode), (*20)
Uploads a local file to the FTP server., (*21)
-
$fileFrom
: The local file path.
-
$fileTo
: The remote file path.
-
$mode
: The transfer mode. Must be either FTP_ASCII
or FTP_BINARY
. Automatic mode resolution will be done if no mode is specified.
downloadFile($fileFrom, $fileTo, $mode), (*22)
Downloads a file from the FTP server, (*23)
-
$fileFrom
: The remote file path.
-
$fileTo
: The local file path (will be overwritten if the file already exists) or an open file pointer in which we store the data.
- .
-
$mode
: The transfer mode. Must be either FTP_ASCII
or FTP_BINARY
. Automatic mode resolution will be done if no mode is specified.
readFile($fileFrom), (*24)
Same as the downloadFile()
method except it downloads the remote file to the PHP output buffer and returns it., (*25)
-
$fileFrom
: The remote file path.
moveUp(), (*26)
Changes to the parent directory., (*27)
permission($mode, $filename), (*28)
Set permissions on a file., (*29)
-
$mode
: The new permissions, given as an octal value.
-
$filename
: The remote file.
delete($path), (*30)
Deletes the file specified by path from the FTP server., (*31)
-
$path
: The file to delete.
currentDir(), (*32)
Returns the current directory name, (*33)
rename($oldName, $newName), (*34)
Renames a file or a directory on the FTP server., (*35)
-
$oldName
: The old file/directory name.
-
$newName
: The new name.
removeDir($directory, $recursive), (*36)
Removes a directory, (*37)
-
$directory
: The directory to delete. This must be either an absolute or relative path to an empty directory.
-
$recursive
: Delete the folder recursively. Default value: false.
truncateDir($directory), (*38)
Truncates a directory, (*39)
-
$directory
: The directory to truncate. This must be either an absolute or relative path to a directory.
size($remoteFile), (*40)
Returns the size of the given file in bytes.
Note: Not all servers support this feature.
, (*41)
-
$remoteFile
: The remote file.
time($remoteFile), (*42)
Returns the last modified time of the given file
Note: Not all servers support this feature.
, (*43)
-
$remoteFile
: The remote file.