Skip to content

Commit c819c88

Browse files
authored
Merge pull request #5 from lazzard/fix-ftp-error-handling
Some fixes to handle correctly the FTP server error message
2 parents e0220b0 + f3e7965 commit c819c88

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/FtpClient.php

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,21 @@ public function removeFile($remoteFile)
386386
*/
387387
public function isExists($remoteFile)
388388
{
389-
return @in_array(
389+
/**
390+
* Trying to get the files list of the remote file parent directory, this
391+
* check is basically to avoid passing false to the next 'in_array' function
392+
* below, so we don't want to get an error because of this.
393+
*
394+
* The str_replace because of dirname in windows gives '\' instead of '/'
395+
* if the path matches for example '/foo/'.
396+
*/
397+
if (($list = $this->wrapper->nlist(str_replace('\\', '/', dirname($remoteFile)))) === false) {
398+
return false;
399+
}
400+
401+
return in_array(
390402
basename($remoteFile),
391-
$this->wrapper->nlist(dirname($remoteFile))
403+
$list
392404
);
393405
}
394406

@@ -449,7 +461,8 @@ public function createDirectory($directory)
449461

450462
if (!$this->isExists($dir)) {
451463
if (!$this->wrapper->mkdir($dir)) {
452-
return false;
464+
throw new FtpClientException(FtpClientException::getFtpServerError()
465+
?: 'Unable to create directory ['.$dir.']');
453466
}
454467
}
455468
}
@@ -807,26 +820,26 @@ public function download($remoteFile, $localFile, $resume = true, $mode = FtpWra
807820
public function getTransferMode($fileName)
808821
{
809822
if (
810-
in_array(substr($fileName, strpos($fileName, '.') + 1), [
811-
"3dm", "3ds", "3g2", "3gp", "7z", "a", "aac", "adp", "ai", "aif", "aiff", "alz", "apk", "ape", "ar",
812-
"arj", "asf", "au", "avi", "bak", "baml", "bh", "bin", "bk", "bmp", "btif", "bz2", "bzip2", "cab",
813-
"caf", "cgm", "class", "cmx", "cpio", "cr2", "cur", "dat", "dcm", "deb", "dex", "djvu", "dll", "dmg",
814-
"dng", "doc", "docm", "docx", "dot", "dotm", "dra", "DS_Store", "dsk", "dts", "dtshd", "dvb", "dwg",
815-
"dxf", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot", "epub", "exe", "f4v", "fbs", "fh",
816-
"fla", "flac", "fli", "flv", "fpx", "fst", "fvt", "g3", "gh", "gif", "graffle", "gz", "gzip", "h261",
817-
"h263", "h264", "icns", "ico", "ief", "img", "ipa", "iso", "jar", "jpeg", "jpg", "jpgv", "jpm", "jxr",
818-
"key", "ktx", "lha", "lib", "lvp", "lz", "lzh", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", "mdi", "mht",
819-
"mid", "midi", "mj2", "mka", "mkv", "mmr", "mng", "mobi", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg",
820-
"mpg", "mpga", "mxu", "nef", "npx", "numbers", "nupkg", "o", "oga", "ogg", "ogv", "otf", "pages", "pbm",
821-
"pcx", "pdb", "pdf", "pea", "pgm", "pic", "png", "pnm", "pot", "potm", "potx", "ppa", "ppam", "ppm",
822-
"pps", "ppsm", "ppsx", "ppt", "pptm", "pptx", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar", "ras",
823-
"raw", "resources", "rgb", "rip", "rlc", "rmf", "rmvb", "rtf", "rz", "s3m", "s7z", "scpt", "sgi",
824-
"shar", "sil", "sketch", "slk", "smv", "snk", "so", "stl", "suo", "sub", "swf", "tar", "tbz", "tbz2",
825-
"tga", "tgz", "thmx", "tif", "tiff", "tlz", "ttc", "ttf", "txz", "udf", "uvh", "uvi", "uvm", "uvp",
826-
"uvs", "uvu", "viv", "vob", "war", "wav", "wax", "wbmp", "wdp", "weba", "webm", "webp", "whl", "wim",
827-
"wm", "wma", "wmv", "wmx", "woff", "woff2", "wrm", "wvx", "xbm", "xif", "xla", "xlam", "xls", "xlsb",
828-
"xlsm", "xlsx", "xlt", "xltm", "xltx", "xm", "xmind", "xpi", "xpm", "xwd", "xz", "z", "zip", "zipx"
829-
])
823+
in_array(substr($fileName, strpos($fileName, '.') + 1), [
824+
"3dm", "3ds", "3g2", "3gp", "7z", "a", "aac", "adp", "ai", "aif", "aiff", "alz", "apk", "ape", "ar",
825+
"arj", "asf", "au", "avi", "bak", "baml", "bh", "bin", "bk", "bmp", "btif", "bz2", "bzip2", "cab",
826+
"caf", "cgm", "class", "cmx", "cpio", "cr2", "cur", "dat", "dcm", "deb", "dex", "djvu", "dll", "dmg",
827+
"dng", "doc", "docm", "docx", "dot", "dotm", "dra", "DS_Store", "dsk", "dts", "dtshd", "dvb", "dwg",
828+
"dxf", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot", "epub", "exe", "f4v", "fbs", "fh",
829+
"fla", "flac", "fli", "flv", "fpx", "fst", "fvt", "g3", "gh", "gif", "graffle", "gz", "gzip", "h261",
830+
"h263", "h264", "icns", "ico", "ief", "img", "ipa", "iso", "jar", "jpeg", "jpg", "jpgv", "jpm", "jxr",
831+
"key", "ktx", "lha", "lib", "lvp", "lz", "lzh", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", "mdi", "mht",
832+
"mid", "midi", "mj2", "mka", "mkv", "mmr", "mng", "mobi", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg",
833+
"mpg", "mpga", "mxu", "nef", "npx", "numbers", "nupkg", "o", "oga", "ogg", "ogv", "otf", "pages", "pbm",
834+
"pcx", "pdb", "pdf", "pea", "pgm", "pic", "png", "pnm", "pot", "potm", "potx", "ppa", "ppam", "ppm",
835+
"pps", "ppsm", "ppsx", "ppt", "pptm", "pptx", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar", "ras",
836+
"raw", "resources", "rgb", "rip", "rlc", "rmf", "rmvb", "rtf", "rz", "s3m", "s7z", "scpt", "sgi",
837+
"shar", "sil", "sketch", "slk", "smv", "snk", "so", "stl", "suo", "sub", "swf", "tar", "tbz", "tbz2",
838+
"tga", "tgz", "thmx", "tif", "tiff", "tlz", "ttc", "ttf", "txz", "udf", "uvh", "uvi", "uvm", "uvp",
839+
"uvs", "uvu", "viv", "vob", "war", "wav", "wax", "wbmp", "wdp", "weba", "webm", "webp", "whl", "wim",
840+
"wm", "wma", "wmv", "wmx", "woff", "woff2", "wrm", "wvx", "xbm", "xif", "xla", "xlam", "xls", "xlsb",
841+
"xlsm", "xlsx", "xlt", "xltm", "xltx", "xm", "xmind", "xpi", "xpm", "xwd", "xz", "z", "zip", "zipx"
842+
])
830843
) {
831844
return FtpWrapper::BINARY;
832845
}
@@ -984,7 +997,8 @@ public function createFile($remoteFile, $content = null)
984997
rewind($handle); // Rewind position
985998

986999
if (!$this->wrapper->fput($remoteFile, $handle, FtpWrapper::ASCII)) {
987-
throw new FtpClientException("Failed to create file [{$remoteFile}].");
1000+
throw new FtpClientException(FtpClientException::getFtpServerError() ?:
1001+
"Failed to create file [{$remoteFile}].");
9881002
}
9891003

9901004
return true;

src/FtpWrapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function rmdir($directory)
273273
*/
274274
public function mkdir($directory)
275275
{
276-
return ftp_mkdir($this->connection->getStream(), $directory);
276+
return @ftp_mkdir($this->connection->getStream(), $directory);
277277
}
278278

279279
/**
@@ -392,7 +392,7 @@ public function put($remoteFile, $localFile, $mode, $startPos = 0)
392392
*/
393393
public function fput($remoteFile, $handle, $mode, $startPos = 0)
394394
{
395-
return ftp_fput($this->connection->getStream(), $remoteFile, $handle, $mode, $startPos);
395+
return @ftp_fput($this->connection->getStream(), $remoteFile, $handle, $mode, $startPos);
396396
}
397397

398398
/**

0 commit comments

Comments
 (0)