Skip to content

Commit

Permalink
Merge pull request #5 from lazzard/fix-ftp-error-handling
Browse files Browse the repository at this point in the history
Some fixes to handle correctly the FTP server error message
  • Loading branch information
AmraniCh authored Aug 17, 2020
2 parents e0220b0 + f3e7965 commit c819c88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
62 changes: 38 additions & 24 deletions src/FtpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,21 @@ public function removeFile($remoteFile)
*/
public function isExists($remoteFile)
{
return @in_array(
/**
* Trying to get the files list of the remote file parent directory, this
* check is basically to avoid passing false to the next 'in_array' function
* below, so we don't want to get an error because of this.
*
* The str_replace because of dirname in windows gives '\' instead of '/'
* if the path matches for example '/foo/'.
*/
if (($list = $this->wrapper->nlist(str_replace('\\', '/', dirname($remoteFile)))) === false) {
return false;
}

return in_array(
basename($remoteFile),
$this->wrapper->nlist(dirname($remoteFile))
$list
);
}

Expand Down Expand Up @@ -449,7 +461,8 @@ public function createDirectory($directory)

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

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

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/FtpWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function rmdir($directory)
*/
public function mkdir($directory)
{
return ftp_mkdir($this->connection->getStream(), $directory);
return @ftp_mkdir($this->connection->getStream(), $directory);
}

/**
Expand Down Expand Up @@ -392,7 +392,7 @@ public function put($remoteFile, $localFile, $mode, $startPos = 0)
*/
public function fput($remoteFile, $handle, $mode, $startPos = 0)
{
return ftp_fput($this->connection->getStream(), $remoteFile, $handle, $mode, $startPos);
return @ftp_fput($this->connection->getStream(), $remoteFile, $handle, $mode, $startPos);
}

/**
Expand Down

0 comments on commit c819c88

Please sign in to comment.