PHP code example of azurre / php-simple-file-uploader
1. Go to this page and download the library: Download azurre/php-simple-file-uploader library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
azurre / php-simple-file-uploader example snippets
$loader = re\Component\Http\Uploader;
if (isset($_FILES['file'])) {
try {
$uploader = Uploader::create()->upload('file');
} catch (\Exception $e) {
exit("Error: {$e->getMessage()}");
}
echo $uploader->getFirstFile()->getFullPath();
}
if (isset($_FILES['file'])) {
try {
$uploader = Uploader::create()
->setDestination('./')
->setOverwrite(false)// Overwrite existing files?
->setNameFormat(Uploader::NAME_FORMAT_ORIGINAL)
->setReplaceCyrillic(false)// Transliterate cyrillic names
->addValidator(Uploader::VALIDATOR_MIME, ['image/png', 'image/jpeg'])
->addValidator(Uploader::VALIDATOR_EXTENSION, ['png', 'jpg'])
->addValidator(Uploader::VALIDATOR_SIZE, '1M');
// After upload callback
$uploader->afterUpload(function ($file) {
//do something
});
$customData = 'KEY';
// Custom name formatter. If you will use custom formatter setNameFormat() setReplaceCyrillic() will be ignored.
$uploader->setNameFormatter(function ($file, $upl) use ($customData) {
/** @var Uploader\File $file */
/** @var Uploader $upl */
$newName = str_replace(' ', '-', $file->getName());
$newName = Uploader::transliterate($newName);
$newName .= uniqid("_{$customData}_", true) . ".{$file->getExtension()}";
return $newName;
});
$uploader->upload('file');
echo '<pre>' . print_r($uploader->getFiles(), true) . '</pre>';
} catch (\Exception $e) {
echo 'Error:' . $e->getMessage();
}
}
$url = 'https://img.shields.io/github/release/azurre/php-simple-file-uploader.svg?style=flat-square';
try {
$uploader = Uploader::create()->uploadByUrl($url);
echo '<pre>' . print_r($uploader->getFirstFile(), true) . '</pre>';
} catch (\Exception $e) {
echo 'Error:' . $e->getMessage();
}
curl -s https://getcomposer.org/installer | php
composer
/var/www/Test_5cd31dbb246530.38121881.xlsx