We can now upload an image and associate it to an article and the images are stored in the uploads folder
here.
What happens though if I upload another image to this article that already has one.
The image is uploaded.
The record in the database is updated and the new image is displayed.
However the previous image file is still in the uploads folder.
So we need to delete this file.
If we successfully replace it with another image.
So first let's assign the current value of the image file attributes to a variable.
We do this because we're about to update this value on the next line so to get the previous value we
want to do it before it's overwritten.
Then if the update of the article record is successful and if the previous image value isn't NULL we
can delete the previous image the on Link function in P P is used to delete a file from the file system.
So will call this function passing in the path of the file in the uploads folder.
While we're here as were now showing the image file on this page we can redirect to the same page.
So the newly uploaded image will be displayed.
Let's give that a try.
Here's an article with an image.
And here's the image file in the uploads folder.
If I update the image to a different one.
The new image is displayed and the old image file has been deleted.