├── .gitignore ├── README.md ├── composer.json └── src ├── LaravelFtp └── FTP.php └── helpers.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor 3 | src/tests 4 | composer.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LaravelFtp 2 | Laravel FTP client (or any other project that utilises has composer) 3 | 4 | This project is just called LaravelFtp because I am holding this package to the conventions Laravel itself uses, they are great and easy. 5 | 6 | It also utilises the collection package, files are returned as collection so you can use the Collection functions from Laravel. 7 | 8 | # Usage 9 | 10 | ## Installation 11 | 12 | Require this package with composer: 13 | 14 | ``` 15 | composer require dennissmink/laravel-ftp dev-master 16 | ``` 17 | 18 | ## Usage 19 | 20 | Initiate a new FTP client like so: 21 | 22 | ``` 23 | $ftp = ftp($host, $user, $pass, $optionalport); 24 | ``` 25 | 26 | Then, check if your connection was succesfull: 27 | 28 | ``` 29 | if(!$ftp){ 30 | // Connection failed 31 | return 'Error while connecting to the FTP server'; 32 | } 33 | 34 | // Do your stuff 35 | ``` 36 | 37 | **Methods** 38 | 39 | *General file functions* 40 | ``` 41 | $ftp->all(); // Returns all the files of the users root files (Collection) 42 | $ftp->all('folder'); // Returns all the files of the directory folder (Collection) 43 | 44 | $ftp->get('filename.txt') // Returns the content of the file, can also be: get('directory/filename.txt') 45 | $ftp->save('filename.txt', 'content file'); // Save file 'filename.txt' with content 'content file', returns content if success 46 | $ftp->rename('oldname.txt', 'newname.txt'); // Renames file or directory 47 | ``` 48 | 49 | 50 | *Create files/directories* 51 | ``` 52 | $ftp->createFile('filename.txt'); // Creates a file with the name 'filename.txt' 53 | $ftp->createDirectory('directory name'); // Creates a directory 'directory name' 54 | ``` 55 | 56 | *Delete files/directories* 57 | ``` 58 | $ftp->deleteFile('filename.txt'); // Deletes a file with the name 'filename.txt' 59 | $ftp->deleteDirectory('directory name'); // Removes a directory with the name 'directory name' (And its contents..) 60 | $ftp->emptyDirectory('directory name'); // Emptys a directory but keeps the directory itself 61 | ``` 62 | 63 | 64 | ## Links 65 | 66 | Packagist: https://packagist.org/packages/dennissmink/laravel-ftp 67 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dennissmink/laravel-ftp", 3 | "authors": [ 4 | { 5 | "name": "Dennis Smink", 6 | "email": "dennissmink@gmail.com" 7 | } 8 | ], 9 | "require": { 10 | "php": ">=5.4.0", 11 | "illuminate/support": "^5.0" 12 | }, 13 | "minimum-stability": "stable", 14 | "autoload": { 15 | "psr-0": { 16 | "LaravelFtp": "src/" 17 | }, 18 | "files": [ 19 | "src/helpers.php" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/LaravelFtp/FTP.php: -------------------------------------------------------------------------------- 1 | mode = $mode; 25 | 26 | if ($this->connection = @ftp_connect($host, ($port != 21) ? $port : 21)) { 27 | 28 | try { 29 | @ftp_login($this->connection, $user, $pass); 30 | } catch (\Exception $e) { 31 | throw new \Exception($e->getMessage()); 32 | } 33 | ftp_pasv($this->connection, true); 34 | return true; 35 | 36 | } 37 | 38 | throw new \Exception('Unable to establish connection to FTP server'); 39 | } 40 | 41 | /** 42 | * public function all 43 | * 44 | * 45 | * @param string $directory 46 | * 47 | * @return string 48 | */ 49 | public function all($directory = '') 50 | { 51 | $files = @ftp_nlist($this->connection, $directory); 52 | 53 | if (!$files) { 54 | $files = []; 55 | } 56 | 57 | return collect($files); 58 | } 59 | 60 | /** 61 | * public function size 62 | * 63 | * 64 | * @param string $file 65 | * 66 | * @return string 67 | */ 68 | public function size($file = '') 69 | { 70 | return @ftp_size($this->connection, $file); 71 | } 72 | 73 | /** 74 | * public function get 75 | * 76 | * 77 | * @param string $file 78 | * 79 | * @return string 80 | */ 81 | public function get($file = '') 82 | { 83 | $tempHandle = fopen('php://temp', 'r+'); 84 | $sizeFile = $this->size($file); 85 | if ($sizeFile > 512000) { // 512 000 KB 86 | return 'This file is too big to read, maximum filesize allowed to the browser: 512KB'; 87 | } else { 88 | if (@ftp_fget($this->connection, $tempHandle, $file, $this->mode, 0)) { 89 | rewind($tempHandle); 90 | $total = stream_get_contents($tempHandle); 91 | return $total; 92 | } else { 93 | return false; 94 | } 95 | } 96 | 97 | return false; 98 | } 99 | 100 | /** 101 | * public function download 102 | * 103 | * 104 | * @param string $localFile 105 | * @param string $remoteFile 106 | * @param int $maxSize 107 | * 108 | * @return string 109 | */ 110 | public function download($localFile = '', $remoteFile = '', $maxSize = 512000) 111 | { 112 | $sizeFile = $this->size($remoteFile); 113 | if ($sizeFile > $maxSize) { // 512 000 KB 114 | return 'This file is too big to read, maximum filesize allowed to the browser: 512KB'; 115 | } 116 | 117 | if (@ftp_get($this->connection, $localFile, $remoteFile, $this->mode, 0)) { 118 | return true; 119 | } 120 | 121 | return false; 122 | } 123 | 124 | /** 125 | * public function save 126 | * 127 | * @param $file 128 | * @param $content 129 | * 130 | * @return bool 131 | */ 132 | public function save($file, $content) 133 | { 134 | $tempHandle = fopen('php://temp', 'r+'); 135 | fwrite($tempHandle, $content); 136 | rewind($tempHandle); 137 | 138 | if (@ftp_fput($this->connection, $file, $tempHandle, $this->mode, 0)) { 139 | return $this->get($file); 140 | } else { 141 | return false; 142 | } 143 | } 144 | 145 | /** 146 | * public function rename 147 | * 148 | * @param $old 149 | * @param $new 150 | * 151 | * @return bool 152 | */ 153 | public function rename($old, $new) 154 | { 155 | return @ftp_rename($this->connection, $old, $new); 156 | } 157 | 158 | /** 159 | * public function directory 160 | * 161 | * @return string 162 | */ 163 | public function directory() 164 | { 165 | return ftp_pwd($this->connection); 166 | } 167 | 168 | /** 169 | * public function createFile 170 | * 171 | * @param $directory 172 | * @param $name 173 | * 174 | * @return bool 175 | */ 176 | public function createFile($directory, $name) 177 | { 178 | $temp = tmpfile(); 179 | 180 | return @ftp_fput($this->connection, $directory . '/' . $name, $temp, $this->mode); 181 | } 182 | 183 | /** 184 | * public function deleteFile 185 | * 186 | * @param $file 187 | * 188 | * @return bool 189 | */ 190 | public function deleteFile($file) 191 | { 192 | return @ftp_delete($this->connection, str_replace('../', '', $file)); 193 | } 194 | 195 | /** 196 | * Public function createDirectory 197 | * 198 | * @param $directory 199 | * 200 | * @return boolean 201 | * @throws \Exception 202 | */ 203 | public function createDirectory($directory) 204 | { 205 | @ftp_mkdir($this->connection, $directory); 206 | 207 | return true; 208 | } 209 | 210 | /** 211 | * Check if $directory actually is a directory 212 | * 213 | * @param $directory 214 | * 215 | * @return bool 216 | */ 217 | public function isDirectory($directory) 218 | { 219 | if (ftp_nlist($this->connection, $directory)) { 220 | return true; 221 | } 222 | 223 | return false; 224 | } 225 | 226 | /** 227 | * @param $path 228 | * 229 | * @return bool 230 | */ 231 | public function createDirectoryRecursive($path) 232 | { 233 | $dir = explode("/", $path); 234 | $path = ""; 235 | $ret = true; 236 | 237 | for ($i = 0; $i < count($dir); $i++) { 238 | $path .= "/" . $dir[$i]; 239 | if (!@ftp_chdir($this->connection, $path)) { 240 | @ftp_chdir($this->connection, "/"); 241 | if (!@ftp_mkdir($this->connection, $path)) { 242 | $ret = false; 243 | break; 244 | } 245 | } 246 | } 247 | return $ret; 248 | } 249 | 250 | /** 251 | * Public function deleteDirectory 252 | * 253 | * @param $directory 254 | * 255 | * @return bool 256 | */ 257 | public function deleteDirectory($directory) 258 | { 259 | if (!$directory) { 260 | return false; 261 | } 262 | 263 | $dst_dir = preg_replace('/\\/\$/', '', $directory); 264 | $ar_files = $this->all($dst_dir); 265 | 266 | if ($ar_files) { 267 | foreach ($ar_files as $st_file) { 268 | if (!empty($st_file)) { 269 | $fl_file = $dst_dir . '/' . $st_file; 270 | 271 | $getExtensions = pathinfo($fl_file); 272 | 273 | if (!array_key_exists('extension', $getExtensions)) { 274 | $this->deleteDirectory($fl_file); // Folder 275 | } else { 276 | $this->deleteFile($fl_file); // File 277 | } 278 | } 279 | } 280 | 281 | $delete = @ftp_rmdir($this->connection, $dst_dir); 282 | 283 | if ($delete) { 284 | return true; 285 | } 286 | } 287 | 288 | return false; 289 | } 290 | 291 | /** 292 | * public function emptyDirectory 293 | * 294 | * @param $directory 295 | * 296 | * @return bool 297 | */ 298 | public function emptyDirectory($directory) 299 | { 300 | if (!$directory) { 301 | return false; 302 | } 303 | 304 | $dst_dir = preg_replace('/\\/\$/', '', $directory); 305 | $ar_files = $this->all($dst_dir); 306 | 307 | if ($ar_files) { 308 | foreach ($ar_files as $st_file) { 309 | if (!empty($st_file)) { 310 | $fl_file = $dst_dir . '/' . $st_file; 311 | 312 | $getExtensions = pathinfo($fl_file); 313 | 314 | if (!array_key_exists('extension', $getExtensions)) { 315 | $this->deleteDirectory($fl_file); // Folder 316 | } else { 317 | $this->deleteFile($fl_file); // File 318 | } 319 | } 320 | } 321 | 322 | return true; 323 | } 324 | 325 | return false; 326 | } 327 | 328 | /** 329 | * public function uploadFile 330 | * 331 | * 332 | * @param string $fileToUpload 333 | * @param string $fileUrl 334 | * 335 | * @return boolean 336 | */ 337 | public function uploadFile($fileToUpload, $fileUrl) 338 | { 339 | if (@ftp_put($this->connection, $fileToUpload, $fileUrl, $this->mode)) { 340 | return true; 341 | } else { 342 | return false; 343 | } 344 | } 345 | 346 | } 347 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 |