/** * 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) * @return bool|string * @author Dmitry (dio) Levashov **/ protected function _save($fp, $dir, $name, $stat) { if ($name === '') { return false; } $this->clearcache(); $id = $this->_joinPath($dir, $name); if ($id > 0) { $this->rmTmb($stat); } rewind($fp); $fstat = fstat($fp); if (is_array($fstat) && isset($fstat['size'])) { $size = $fstat['size']; } else { $size = 0; } $time = time(); $gid = 0; $uid = (int) $this->x_uid; $umask = $this->getUmask($dir, $gid); $perm = $this->getDefaultPerm($umask); $gids = join(',', $this->getGroupsByUid($uid)); $cut = $_SERVER['REQUEST_METHOD'] == 'POST' ? !empty($_POST['cut']) : !empty($_GET['cut']); $local_path = !$cut && is_array($stat) && !empty($stat['_localpath']) ? $stat['_localpath'] : ''; $mime = $stat['mime']; $w = $stat['width']; $h = $stat['height']; $sql = $id > 0 ? 'REPLACE INTO %s (`file_id`, `parent_id`, `name`, `size`, `ctime`, `mtime`, `perm`, `umask`, `uid`, `gid`, `mime`, `width`, `height`, `gids`, `local_path`) VALUES (' . $id . ', %d, %s, %d, %d, %d, "%s", "%s", %d, %d, "%s", %d, %d, "%s", %s)' : 'INSERT INTO %s (`parent_id`, `name`, `size`, `ctime`, `mtime`, `perm`, `umask`, `uid`, `gid`, `mime`, `width`, `height`, `gids`, `local_path`) VALUES (%d, %s, %d, %d, %d, "%s", "%s", %d, %d, "%s", %d, %d, "%s", %s)'; $sql = sprintf($sql, $this->tbf, (int) $dir, $this->db->quoteString($name), $size, $time, $time, $perm, $umask, $uid, $gid, $mime, $w, $h, $gids, $this->db->quoteString($local_path)); if ($this->query($sql)) { if ($id < 1) { $id = $this->db->getInsertId(); } if ($local_path) { return $id; } if ($local = $this->readlink($id, true)) { if ($target = @fopen($local, 'wb')) { while (!feof($fp)) { fwrite($target, fread($fp, 8192)); } fclose($target); if ($this->mimeDetect === 'internal' && strpos($mime, 'image') === 0 && !getimagesize($local)) { $this->_unlink($id); return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME); } if ($this->options['autoResize'] && strpos($mime, 'image') === 0 && max($w, $h) > $this->options['autoResize']) { if ($this->imgResize($local, $this->options['autoResize'], $this->options['autoResize'], true, true)) { clearstatcache(); $size = filesize($local); list($width, $height) = getimagesize($local); $sql = 'UPDATE %s SET width=%d, height=%d, size=%d WHERE file_id=%d LIMIT 1'; $sql = sprintf($sql, $this->tbf, $width, $height, $size, $id); $this->query($sql); } } if ($size === 0) { clearstatcache($local); $size = filesize($local); $sql = 'UPDATE %s SET size=%d WHERE file_id=%d LIMIT 1'; $sql = sprintf($sql, $this->tbf, $size, $id); $this->query($sql); } $this->updateDirTimestamp($dir, $time, true); return $id; } else { $this->_unlink($id); } } } return false; }