I am using this.form.submit(); in order to upload an image straight after it is sellected and also pass some other info along.
I am having an issue, the submit works and passes the id with post but the image is not uploaded and the name is not inserted in the db it might add a name like 0_ but that is incorrect as well as the image name does not exist in the table so it should pass the if(file_exists) without any loops.
Any ideas on that?
<form id='image_upload' name='image_upload' action='customer_logo.php' method='post'> <input type="hidden" name='id' id='id' value='<?php echo $cust_id; ?>'> <input type='text' style="background-color:#93CD60; border-radius: 5px; color:white; font-size:17px; width:140px; text-align:center; cursor:pointer;" id='image_text' name='image_text' value="Add Logo"> </input> <input type='file' name='image' id='image' style='visibility: hidden' accept="image/*" onchange="this.form.submit();" /> </form>
And the php code is the following:
if (isset ($_POST['image'])) { $cust_id = $_POST['id']; $images = $_FILES['image']['name']; $tmp_dir = $_FILES['image']['tmp_name']; $imageSize = $_FILES['image']['size']; if (!is_dir('img/logotipa/'.$cust_id)){ mkdir('img/logotipa/'.$cust_id, 0777, true); } $upload_dir = 'img/logo/'.$cust_id.'/'; $imgExt = strtolower(pathinfo($images,PATHINFO_EXTENSION)); $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); $up_image = $images; if (file_exists($upload_dir.$images)){ $counter = 0; while (file_exists($upload_dir.$up_image)){ $up_image = $counter.'_'.$images; $counter++; } move_uploaded_file($tmp_dir, $upload_dir.$images); $stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)'); $stmt->bindParam(':ucid', $cust_id); $stmt->bindParam(':upic', $up_image); if ($stmt->execute()){ echo '<script>window.location.href = "customer_logo.php";</script>'; } } }
Advertisement
Answer
It really was something stupid that I didn’t see as I mentioned in my comment above… If you check closely the if(file_exists) you can see that the stmt is inside it and well… there wasn’t an else to upload so if an image was unique nothing happened so I just coppied the move_uploaded_file and the rest and added an else{ and that fixed the issue.
if (isset ($_POST['image'])) { $cust_id = $_POST['id']; $images = $_FILES['image']['name']; $tmp_dir = $_FILES['image']['tmp_name']; $imageSize = $_FILES['image']['size']; if (!is_dir('img/logotipa/'.$cust_id)){ mkdir('img/logotipa/'.$cust_id, 0777, true); } $upload_dir = 'img/logo/'.$cust_id.'/'; $imgExt = strtolower(pathinfo($images,PATHINFO_EXTENSION)); $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); $up_image = $images; if (file_exists($upload_dir.$images)){ $counter = 0; while (file_exists($upload_dir.$up_image)){ $up_image = $counter.'_'.$images; $counter++; } move_uploaded_file($tmp_dir, $upload_dir.$images); $stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)'); $stmt->bindParam(':ucid', $cust_id); $stmt->bindParam(':upic', $up_image); if ($stmt->execute()){ echo '<script>window.location.href = "customer_logo.php";</script>'; } }else{ move_uploaded_file($tmp_dir, $upload_dir.$images); $stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)'); $stmt->bindP aram(':ucid', $cust_id); $stmt->bindParam(':upic', $up_image); if ($stmt->execute()){ echo '<script>window.location.href = "customer_logo.php";</script>'; } }