You can see on successful upload and insertion of record into table, we will display message to user. On failure also we will display reason for the user to correct it.
Following checks are performed at backend script and matching reply is posted to user .
Product name should have Alphabet , number and space. No special chars are allowed.
Price should be float ( 22.34 )
Image must be jpg or gif type, file size should be less than 250KB.
if(!filter_var($price,FILTER_VALIDATE_FLOAT)){
$elements['msg'].=" Enter Price details <br>";
$elements['validation_status']="F";
}
Check Product name
if(!ctype_alpha(str_replace(' ', '', $p_name)) === true){
$elements['msg'].=" Enter Product Name without Special Chars <br>";
$elements['validation_status']="F";
}
Check file type ( only Gif and Jpg are allowed )
if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif"))
{$elements['msg'].="Your uploaded file must be of JPG or GIF. ";
$elements['msg'].="Other file types are not allowed<BR>";
$elements['validation_status']="F"; // Set the flag to F
}
Check file size ( upto 250 KB )
if ($_FILES[file_up][size]>250000){
$elements['msg'].="Your uploaded file size is more than 250KB ";
$elements['msg'].=" so reduce the file size and then upload.<BR>";
$elements['validation_status']="F"; // Set the flag to F
}
If all above validations are cleared then we will go for uploading the file.
$file_name=$_FILES[file_up][name];// Name of the file upload
if($elements['validation_status']=="T"){
// the path with the file name where the file will be stored
$add="images/$file_name";
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
/// Other code to store record in database
}
}
After uploading we will insert one record to MySQL database table with Product name, price and file name.
Each stage we will be setting the validation status to T or F and adding a message about the success or failure of the process for the user .
Finally we will use JSON to post back the message to user.
echo json_encode($elements);
Downloading Zip file
Download the zip file and use this instructions to run the file.
How to Install and test
Download the zip file at the end of this page. plus2_db_images_v2.zip