cvetan
cvetan

Reputation: 403

file_exists returns different results for the same condition in two places

I have some upload form, and file_exists returns different results for the same condition in two places in the same request. Here is the example code bellow.

$flag = file_exists($_FILES['image']['name']); // return TRUE
move_uploaded_file(
     $_FILES['image']['tmp_name'],
     'uploads/' . $_FILES['image']['name']
);

require_once APPPATH . 'custom_classes/ImageResize.class.php';

$img_resize = new ImageResize($_FILES['image']['tmp_name']); // here is the Exception thrown
$img_resize->set_resize_dimensions(650, 451);
$img_resize->crop_image();
$img_resize->save_image('uploads/cropped.jpg');
$img_resize->free_resourses();

Here is the class Constructor that throws Exception.

public function __construct($filepath)
{
    if (!file_exists($filepath)) // same condition as above, throws Exception
    {
        throw new Exception('File not found!');
    }

    $this->get_source_dimensions($filepath);
    $this->load_source_img($filepath);
}

It drives me crazy. I could pass temp path from the filesystem, but i am pretty certain that this code worked before, now it gives me this. Am i doing something wrong?

Upvotes: 0

Views: 41

Answers (2)

zoubida13
zoubida13

Reputation: 1759

Once you moved the file then it's not in the old location anymore, it's a "move" not a "copy", therefore :

$flag = file_exists($_FILES['image']['name']); // return TRUE
$newlocation = 'uploads/'.$_FILES['image']['name'];
move_uploaded_file(
     $_FILES['image']['tmp_name'],
     $newlocation
);

require_once APPPATH . 'custom_classes/ImageResize.class.php';

$img_resize = new ImageResize($newlocation); // no more Exception thrown!
$img_resize->set_resize_dimensions(650, 451);
$img_resize->crop_image();
$img_resize->save_image('uploads/cropped.jpg');
$img_resize->free_resourses();

should work better for you. (not quite sure why you have tmp_name and name but I assume you know)

Upvotes: 0

James Hunt
James Hunt

Reputation: 2528

The reason it's saying the file doesn't exist is because it doesn't exist there anymore.

You are moving the file from the tmp_name location into the uploads folder, then looking for it at the tmp_name location you just moved it out of.

Upvotes: 3

Related Questions