File size: 1.32Kb
<?php
function uploadImage($file) {
$target_dir = "uploads/"; // specify your uploads directory
$target_file = $target_dir . basename($file["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($file["tmp_name"]);
if ($check === false) {
echo "File is not an image.";
return null;
}
// Check file size (you can set the maximum size according to your requirements)
if ($file["size"] > 500000) {
echo "Sorry, your file is too large.";
return null;
}
// Allow certain file formats
$allowedFormats = ["jpg", "jpeg", "png", "gif"];
if (!in_array($imageFileType, $allowedFormats)) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
return null;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
return null;
}
// if everything is ok, try to upload file
if (move_uploaded_file($file["tmp_name"], $target_file)) {
return $target_file; // return the path where the image was saved
} else {
echo "Sorry, there was an error uploading your file.";
return null;
}
}
?>