Reputation: 199
I have this table:
+----+------------+-----------------------+
| ID | ID_PRODUCT | LINK_DOWNLOAD |
+----+------------+-----------------------+
| 1 | 2369 | folder/2015.03.12.pdf |
| 2 | 3694 | folder/2014.01.10.pdf |
| 3 | 56 | folder/2016.09.25.pdf |
+----+------------+-----------------------+
End code php:
<form action="upload_file.php" method="post" enctype="multipart/form-data" name="upload_file"><br>
<label for="UploadFileField"></label><br>
<input type="file" name="UploadFileField"/>
<input type="submit" name="UploadButton" value="Upload" /><br>
<input type ="text" name="UPLOAD_COD" />
</form>
<?php
include "bd_cnx.php";
if (isset ($_FILES['UploadFileField'])){
$UploadName = $_FILES['UploadFileField'] ['name'];
$UploadName = mt_rand (100000, 999999).$UploadName;
$UploadTmp = $_FILES['UploadFileField'] ['tmp_name'];
$UploadType = $_FILES['UploadFileField'] ['type'];
$FileSize = $_FILES['UploadFileField'] ['size'];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 1250000)){
die ("Error - File to big");
}
if(!$UploadTmp) {
die ("No File Selected");
}
else {
if (move_uploaded_file($UploadTmp, "Upload/$UploadName")) {
$UPLOAD_COD = $_POST['UPLOAD_COD'];
$sql = "INSERT INTO download (ID_PRODUCT,LINK_DOWNLOAD) VALUES ('$UPLOAD_COD','Upload/$UploadName')";
$result = $conn->query($sql);
echo '<script> alert("Successfully inserted"); window.location ="/index.php";</script>';
}
}
}
?>
2015.03.12.pdf file is today updating and becomes 2016.09.25.pdf . When i enter in the input "UPLOAD_COD" the number 2369, I want to delete the file 2015.03.12.pdf from server and be replaced with 2016.09.25.pdf, and in the LINK_DOWNLOAD column to be updated to the new link (folder/2015.03.12.pdf). Thank you!
Upvotes: 1
Views: 95
Reputation: 4153
So you want to delete the file with ID_PRODUCT = 2369? just fetch it's LINK_DOWNLOAD from database
You put this code after uploading and inserting your file
//first create a query that select the row of the data that you are finding
$currentSql = "Select * from download where ID_PRODUCT = '".$UPLOAD_COD."'";
$current = $conn->query($currentSql);
// then fetch it's result then unlink the file from the server
while($row = $current->fetch_assoc()) {
$file = $row['LINK_DOWNLOAD'];
// check if file is exist then delete
if (file_exists($file)) {
unlink($file);
}
}
Upvotes: 1
Reputation: 434
You have to update the corresponding entry in your database :
UPDATE download SET LINK_DOWNLOAD='folder/2016.09.25.pdf' WHERE ID=1
I'm using the WHERE
criteria on the ID
column because you didn't tell if there was only one entry per product. If there is only one entry per product, the LINK_DOWNLOAD
would also be right in the product table.
By looking at your table, you may have multiple download links for one product since it seems to be a linked table, so if you want to update one entry, maybe you want to update only this entry.
So in your code, I would add a block to look for an existing entry for the product_id input in UPLOAD_COD
.
Then, if you're updating a link to a physical file, you have to remove/archive the initial file. So, you can look at the file_exists php command, as weel as this SO question.
And to finish, I would process an update only if there is an existing entry for this product (and maybe you have other criterias, or at least, you can add others later if needed).
Hope this helps !
Upvotes: 1
Reputation: 4153
just add delete query after insert query is executed
$deleteSql = "Delete from download where ID_PRODUCT = '".$UPLOAD_COD."'";
$conn->query($deleteSql);
Upvotes: 1