-1

So iv been trying to upload an image into my SQL database and display it on a diffrent page, the display part works fine as long as I upload it manually via the database (type longblob) but if i try to store it using the <label for="image">image</label> <input type="file" name="image" id="image" /> whenever i try to display it i get the error if an empty square which means im not storing anything. The thing is i have found a solution, since every one is doing it with the addslashes(file_get_contents($_FILES['image']['tmp_name'])); but i dont know how to implement it. In the few ways i tried it i get a "Unidentified array key ['image']EVERYTIME. so here is my code (its separated on 2 different pages, ill just show the functions) and i really need your help on this one.
This is addpromo.php:

*testing if everything isset and not empty* {
        $promotion = new promotion(
    
            $_POST['id_artiste'], 
            $_POST['id_produit'],
            $_POST['nom'],
            $_POST['reduction'],
            $_POST['duree'],
            $_POST['description'],
            $_POST['image'],
        );
        $promotionC->ajouterpromotion($promotion);
        header('Location:afficherpromo.php');
    }
    else
        $error = "Missing information";
}

And this is The insert function in PromoC.php:

        function ajouterpromotion($promotion) {
        $sql = "INSERT INTO promotion (id_artiste, id_produit, nom, reduction, duree, description, image) VALUES (:id_artiste, :id_produit, :nom, :reduction, :duree, :description, :image)";
        $db = config::getConnexion();
        try{
            $query = $db->prepare($sql);

            $query->execute([
                'id_artiste'=>$promotion->getida(),
                'id_produit' => $promotion->getprod(),
                'nom' => $promotion->getno(),
                'reduction' => $promotion->getred(),
                'duree'     => $promotion->getdur(),
                'description'=> $promotion->getdesc(),
                'image'     => $promotion->getima(),
            ]);
        }
        catch(Exception $e) {
            echo 'Erreur: '.$e->getMessage();
        }
    }

Everything works exept for the image

Latenz
  • 1
  • 1
  • It's `$_FILES['image']` for uploaded files, not `$_POST['image']`. https://www.php.net/manual/en/features.file-upload.post-method.php – brombeer Apr 29 '21 at 14:13
  • i tried that too and it didnt work, do i need to make some other changes elsewhere maybe, cus in my code i declared image as a string since there was no blob `function getima(): string { return $this->image; }` – Latenz Apr 29 '21 at 14:16
  • Do you want to store the complete image or only its name/path? Make sure the file is uploaded, then use `$_FILES['image']['name']` to get its filename. – brombeer Apr 29 '21 at 14:17
  • the error for $_FILES['image'] is Warning: Undefined array key "image" and for $_FILES['image']['name'] is the same plus Warning: Trying to access array offset on value of type null – Latenz Apr 29 '21 at 17:52
  • Does your `
    ` element use the required `enctype="multipart/form-data"` attribute? Is the file uploaded at all?
    – brombeer Apr 29 '21 at 18:47
  • i thought about that and for some reason when i add it the whole thing stops working, when i hit submit it just refreshes the page now. Is it mandatory for image storage? – Latenz Apr 29 '21 at 19:37
  • It's mandatory for file uploads to work, yes. From the link in my 1st comment: "_Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work._" I'd recommend getting your uploads working first, then deal with the database stuff. Might be interesting: [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) – brombeer Apr 29 '21 at 19:49
  • OK, i implemented `enctype="multipart/form-data"` and the `$_FILES['image']['tmp_name']` i even tried the addslashes() and file_get_contents() and it goes through i dont get any errors like i used to but the image is still not uploading. it still draws blank. Can you think of anything else that might causes the problem ? – Latenz Apr 29 '21 at 22:24
  • I finally got it to work. And thank you very much for your help <3, i changed the type in the database to varshar and started working the paths and `$img = $_FILES['image']['name']; $img_loc = $_FILES['image']['tmp_name']; $img_folder = "display/";` and it was much simpler – Latenz Apr 29 '21 at 23:41
  • Nice, glad it works – brombeer Apr 30 '21 at 06:22

0 Answers0