Prestashop file upload throwing error but images get to upload

60 Views Asked by At

This is giving me a pain in the brain.

if(!empty($_FILES)){
        $files = restructureFilesArray($_FILES)["bulkImages"];
        $i=0;
        $media = new Media(); 
        $result["images"] = [];
        $result["html"] = '';
        foreach($files as $file){ 
            $target_file = $target_dir . basename($file["name"]);
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
            if(file_exists($file["tmp_name"])) {
                
                $check = getimagesize($file["tmp_name"]);

                if(!is_dir($target_dir)){
                    mkdir($target_dir, 0777);
                }

                if(file_exists($target_file)){
                    unlink($target_file);
                } 
                                
                if($check != false && is_uploaded_file($file["tmp_name"])) { 
                    try {
                        // Upload the file using the Media class method
                        #$moved = $media->uploadFile($file["tmp_name"], $target_dir);
                        $moved= @move_uploaded_file($file["tmp_name"], $target_file);
                        chmod($target_file, 0777);
                        $result["images"][] = $target_file;
                        echo "File uploaded and moved successfully.";
                    } catch (Exception $e) {
                        $result["html"] .= "Sorry, there was an error uploading your file." . $e->getMessage();
                    } 
                } else {
                    $result["html"] .= "File is not an image.";
                }
            }   
            $i++;

        } 
        #autoAssignImages();
        return $result;
    }else{
        return ["html"=>"Sem ficheiros para upload!"];
    }

This part of my code, which gets $_FILES and copies them with move_uploaded_file(), But returns me the first image tmp_name as an error:

The file "/tmp/php615eX3" does not exist after that error, if I check my upload folder, the files are there, and as this being the first tmp_name from the list of files uploaded, it should stop all others from upload, this case looks like the function is being executed twice, but it is not, I already checked that the function is only running once, what can be making PHP throw this error?. is it a prestashop's feature?

I have already used try catch, is_uploaded_file(), and even '@' suppressors, but it keeps throwing the error, this is not making sense.

Thank you in advance if someone can give me some lights on this.

0

There are 0 best solutions below