PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] CakePHP 3: How to check if file or image exists

 February 02, 2022     cakephp, cakephp-3.0     No comments   

Issue

I am just trying to check if an image exists or not, I can do it by using PHP. For example:-

$file = WWW_ROOT .'uploads' . DS . 'employee' . DS .'_'.check.jpg;

$file_exists = file_exists($file);

It's working fine for me. But I have tried also tried using elementExists like this:-

if($this->elementExists("../".$employees->front_image))
{
   echo $this->Html->image("../".$employees->front_image); // image output fine without condition.
}

// Here $employees->front_image = uploads/employee/employeename.jpg

This check is not working. How can I do this in CakePHP?


Solution

CakePHP is written in PHP, so if you already have a simple solution like file_exists() use that. So you can write something like this:-

if (file_exists(WWW_ROOT . $employees->front_image)):
   echo $this->Html->image('../' . $employees->front_image);
endif;

elementExists() is intended for checking that a View element exists, not if files exist in the webroot, so should not be used like you are trying. It does do a file_exists() check, but this scans all available View element paths only.



Answered By - drmonkeyninja
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing