I’m attempting to do an file upload form for when you update the system but when you click the submit button, if you have not added any files then it wont change the database row to a blank and just keep the previous value in it.
Currently i have all the file uploads working but if you dont add any files then it just changes the database rows to blank so does anyone know a way how i can do this without having to do separate forms for each file upload field.
My code
<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
require_once('../system/config-admin.php');
/*if(!empty($_POST['updatedata']))
{*/
if(!empty($_FILES['previewimgfile']['name'])){
if(isset($_FILES['previewimgfile'])){
$image_name = $_FILES['previewimgfile']['name'];
$image_name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $image_name);
$tmp_name = $_FILES['previewimgfile']['tmp_name'];
$image_type = $_FILES['previewimgfile']['type'];
$image_size = $_FILES['previewimgfile']['size'];
$allowed_image = array('image/png', 'image/PNG', 'image/jpeg', 'image/JPEG');
if(!in_array($image_type, $allowed_image)){
echo '<span class="text-danger">Please select a jpg/png for preview img!</span>';
die();
}
else
{
if($image_size > 5767168){
echo'file too big';
die();
}
else{
$new_image_name = time().$image_name;
move_uploaded_file($tmp_name, '../system/assets/uploads/products/'.$new_image_name.'');
}
}
}
}
if(!empty($_FILES['iconimgfile']['name'])){
if(isset($_FILES['iconimgfile'])){
$aimage_name = $_FILES['iconimgfile']['name'];
$aimage_name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $aimage_name);
$tmp_name = $_FILES['iconimgfile']['tmp_name'];
$aimage_type = $_FILES['iconimgfile']['type'];
$aimage_size = $_FILES['iconimgfile']['size'];
$aallowed_image = array('image/png', 'image/PNG', 'image/jpg', 'image/JPG');
if(!in_array($aimage_type, $aallowed_image)){
echo '<span class="text-danger">Please select a jpg/png for icon img!</span>';
die();
}
else
{
if($aimage_size > 5767168){
echo'file too big';
die();
}
else{
$new_image_name1 = time().$aimage_name;
move_uploaded_file($tmp_name, '../system/assets/uploads/products/'.$new_image_name1.'');
}
}
}
}
if(!empty($_FILES['mainfile']['name'])){
if(isset($_FILES['mainfile'])){
$name = $_FILES['mainfile']['name'];
$name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $name);
$tmp_name = $_FILES['mainfile']['tmp_name'];
$file_size = $_FILES['mainfile']['size'];
if($file_size < 1){
echo 'No file selcted try again!';
die();
}
$file_type = pathinfo($name);
$file_type = $file_type['extension'];
$allowed_file = array('zip','jpg');
if(!in_array($file_type, $allowed_file)){
echo '<span class="text-danger">Please select a .zip file for the main file</span>';
}
else
{
$new_file_name = time().$name;
move_uploaded_file(''.$tmp_name.'', '../system/assets/uploads/product-files/'.$new_file_name.'');
}
}
}
/*}else{
echo '<span class="text-danger">You need to add atleast one file!</span>';
die();
}*/
$id = $_POST['id'];
$sql_upload = $DB_con->prepare("UPDATE dsptesty_products SET icon_img=:icon_img, preview_img=:preview_img, file=:file WHERE id=:id");
$sql_upload->bindparam(":icon_img",$new_image_name1);
$sql_upload->bindparam(":preview_img",$new_image_name);
$sql_upload->bindparam(":file",$new_file_name);
$sql_upload->bindparam(":id",$id);
if($sql_upload->execute()){
echo '<span class="text-success">Product Files Updated!</span>';
}
else{
echo "Error: " . $sql_upload->error;
}
}
else {
header('location: ../index.php');
}
?>
My File Upload Form Code
<div class="my-3 p-3 bg-white rounded box-shadow">
<form id="upload1" class="form-horizontal">
<div class="form-group"> <label>Main File (.ZIP):</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="mainfile" class="custom-file-input" id="inputGroupFile04">
<label class="custom-file-label" for="inputGroupFile04">Choose File</label>
</div>
</div>
</div>
<div class="form-group"> <label>Icon Image File (.PNG,.JPG):</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="iconimgfile" class="custom-file-input" id="inputGroupFile04">
<label class="custom-file-label" for="inputGroupFile04">Choose File</label>
</div>
</div>
</div>
<div class="form-group"> <label>Preview Image File (.PNG,.JPG):</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="previewimgfile" class="custom-file-input" id="inputGroupFile04">
<label class="custom-file-label" for="inputGroupFile04">Choose File</label>
</div>
</div>
</div>
<hr>
<input class="form-control" type="hidden" name="id" value="<?php echo $productDetails['id'];?>">
<button type="submit" id="btn" class="btn btn-primary w-100">Update Files</button>
<script type="text/javascript">
$("#upload1").on("submit",(function(e) {
e.preventDefault();
$.ajax({
url: "<?php echo $setting['website_url'];?>/admin/ajax-update-files.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
beforeSend: function()
{
$("#res1").html('Updating..Please wait!');
},
success: function(response)
{
$("#res1").html(response);
}
});
}));
</script>
<div id="res1"></div>
</form> </div>
Thanks very much for help in advance!