First you need to create File Upload form,
Use the following html form,
<form method=”post” action=”” enctype=”multipart/form-data”>
Choose a file to upload : <input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″><input name=”uploadedfile” type=”file”>
</form>
Use the following in the Action PHP script,
if($_POST){
if (!empty($_FILES[‘uploadedfile’][‘name’]){
$imageType = $_FILES[‘uploadedfile’][‘type’];
if($imageType == “image/jpeg” || $imageType == “image/png” || $imageType == “image/gif”) {
$target_path = getcwd() . “/uploads/”;
$randNum = rand(1000, 9999);
$imageName = date(“YmdHis”) . “-” . $randNum . “.jpg”;
$target_path = $target_path . $imageName;
if (move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo “<script type=’text/javascript’>
alert(‘Image uploaded successfully. Click here to continue!’);
window.location = ‘index.php’;
</script>”;
} else {
$cardInsertResponse = “There was an error uploading the file, please try again!”;
}
}
}
If there is a requirement to handle large number of images, we have two options to store the images
- Store the raw images at the Database
- Store images at separate folder and only store the image path at Database
Here we are using the Second option, due to the following reasons,
- Assuming Image of Maximum 100KB, with 10 images we need to have 1 MB of database capacity
- Retrieving an image from a database incurs significant overhead compared to using the file system.
- Disk storage on database is typically more expensive than storage on disks