Copy a file from source to destination. If unique == true, then if
the destination exists, it will be renamed by appending an increamenting
counting number.
public copyFile ( string $source, string $destination_dir, string $destination_file, boolean $unique = true ) : string | ||
$source | string | where the file is from, full path to the files required |
$destination_dir | string | where the files, just the destination dir, e.g., /www/html/gallery/ |
$destination_file | string | name of the new file, just the filename |
$unique | boolean | create unique destination file if true. |
return | string | the new copied filename, else error if anything goes bad. |
/** * Process upload files. The file must be an * uploaded file. If 'validate_images' is set to * true, only images will be processed. Any duplicate * file will be renamed. See Files::copyFile for details * on renaming. * @param string $relative the relative path where the file * should be copied to. * @param array $file the uploaded file from $_FILES * @return boolean true if the file was processed successfully, * false otherwise */ function _processFiles($relative, $file) { if ($file['error'] != 0) { return false; } if (!is_file($file['tmp_name'])) { return false; } if (!is_uploaded_file($file['tmp_name'])) { Files::delFile($file['tmp_name']); return false; } if ($this->config['validate_images'] == true) { $imgInfo = @getImageSize($file['tmp_name']); if (!is_array($imgInfo)) { Files::delFile($file['tmp_name']); return false; } } $valid_extensions = $this->config['allowed_image_extensions']; $afruext = strtolower(substr(strrchr($file['name'], "."), 1)); if (!in_array($afruext, $valid_extensions)) { Files::delFile($file['tmp_name']); return 'Cannot upload $extension=' . $afruext . '$ Files. Permission denied.'; } //now copy the file $path = Files::makePath($this->getImagesDir(), $relative); $result = Files::copyFile($file['tmp_name'], $path, $file['name']); //no copy error if (!is_int($result)) { Files::delFile($file['tmp_name']); return true; } //delete tmp files. Files::delFile($file['tmp_name']); return false; }
/** * Process upload files. The file must be an * uploaded file. If 'validate_images' is set to * true, only images will be processed. Any duplicate * file will be renamed. See Files::copyFile for details * on renaming. * @param string $relative the relative path where the file * should be copied to. * @param array $file the uploaded file from $_FILES * @return boolean true if the file was processed successfully, * false otherwise */ function _processFiles($relative, $file) { if ($file['error'] != 0) { return false; } if (!is_file($file['tmp_name'])) { return false; } if (!is_uploaded_file($file['tmp_name'])) { Files::delFile($file['tmp_name']); return false; } if ($this->config['validate_images'] == true) { $imgInfo = @getImageSize($file['tmp_name']); if (!is_array($imgInfo)) { Files::delFile($file['tmp_name']); return false; } } //now copy the file $path = Files::makePath($this->getImagesDir(), $relative); $result = Files::copyFile($file['tmp_name'], $path, $file['name']); //no copy error if (!is_int($result)) { Files::delFile($file['tmp_name']); return true; } //delete tmp files. Files::delFile($file['tmp_name']); return false; }
/** * copy a directory and all subdirectories and files (recursive) * @author SBoisvert at Don'tSpamMe dot Bryxal dot ca (adapted from php.net) * @author Raimund Meyer * @param string base path * @param string source directory * @param string destination directory * @param bool overwrite existing files * * @return mixed bool true on pass, number on fail */ function copyDir($basePath, $source, $dest, $overwrite = false) { if (!is_dir($basePath . $dest)) { if (!@mkdir($basePath . $dest)) { return FILE_ERROR_DST_DIR_FAILED; } } if ($handle = opendir($basePath . $source)) { // if the folder exploration is sucsessful, continue while (($file = readdir($handle)) !== false) { // as long as storing the next file to $file is successful, continue if ($file != '.' && $file != '..') { $path = $source . '/' . $file; if (is_file($basePath . $path)) { /*if(!is_file($basePath . $dest . '/' . $file) || $overwrite) { if(!@copy($basePath . $path, $basePath . $dest . '/' . $file)) { return FILE_ERROR_COPY_FAILED; } }*/ Files::copyFile($basePath . $path, $basePath . $dest . '/', $file, true); } elseif (is_dir($basePath . $path)) { if (!is_dir($basePath . $dest . '/' . $file)) { mkdir($basePath . $dest . '/' . $file); // make subdirectory before subdirectory is copied Files::copyDir($basePath, $path, $dest . '/' . $file, $overwrite); //recurse! } } } } closedir($handle); } return true; }
function processPaste() { switch ($_GET['paste']) { case 'copyFile': $src = Files::makeFile($this->getImagesDir(), $_GET['srcdir'] . $_GET['file']); $file = $_GET['file']; $dest = Files::makeFile($this->getImagesDir(), $_GET['dir']); return Files::copyFile($src, $dest, $file); break; case 'copyDir': $basePath = $this->getImagesDir(); $src = $_GET['srcdir'] . $_GET['file']; $dest = $_GET['dir'] . $_GET['file']; return Files::copyDir($basePath, $src, $dest); break; case 'moveFile': $src = Files::makePath($this->getImagesDir(), $_GET['srcdir'] . $_GET['file']); $dest = Files::makePath($this->getImagesDir(), $_GET['dir'] . $_GET['file']); return Files::rename($src, $dest); break; case 'moveDir': $src = Files::makeFile($this->getImagesDir(), $_GET['srcdir'] . $_GET['file']); $dest = Files::makeFile($this->getImagesDir(), $_GET['dir'] . $_GET['file']); return Files::rename($src, $dest); break; } }
/** * Process upload files. The file must be an * uploaded file. If 'validate_images' is set to * true, only images will be processed. Any duplicate * file will be renamed. See Files::copyFile for details * on renaming. * @param string $relative the relative path where the file * should be copied to. * @param array $file the uploaded file from $_FILES * @return boolean true if the file was processed successfully, * false otherwise */ function _processFiles($relative, $file) { if ($file['error'] != 0) { return false; } if (!is_file($file['tmp_name'])) { return false; } if (!is_uploaded_file($file['tmp_name'])) { Files::delFile($file['tmp_name']); return false; } if ($this->config['validate_images'] == true) { $imgInfo = @getImageSize($file['tmp_name']); if (!is_array($imgInfo)) { Files::delFile($file['tmp_name']); return false; } } //now copy the file $path = Files::makePath($this->getBaseDir(), $relative); $result = Files::copyFile($file['tmp_name'], $path, $file['name']); //no copy error if (!is_int($result)) { $dimensionsIndex = isset($_REQUEST['uploadSize']) ? $_REQUEST['uploadSize'] : 0; // If maximum size is specified, constrain image to it. if ($this->config['maxWidth'][$dimensionsIndex] > 0 && $this->config['maxHeight'][$dimensionsIndex] > 0) { $img = Image_Transform::factory(IMAGE_CLASS); $img->load($path . $result); // image larger than max dimensions? if ($img->img_x > $this->config['maxWidth'][$dimensionsIndex] || $img->img_y > $this->config['maxHeight'][$dimensionsIndex]) { $percentage = min($this->config['maxWidth'][$dimensionsIndex] / $img->img_x, $this->config['maxHeight'][$dimensionsIndex] / $img->img_y); $img->scale($percentage); } $img->save($path . $result); $img->free(); } } //delete tmp files. Files::delFile($file['tmp_name']); return false; }
/** * Process upload files. The file must be an * uploaded file. If 'validate_images' is set to * true, only images will be processed. Any duplicate * file will be renamed. See Files::copyFile for details * on renaming. * @param string $relative the relative path where the file * should be copied to. * @param array $file the uploaded file from $_FILES * @return boolean true if the file was processed successfully, * false otherwise */ function _processFiles($relative, $file) { global $_course; if ($file['error'] != 0) { return false; } if (!is_file($file['tmp_name'])) { return false; } if (!is_uploaded_file($file['tmp_name'])) { Files::delFile($file['tmp_name']); return false; } $file['name'] = replace_dangerous_char($file['name'], 'strict'); $file_name = $file['name']; $extension = explode('.', $file_name); $count = count($extension); if ($count == 1) { $extension = ''; } else { $extension = strtolower($extension[$count - 1]); } // Checking for image by file extension first, using the configuration file. if (!in_array($extension, $this->config['accepted_extensions'])) { Files::delFile($file['tmp_name']); return false; } // Second, filtering using a special function of the system. $result = filter_extension($file_name); if ($result == 0 || $file_name != $file['name']) { Files::delFile($file['tmp_name']); return false; } // Checking for a valid image by reading binary file (partially in most cases). if ($this->config['validate_images']) { $imgInfo = @getImageSize($file['tmp_name']); if (!is_array($imgInfo)) { Files::delFile($file['tmp_name']); return false; } } //now copy the file $path = Files::makePath($this->getBaseDir(), $relative); $result = Files::copyFile($file['tmp_name'], $path, $file['name']); //no copy error if (!is_int($result)) { if (isset($_course) && !empty($_course) && isset($_course['code'])) { //adding the document to the DB global $to_group_id; // looking for the /document/ folder $document_path = substr($path, strpos($path, '/document/') + 9, strlen($path)); // /shared_folder/4/name $document_path .= $result; $chamiloFile = $file['name']; $chamiloFileSize = $file['size']; if (!empty($group_properties['directory'])) { $chamiloFolder = $group_properties['directory'] . $chamiloFolder; } $doc_id = add_document($_course, $document_path, 'file', $chamiloFileSize, $chamiloFile); api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', api_get_user_id(), $to_group_id, null, null, null, api_get_session_id()); } $dimensionsIndex = isset($_REQUEST['uploadSize']) ? $_REQUEST['uploadSize'] : 0; // If maximum size is specified, constrain image to it. if ($this->config['maxWidth'][$dimensionsIndex] > 0 && $this->config['maxHeight'][$dimensionsIndex] > 0) { $img = Image_Transform::factory(IMAGE_CLASS); $img->load($path . $result); // image larger than max dimensions? if ($img->img_x > $this->config['maxWidth'][$dimensionsIndex] || $img->img_y > $this->config['maxHeight'][$dimensionsIndex]) { $percentage = min($this->config['maxWidth'][$dimensionsIndex] / $img->img_x, $this->config['maxHeight'][$dimensionsIndex] / $img->img_y); $img->scale($percentage); } $img->save($path . $result); $img->free(); } } // Delete tmp files. Files::delFile($file['tmp_name']); return false; }
/** * Process upload files. The file must be an * uploaded file. Any duplicate * file will be renamed. See Files::copyFile for details * on renaming. * @param string $relative the relative path where the file * should be copied to. * @param array $file the uploaded file from $_FILES * @return boolean true if the file was processed successfully, * false otherwise */ function _processFiles($relative, $file) { if ($file['error'] != 0) { return false; } if (!is_file($file['tmp_name'])) { return false; } if (!is_uploaded_file($file['tmp_name'])) { Files::delFile($file['tmp_name']); return false; } $valid_extensions = $this->mode == 'image' ? $this->config['allowed_image_extensions'] : $this->config['allowed_link_extensions']; $max_size = $this->mode == 'image' ? $this->config['max_filesize_kb_image'] : $this->config['max_filesize_kb_link']; $afruext = strtolower(substr(strrchr($file['name'], "."), 1)); if (!in_array($afruext, $valid_extensions)) { Files::delFile($file['tmp_name']); return "Cannot upload ." . $afruext . " Files. Permission denied."; } if ($file['size'] > $max_size * 1024) { Files::delFile($file['tmp_name']); return "Unble to upload file. Maximum file size [" . $max_size . "Kb] exceeded."; } if (!empty($this->config['max_foldersize_mb']) && Files::dirSize($this->getImagesDir()) + $file['size'] > $this->config['max_foldersize_mb'] * 1048576) { Files::delFile($file['tmp_name']); return "Cannot upload. Maximum folder size reached. Delete unwanted files and try again."; } //now copy the file $path = Files::makePath($this->getImagesDir(), $relative); $result = Files::copyFile($file['tmp_name'], $path, $file['name']); //no copy error if (!is_int($result)) { Files::delFile($file['tmp_name']); return $file['name'] . " successfully uploaded."; } //delete tmp files. Files::delFile($file['tmp_name']); return false; }