: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
protected function _joinPath($dir, $name)
$sql = 'SELECT id FROM ' . $this->tbf . ' WHERE parent_id=\'' . $dir . '\' AND name=\'' . $this->db->real_escape_string($name) . '\'';
if (($res = $this->query($sql)) && ($r = $res->fetch_assoc())) {
$this->updateCache($r['id'], $this->_stat($r['id']));
* Return normalized path, this works the same as os.path.normpath() in Python
* @param string $path path
protected function _normpath($path)
* Return file path related to root dir
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _relpath($path)
* Convert path related to root dir into real path
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _abspath($path)
* Return fake path started from root dir
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _path($path)
if (($file = $this->stat($path)) == false) {
$parentsIds = $this->getParents($path);
foreach ($parentsIds as $id) {
$path .= $dir['name'] . $this->separator;
return $path . $file['name'];
* Return true if $path is children of $parent
* @param string $path path to check
* @param string $parent parent path
* @author Dmitry (dio) Levashov
protected function _inpath($path, $parent)
: in_array($parent, $this->getParents($path));
/***************** file stat ********************/
* Return stat for given path.
* Stat contains following fields:
* - (int) size file size in b. required
* - (int) ts file modification time in unix time. required
* - (string) mime mimetype. required for folders, others - optionally
* - (bool) read read permissions. required
* - (bool) write write permissions. required
* - (bool) locked is object locked. optionally
* - (bool) hidden is object hidden. optionally
* - (string) alias for symlinks - link target path relative to root path. optionally
* - (string) target for symlinks - link target path. optionally
* If file does not exists - returns empty array or false.
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _stat($path)
$sql = 'SELECT f.id, f.parent_id, f.name, f.size, f.mtime AS ts, f.mime, f.read, f.write, f.locked, f.hidden, f.width, f.height, IF(ch.id, 1, 0) AS dirs
FROM ' . $this->tbf . ' AS f
LEFT JOIN ' . $this->tbf . ' AS ch ON ch.parent_id=f.id AND ch.mime=\'directory\'
WHERE f.id=\'' . $path . '\'
$res = $this->query($sql);
$stat = $res->fetch_assoc();
if ($stat['id'] == $this->root) {
$this->rootHasParent = true;
if ($stat['parent_id']) {
$stat['phash'] = $this->encode($stat['parent_id']);
if ($stat['mime'] == 'directory') {
unset($stat['parent_id']);
* Return true if path is dir and has at least one childs directory
* @param string $path dir path
* @author Dmitry (dio) Levashov
protected function _subdirs($path)
return ($stat = $this->stat($path)) && isset($stat['dirs']) ? $stat['dirs'] : false;
* Return object width and height
* Usualy used for images, but can be realize for video etc...
* @param string $path file path
* @param string $mime file mime type
* @author Dmitry (dio) Levashov
protected function _dimensions($path, $mime)
return ($stat = $this->stat($path)) && isset($stat['width']) && isset($stat['height']) ? $stat['width'] . 'x' . $stat['height'] : '';
/******************** file/dir content *********************/
* Return files list in directory.
* @param string $path dir path
* @author Dmitry (dio) Levashov
protected function _scandir($path)
return isset($this->dirsCache[$path])
? $this->dirsCache[$path]
: $this->cacheDir($path);
* Open file and return file pointer
* @param string $path file path
* @param string $mode open file mode (ignored in this driver)
* @author Dmitry (dio) Levashov
protected function _fopen($path, $mode = 'rb')
? fopen($this->getTempFile($path), 'w+')
if (($res = $this->query('SELECT content FROM ' . $this->tbf . ' WHERE id=\'' . $path . '\''))
&& ($r = $res->fetch_assoc())) {
fwrite($fp, $r['content']);
$this->_fclose($fp, $path);
* @param resource $fp file pointer
* @author Dmitry (dio) Levashov
protected function _fclose($fp, $path = '')
is_resource($fp) && fclose($fp);
$file = $this->getTempFile($path);
is_file($file) && unlink($file);
/******************** file/dir manipulations *************************/
* Create dir and return created dir path or false on failed
* @param string $path parent dir path
* @param string $name new directory name
* @author Dmitry (dio) Levashov
protected function _mkdir($path, $name)
return $this->make($path, $name, 'directory') ? $this->_joinPath($path, $name) : false;
* Create file and return it's path or false on failed
* @param string $path parent dir path
* @param string $name new file name
* @author Dmitry (dio) Levashov
protected function _mkfile($path, $name)
return $this->make($path, $name, '') ? $this->_joinPath($path, $name) : false;
* Create symlink. FTP driver does not support symlinks.
* @param string $target link target
* @param string $path symlink path
* @author Dmitry (dio) Levashov
protected function _symlink($target, $path, $name)
* Copy file into another file
* @param string $source source file path
* @param string $targetDir target directory path
* @param string $name new file name
* @author Dmitry (dio) Levashov
protected function _copy($source, $targetDir, $name)
$id = $this->_joinPath($targetDir, $name);
? sprintf('REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden`) (SELECT %d, %d, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden` FROM %s WHERE id=%d)', $this->tbf, $id, $this->_dirname($id), $this->tbf, $source)
: sprintf('INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden`) SELECT %d, \'%s\', content, size, %d, mime, width, height, `read`, `write`, `locked`, `hidden` FROM %s WHERE id=%d', $this->tbf, $targetDir, $this->db->real_escape_string($name), time(), $this->tbf, $source);
return $this->query($sql);
* Move file into another parent dir.
* Return new file path or false.
* @param string $source source file path
* @param string $name file name
* @internal param string $target target dir path
* @author Dmitry (dio) Levashov
protected function _move($source, $targetDir, $name)
$sql = 'UPDATE %s SET parent_id=%d, name=\'%s\' WHERE id=%d LIMIT 1';
$sql = sprintf($sql, $this->tbf, $targetDir, $this->db->real_escape_string($name), $source);
return $this->query($sql) && $this->db->affected_rows > 0 ? $source : false;
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _unlink($path)
return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime!=\'directory\' LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
* @param string $path dir path
* @author Dmitry (dio) Levashov
protected function _rmdir($path)
return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime=\'directory\' LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
* @author Dmitry Levashov
protected function _setContent($path, $fp)
* Create new file and write into it from file pointer.
* Return new file path or false on error.
* @param resource $fp file pointer
* @param string $dir target dir path
* @param string $name file name
* @param array $stat file stat (required by some virtual fs)
* @author Dmitry (dio) Levashov
protected function _save($fp, $dir, $name, $stat)
$mime = !empty($stat['mime']) ? $stat['mime'] : $this->mimetype($name, true);
$w = !empty($stat['width']) ? $stat['width'] : 0;
$h = !empty($stat['height']) ? $stat['height'] : 0;
$ts = !empty($stat['ts']) ? $stat['ts'] : time();
$id = $this->_joinPath($dir, $name);
if (!isset($stat['size'])) {
if ($this->isLocalhost && ($tmpfile = tempnam($this->tmpPath, $this->id))) {
if (($trgfp = fopen($tmpfile, 'wb')) == false) {
stream_copy_to_stream($fp, $trgfp);
? 'REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height) VALUES (' . $id . ', ?, ?, LOAD_FILE(?), ?, ?, ?, ?, ?)'
: 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (?, ?, LOAD_FILE(?), ?, ?, ?, ?, ?)';
$stmt = $this->db->prepare(sprintf($sql, $this->tbf));
$path = $this->loadFilePath($tmpfile);
$stmt->bind_param("issiisii", $dir, $name, $path, $size, $ts, $mime, $w, $h);
$res = $this->execute($stmt);
return $id > 0 ? $id : $this->db->insert_id;
$content .= fread($fp, 8192);
? 'REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height) VALUES (' . $id . ', ?, ?, ?, ?, ?, ?, ?, ?)'
: 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
$stmt = $this->db->prepare(sprintf($sql, $this->tbf));
$stmt->bind_param("issiisii", $dir, $name, $content, $size, $ts, $mime, $w, $h);
if ($this->execute($stmt)) {
return $id > 0 ? $id : $this->db->insert_id;
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _getContents($path)
return ($res = $this->query(sprintf('SELECT content FROM %s WHERE id=%d', $this->tbf, $path))) && ($r = $res->fetch_assoc()) ? $r['content'] : false;
* Write a string to a file
* @param string $path file path
* @param string $content new file content
* @author Dmitry (dio) Levashov
protected function _filePutContents($path, $content)
return $this->query(sprintf('UPDATE %s SET content=\'%s\', size=%d, mtime=%d WHERE id=%d LIMIT 1', $this->tbf, $this->db->real_escape_string($content), strlen($content), time(), $path));
* Detect available archivers
protected function _checkArchivers()
protected function _chmod($path, $mode)
* @param string $path archive path
* @param array $arc archiver command and arguments (same as in $this->archivers)