jeudi 18 janvier 2018

php - rand() update image with the name of the previous one

I have a table CRUD that display my database. When I upload an image in my image folder I generate a random name of numbers with that function rand()

Here is what I have coded :

My upload function

function importer_image()
{
    if(isset($_FILES["image"]))
    {
        $extension = explode('.', $_FILES['image']['name']);
        $new_name = rand() . '.' . $extension[1];
        $destination = './upload/' . $new_name;
        move_uploaded_file($_FILES['image']['tmp_name'], $destination);
        return $new_name;
    }
}

My upload php

if($_POST["operation"] == "Ajouter")
    {
        $image = '';
        if($_FILES["image"]["name"] != '')
        {
            $image = importer_image();
        }

The problem is then when I code the update function, the substitued image stays in my folder and the new one has a new name generated. In order to avoid this, I would like to create a condition that says if $image !='' 1/ erase the old file 2/ upload the new file and keep the same name than the deleted image.

So I'm trying to create an update php process that would 1/ delete unlink() 2/ upload the new image with the name of the previous image.




Aucun commentaire:

Enregistrer un commentaire