Reputation: 65
I have a page that can upload files into the server's folder and write reference of the files in MySQL. Right now I am able to view and download the files through clicking the links.
But I also want to delete the files from server's directory as well as from the database so that the filename never shows up in the display page. I have a font awesome trash icon and I want the current file to be deleted once I click on the icon.
I tried with unlink
and unset
method and used the filepath as argument, but it always shows errors:
Warning: unlink(uploads/75-horse.jpg): No such file or directory in C:\wamp64\www\mysite\upload-page.php on line 214
MyCode:
<tbody>
<?php
$i = 1;
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['filename']; ?></td>
<td><a href="uploads/<?php echo $row['filename']; ?>" target="_blank">View</a></td>
<td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</a></td>
<td>
<!--trying to delete the file: starts-->
<a href="?delete=1">
<i class="fa fa-trash-o" style="color:red"></i>
</a>
<?php
$current_file_name = $row['filename'];
$file_path = "uploads/$current_file_name";
if (isset($_GET['delete'])) {
unlink($file_path);
}
?>
<!--trying to delete the file: ends-->
</td>
</tr>
<?php } ?>
</tbody>
Could anyone please help me figure this out why such error occurs as well as best practice of deleting a file in PHP?
Update after two hours of trying: Finally this trick help me to delete the file without displaying any error/warning.
if (isset($_GET['delete'])) {
if (is_file($_SERVER['mywebsite.se/'] . 'uploads/' . $current_file_name)) {
unlink($_SERVER['mywebsite.se/'] . 'uploads/' . $current_file_name);
}
}
However, although it removes the file from the directory, the web page still displays the file name with the delete icon. Could anyone please guide me how could I entirely take away the row so that the deleted file never appears again?
Upvotes: 0
Views: 379
Reputation: 78
I don't jave knowledge of php but I did same thing using jQuery and ajax,JSP
I create one java page for delete clicked delete button of that file row I called through ajax on button clicked and passed filename in data aprameter in ajax.
I think this part you did it
And for remove from webpage that 'tr' I use jQuery on delete button click
$(this).closest(tr).remove() so that time tr removed from your webpage
And when you refresh,
your page create only number of tr which is stored in your database (deleted one is not displayed)
Upvotes: 0
Reputation: 289
Could you try using the path from document root?
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $current_file_name);
For the second part of your question, it looks as though you are creating the rows based on the data coming from an SQL query. To stop the row being created you would also need to delete the data from the table in SQL so that the row is completely removed. This would look like the following if you use a prepared statement.
if (is_file($_SERVER['mywebsite.se/'] . 'uploads/' . $current_file_name)) {
unlink($_SERVER['mywebsite.se/'] . 'uploads/' . $current_file_name);
$stmt = $connection->prepare("DELETE FROM tableName WHERE ID = ?");
$stmt->bind_param('s', $id);
$result = $stmt->execute();
$stmt->close();
}
Upvotes: 1