Em làm một trang upload nhưng khi upload một file lên vidu hoahong.jpg thì sau đó link download có dạng MD5:
PHP Code:
class FileUploader {
private $fileTypesAllowed;
private $fileSizeMax;
private $uploadDir;
private $files;
/*
Function: Create FileUploader object
Arguments:
$fileTypesAllowed: types of files allowed, separated by commas(,)
$fileSizeMax: max size of file upload in Kb
$uploadDir: path to directory where files go to, ended with trailing slash (/)
*/
function __construct($fileTypesAllowed = 'jpg, jpeg, gif, png', $fileSizeMax = 1024, $uploadDir = 'files/') {
$this->fileTypesAllowed = strtolower($fileTypesAllowed);
$this->fileSizeMax = $fileSizeMax;
$this->uploadDir = $uploadDir;
}
/*
Function: Show form upload
*/
function showFormUpload() {
echo '
';
}
/*
Function: Get file upload and set file name and file type
*/
function getFilesUpload($files) {
$j = 0;
$this->files = array();
for ($i = 0; $i < count($files['name']); $i++) {
if (trim($files['name'][$i]) != '') {
$this->files[$j]['name'] = trim($files['name'][$i]);
$this->files[$j]['tmp_name'] = $files['tmp_name'][$i];
$this->files[$j]['error'] = $files['error'][$i];
$this->files[$j]['type'] = $files['type'][$i];
$this->files[$j]['ext'] = strtolower(substr($this->files[$j]['name'], strrpos($this->files[$j]['name'], '.') + 1));
$this->files[$j]['newName'] = $this->uploadDir.md5_file($this->files[$j]['tmp_name']).'.'.$this->files[$j]['ext'];
$j++;
}
}
}
/*
Function: Get total files upload
*/
function getTotalFiles() {
return count($this->files);
}
/*
Function: Check error while uploading
*/
function checkUploadProcess($i) {
switch ($this->files[$i]['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo '
return false;
case UPLOAD_ERR_PARTIAL:
echo '
return false;
}
return true;
}
/*
Function: Check file type
*/
function checkFileType($i) {
if (strpos($this->fileTypesAllowed, $this->files[$i]['ext']) === false) {
echo '
return false;
}
return true;
}
/*
Function: Move file to destination folder
*/
function moveFile($i) {
if (!@file_exists($this->files[$i]['newName'])) {
if (!@move_uploaded_file($this->files[$i]['tmp_name'], $this->files[$i]['newName'])) {
echo '
return false;
} else {
echo '
}
} else {
echo '
}
return true;
}
/*
Function: Return link to uploaded file
*/
function getLink($i) {
$path = dirname($_SERVER['PHP_SELF']);
if ($path == '' || $path == '/' || $path == '\\') {
$path = '';
}
$link = 'http://'.$_SERVER['SERVER_NAME'].$path.'/'.$this->files[$i]['newName'];
$return = "
";
return $return;
}
}
?>
Image Uploader
You must be registered for see links
. Làm thế nào để nó có dạng
You must be registered for see links
ạ. Code gốc nó thế này đâyPHP Code:
class FileUploader {
private $fileTypesAllowed;
private $fileSizeMax;
private $uploadDir;
private $files;
/*
Function: Create FileUploader object
Arguments:
$fileTypesAllowed: types of files allowed, separated by commas(,)
$fileSizeMax: max size of file upload in Kb
$uploadDir: path to directory where files go to, ended with trailing slash (/)
*/
function __construct($fileTypesAllowed = 'jpg, jpeg, gif, png', $fileSizeMax = 1024, $uploadDir = 'files/') {
$this->fileTypesAllowed = strtolower($fileTypesAllowed);
$this->fileSizeMax = $fileSizeMax;
$this->uploadDir = $uploadDir;
}
/*
Function: Show form upload
*/
function showFormUpload() {
echo '
';
}
/*
Function: Get file upload and set file name and file type
*/
function getFilesUpload($files) {
$j = 0;
$this->files = array();
for ($i = 0; $i < count($files['name']); $i++) {
if (trim($files['name'][$i]) != '') {
$this->files[$j]['name'] = trim($files['name'][$i]);
$this->files[$j]['tmp_name'] = $files['tmp_name'][$i];
$this->files[$j]['error'] = $files['error'][$i];
$this->files[$j]['type'] = $files['type'][$i];
$this->files[$j]['ext'] = strtolower(substr($this->files[$j]['name'], strrpos($this->files[$j]['name'], '.') + 1));
$this->files[$j]['newName'] = $this->uploadDir.md5_file($this->files[$j]['tmp_name']).'.'.$this->files[$j]['ext'];
$j++;
}
}
}
/*
Function: Get total files upload
*/
function getTotalFiles() {
return count($this->files);
}
/*
Function: Check error while uploading
*/
function checkUploadProcess($i) {
switch ($this->files[$i]['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo '
File '.$this->files[$i]['name'].' is too big, please choose again
'; return false;
case UPLOAD_ERR_PARTIAL:
echo '
Error while uploading file '.$this->files[$i]['name'].' , please try again
'; return false;
}
return true;
}
/*
Function: Check file type
*/
function checkFileType($i) {
if (strpos($this->fileTypesAllowed, $this->files[$i]['ext']) === false) {
echo '
File type of file '.$this->files[$i]['name'].' is not allowed, please choose again
'; return false;
}
return true;
}
/*
Function: Move file to destination folder
*/
function moveFile($i) {
if (!@file_exists($this->files[$i]['newName'])) {
if (!@move_uploaded_file($this->files[$i]['tmp_name'], $this->files[$i]['newName'])) {
echo '
Cannot save file '.$this->files[$i]['name'].' on server, try again
'; return false;
} else {
echo '
File '.$this->files[$i]['name'].' has been uploaded OK
'; }
} else {
echo '
File '.$this->files[$i]['name'].' already exists
'; }
return true;
}
/*
Function: Return link to uploaded file
*/
function getLink($i) {
$path = dirname($_SERVER['PHP_SELF']);
if ($path == '' || $path == '/' || $path == '\\') {
$path = '';
}
$link = 'http://'.$_SERVER['SERVER_NAME'].$path.'/'.$this->files[$i]['newName'];
$return = "
Link to file: | |
BBCode: |
return $return;
}
}
?>
Dich vu tai len$fileUploader = new FileUploader('jpg, jpeg, gif, png, txt, exe, rar, doc, xls, mp3', 5000, 'files/'); $fileUploader->showFormUpload(); if (isset($_POST['submit'])) { $fileUploader->getFilesUpload($_FILES['filesUpload']); if ($fileUploader->getTotalFiles() > 0) { for ($i = 0; $i < $fileUploader->getTotalFiles(); $i++) { if ($fileUploader->checkUploadProcess($i) && $fileUploader->checkFileType($i) && $fileUploader->moveFile($i)) { echo $fileUploader->getLink($i); } } } else { echo ' Please choose at least one file before upload '; } } ?> |