It is a very easy way to uploading image in php folder with particular location and allowed minimum size of image and also with particular extension.
1. upload.php
2. create upload folder in root directory
/// upload.php
if(isset($_POST['submit']))
{
1. upload.php
2. create upload folder in root directory
/// upload.php
if(isset($_POST['submit']))
{
$fileName = $_FILES['file']['name'];
$targetDir = 'upload/';
$targetDir = 'upload/';
$targetDir = $targetDir . basename($fileName);
$fileExtension = strtolower(pathinfo($targetDir, PATHINFO_EXTENSION));
$fileSize = $_FILES['file']['size'];
try {
if($fileExtension != 'jpg' && $fileExtension != 'jpeg' && $fileExtension != 'png') {
echo "sorry file format not allowed";
}
if($fileSize > 1000000) {
echo "file size not allowed more than 1 MB";
}
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetDir)) {
echo "successfully uploaded";
}
else{
echo "Error for uploading file";
}
} catch (Exception $e) {
echo $e;
}
}
?>
$fileSize = $_FILES['file']['size'];
try {
if($fileExtension != 'jpg' && $fileExtension != 'jpeg' && $fileExtension != 'png') {
echo "sorry file format not allowed";
}
if($fileSize > 1000000) {
echo "file size not allowed more than 1 MB";
}
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetDir)) {
echo "successfully uploaded";
}
else{
echo "Error for uploading file";
}
} catch (Exception $e) {
echo $e;
}
}
?>
Comments
Post a Comment