mod_upload_image function
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
function mod_upload_image($request, $postName = 'img', $defaultPath = 'front/user/')
{
$imgURL = '';
$target_name = '';
if (isset($_FILES['img']['name']) && $_FILES[$postName]['name'] != '') {
$allowed = array('gif', 'png', 'jpg');
$filename = $_FILES[$postName]['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$target_name = time() . rand(1111, 9999) . '-' . $filename;
$target_file = $defaultPath . $target_name;
if (!in_array($ext, $allowed)) {
echo 'Please upload valid image';
exit;
}
$check = getimagesize($_FILES[$postName]["tmp_name"]);
if ($check !== false) {
} else {
echo 'Please upload valid image';
exit;
}
if (move_uploaded_file($_FILES[$postName]["tmp_name"], $target_file)) {
}
}
return $target_name;
}