In core PHP
<?php$filedname = 'albumart';
$restriction = FALSE;
if(!is_array($_FILES[$filedname]['name'])){
if(isset($_FILES[$filedname]['name']))
{
$fileName=$_FILES[$filedname]['name'];
$fileTmpLoc=$_FILES[$filedname]['tmp_name'];
$fileSize=$_FILES[$filedname]['size'];
$fileType=$_FILES[$filedname]['type'];
$fileErrMsg=$_FILES[$filedname]['error'];
$allowed = array('gif','png' ,'jpg','jpeg');
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
try{
if ($fileSize > 50000)
{
if($restriction==TRUE){
$allowed = array('gif','png' ,'jpg','jpeg');
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed)) {
echo 'Sorry, only JPG, JPEG, PNG & GIF files are allowed.';
}
else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}
else{
echo "Invalid size";
}
}catch(Exception $e){
'oops!..'.$fileName.' Not uploaded......';
}
else
{
echo "File Upload Error";
}
}else{
for($f=0; $f<count($_FILES[$filedname]['name']); $f++){
if(isset($_FILES[$filedname]['name'][$f]))
{
$fileName=$_FILES[$filedname]['name'][$f];
$fileTmpLoc=$_FILES[$filedname]['tmp_name'][$f];
$fileSize=$_FILES[$filedname]['size'][$f];
$fileType=$_FILES[$filedname]['type'][$f];
$fileErrMsg=$_FILES[$filedname]['error'][$f];
try{
if ($fileSize > 50000)
{
if($restriction==TRUE){
$allowed = array('gif','png' ,'jpg','jpeg');
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'Sorry, only JPG, JPEG, PNG & GIF files are allowed.';
}
}
else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}
else{
echo "Invalid size";
}
}catch(Exception $e){
'oops!..'.$fileName.' Not uploaded......';
}
}
}
}
?>
in Codeigniter
class Fileupload extends CI_Controller{
function __consturct(){
parent::__construct();
}
function myupload($restriction = FALSE;){
$filedname = 'albumart';
if(!is_array($_FILES[$filedname]['name'])){
if(isset($_FILES[$filedname]['name']))
{
$fileName=$_FILES[$filedname]['name'];
$fileTmpLoc=$_FILES[$filedname]['tmp_name'];
$fileSize=$_FILES[$filedname]['size'];
$fileType=$_FILES[$filedname]['type'];
$fileErrMsg=$_FILES[$filedname]['error'];
$allowed = array('gif','png' ,'jpg','jpeg');
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if ($fileSize > 50000)
{
if(!in_array($ext,$allowed) ) {
echo 'Sorry, only JPG, JPEG, PNG & GIF files are allowed.';
}
else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}
else{
echo "Invalid size";
}
}
else
{
echo "File Upload Error";
}
}else{
for($f=0; $f<count($_FILES[$filedname]['name']); $f++){
if(isset($_FILES[$filedname]['name'][$f]))
{
$fileName=$_FILES[$filedname]['name'][$f];
$fileTmpLoc=$_FILES[$filedname]['tmp_name'][$f];
$fileSize=$_FILES[$filedname]['size'][$f];
$fileType=$_FILES[$filedname]['type'][$f];
$fileErrMsg=$_FILES[$filedname]['error'][$f];
try{
if ($fileSize > 50000)
{
if($restriction==TRUE){
$allowed = array('gif','png' ,'jpg','jpeg');
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed)) {
echo 'Sorry, only JPG, JPEG, PNG & GIF files are allowed.';
}
else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}else{
$dir="upload";
if(move_uploaded_file($fileTmpLoc,"$dir/$fileName"))
{
$path="upload/$fileName";
echo $path;
}
else
echo "Folder is there, but file not uploaded some error";
}
}
else{
echo "Invalid size";
}
}catch(Exception $e){
'oops!..'.$fileName.' Not uploaded......';
}
}
}
}
}
}
0 comments:
Post a Comment