How to save an image with image validation in new folder in php?

If you want to save an image on a new folder which will create on run time and with image validation ,
then simply copy and paste this code and change the name of image field name and you can replace $id to your folder name.


$id=$_POST['id'];
$last_url = $_SERVER['HTTP_REFERER'];
if(!empty($_FILES['uploadedfile']['name'])){
$imgExtension = array("jpg","jpe","jpeg","gif","png","GIF","JPG","JPEG");
$image_name = pathinfo($_FILES['uploadedfile']['name']);
$extension = strtolower($image_name['extension']);
if(in_array($extension,$imgExtension)){

$file_name = $_FILES['uploadedfile']['name'];
$ext = end(explode('.',$file_name));
$folder_path = 'public/uploads/auction/'.$id;
$save_path  = 'public/uploads/auction/'.$id.'/'.$file_name;
$path  = 'public/uploads/auction/'.$id.'/'.$file_name;
if(!is_dir($folder_path)){
mkdir($folder_path, 0777, "True");
chmod($folder_path , 0777);
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $save_path))
{
$this->auction_model->updateImg($id,$save_path);
$this->session->set_flashdata('PopUpsuccess', 'Image has been updates');
redirect($last_url);
}
}
else
{
$this->session->set_flashdata('PopUpsuccess', 'Please upload image file only');
redirect($last_url);
}
}else{
$this->session->set_flashdata("PopUpsuccess","There was an error uploading the file. Please try again!");
redirect($last_url);
}

Comments