CodeIgniter Forums
Problem with move() and file exists (SOLVED) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Problem with move() and file exists (SOLVED) (/showthread.php?tid=78017)



Problem with move() and file exists (SOLVED) - neoneeco - 11-19-2020

English:
(sorry for my bad english)
Hello,
I have a problem with the file exists function, after a file move(). This one tells me each time the file does not exist
Cannot then resize an uploaded image, however the image is well saved in the local directory, so the file exists function should output "true"
Does somebody have an idea ? Thanks in advance

French :
Bonjour,
J'ai un probleme avec la fonction file exists , après un move() de fichiers. Celle ci m'indique a chaque fois que le fichier n'existe pas
Impossible ensuite de redimensionner une image uploadé , pourtant l'image est bien enregistrée dans le repertoire local, la fonction file exists devrait sortir "true"
Quelqu'un a t-il une idée ? Merce d'avance 


PHP Code:
private function manageFiles($files$id$thumb false) {
                        
        $db              
= \Config\Database::connect();
        $builderImages   $db->table('images');

        foreach($files['theFile'] as $file) {
            if($file->isValid() && !$file->hasMoved()) {

                $newName $file->getRandomName();
                $file->move(WRITEPATH.'uploads'$newName);
 
                 
                $dataImages 
= [
                    'name' => $newName,
                    'galeries_id'  => $id,
                ];
                $builderImages->insert($dataImages);

                $image_path base_url() . '/uploads/' .$newName;

                if (!file_exists($image_path)) :
                    echo $image_path;
                    echo 'wrong_path';     // ==> Always !!
                endif;

                if($thumb) {
                    
                    $image_dest 
'"'.base_url() . '/uploads/330x200_' .$newName.'"';
                    $return $this->resize_img($image_path$image_dest$max_size 330$quality 80);
                    echo $return;
                }

            }
        
        
}
    



RE: Problem with move() and file exists - InsiteFX - 11-19-2020

PHP Code:
// just before the if statement.
echo $image_path;
exit(); 

See if your getting the correct path.


RE: Problem with move() and file exists - neoneeco - 11-20-2020

Thank you for your reply.

For the following source code:

PHP Code:
    private function manageFiles($files$id$thumb false)
    {

        $db              = \Config\Database::connect();
        $builderImages   $db->table('images');

        foreach ($files['theFile'] as $file) {
            if ($file->isValid() && !$file->hasMoved()) {

                $newName $file->getRandomName();
                $file->move('./uploads/'$newName);

                $dataImages = [
                    'name' => $newName,
                    'galeries_id'  => $id,
                ];
                $builderImages->insert($dataImages);

                $image_path base_url() . '/uploads/' $newName;

                // just before the if statement.
                echo $image_path;
                exit();

                if (!file_exists($image_path)) :
                    echo $image_path;
                    echo 'wrong_path';     // ==> Always !!
                endif;

                if ($thumb) {

                    $image_dest '"' base_url() . '/uploads/330x200_' $newName '"';
                    $return $this->resize_img($image_path$image_dest33080'auto'true);
                    echo $return;
                }
            }
        }
    

It give me the correct image path, this path points to an image, the browser finds it when I try to search for it with.
As the following screenshot shows:


[Image: kxn6.jpg]
In this topic, we can read the following: file exists doesn't work with http address

https://stackoverflow.com/questions/6930150/file-exists-returns-false-but-the-file-does-exist


RE: Problem with move() and file exists - InsiteFX - 11-20-2020

CodeIgniter 4 User Guide - Uploaded Files


RE: Problem with move() and file exists - neoneeco - 11-20-2020

Hi,
I found the problem: this is to use $ _SERVER['DOCUMENT_ROOT'] instead of base_url(), because the function does not accept HTTP requests and neither does the resize function. Solved !


RE: Problem with move() and file exists (SOLVED) - superior - 11-22-2020

(11-20-2020, 02:28 PM)neoneeco Wrote: Hi,
I found the problem: this is to use $ _SERVER['DOCUMENT_ROOT'] instead of base_url(), because the function does not accept HTTP requests and neither does the resize function. Solved !

Isn't it a better idea to use the core constants provided in CI4?

> https://codeigniter.com/user_guide/general/common_functions.html#core-constants


RE: Problem with move() and file exists (SOLVED) - neoneeco - 11-23-2020

The both are working