Checking if view exists?

Is there a quick, easy method to check, if view exists?

I’m doing something like that in my widget:


if(isset($item['view']) && $item['view'] != '')

    	echo($this->controller->renderPartial($item['view'], NULL, true));

else

    	echo('Error: Property mode is set to view, but no view (property view) is provided!');



Code properly reacts on user-specified view being empty or not set, but fails with exception, when provided view file is not existing. I want to avoid that. How can I do this?

You will need to check if the file exists with is_file() or even better with is_readable()

but again… IMO you need to check your app logic… if you list some views for a user to select them… there is the place to check the views that exists… so that a user can choose only existing views…

I was wondering if there is Yii built-in function for that? But from your answer it seems, that not.

I’m writing a widget, not an application. I’m not listing available views, I’m only trying to use view provided by user in widget’s configuration. This is totally additional element (not main part of how this widget would work) so I was rather asking like to feed my curiosity. I could leave it even as it is right, so exception would pop up in situation of providing not existing views.

On the other hand, it would be an interesting learn to see, how you would implements listing of available views. If you have some time to spare, share it with us, please.

OK, I got you wrong… when you wrote "user specific view" I was thinking about the customer using the application…

but

as you explained you are thinking on other developers that will configure the "view"… in this case (IMO) there is no need to check… let the exception fire… as those are developers… and our job is not to do their job… and to repair to their errors… when a developer sees an exception… he knows (should know) what is wrong and he will repair the problem…

to show available views… there is no automatic for that… as you can have many not intended views like partial ones… so you would just manualy compile a list of available views and put a dropdown list for a customer to select one…

Haven’t checked, but if there is a native PHP function that does the job… why would we need a Yii version?

Because, it is always good to have a good Yii function for everything! :] Kidding… I was just thinking, that if there is a rendering function, there should also be a function for checking if view exists. But then again, as you explained, most of the cases views directly are being used by developers not, application users, therefore there is no need for such function, as most of the time view will be permanently, not dynamically selected.

Thanks. EoT.

I have a problem ,I can’t understand why this is echoing “The file is not readable”. My file exists of course!




<?php

$im1='http://localhost'.Yii::app()->request->baseUrl.'/images/'.$model->id.'_1.jpg';

if (is_readable($im1)) {

    echo 'The file is readable';

} else {

    echo 'The file is not readable';

}

?>




@fouss

Note that is_readable() checks local files (on your computer)… you are giving an URL to it…

A bit OT, but thought to write you this for some time… Why are you asking to say thank you, for posting questions?

ok I tried "is_file()" and "file_exists()" also but still the same problem…

Q:Why are you asking to say thank you, for posting questions?

R:this is my signature

you can use file_get_contents(); that will return false on failure

the functions you were trying are for the server machine only

I know that this is your signature… just wanted to point you that it’s not appropriate for every post you make…

As mdomba told you, you’re trying to open URL resource with function mainly designed to open files in local file system. This is possible and allowed, but not always!

Check, if your PHP configuration (php.ini) and server configuration allow to use open wrappers, that is if you can pass some protocols (like HTTP in your example) to functions normally operating on files. If your configuration doesn’t allows this (as in default configuration in many ISPs and in many server packages - as this is considered as being rather huge security breach) you will always get result coresponding to file not being found, if trying to use wrappers instead of true files.

Gustavo,

file_get_contents() is actually trying to open file (load it’s contents to the memory) if it exists, so in a matter of speed of execution and application performance, I would rather use your solution as one of last, when other would fail. Using functions that only checks, if file exists I would personally consider as better - again, only in a matter of performance.

Of course, only if we are talking about checking if file exists and openining it with another function (like view rendering in this example). If we want to check if file exists and open it, if it exists, in the same time, your solution is probably the best for doing so.

I also wanted to notice fouss that his signature is a bit extra-ordinary! :] But, after all - it is up to the user, what he or she has in the signature. For example I’m yelling around that I wrote a lot of posts and wiki articles, which can be seen as rude by someone! :] :] :]

Beside that fouss’ signature has type - should be “click”, not “Clic”! :] “Clic” is in French, if I’m not mistaken! :]

And, again, mdomba, we drifted far off-topic! :]

file_get_contents() works if the file exits else this error:

The fastest way to check if a remote file exists is to use headers…

Just recently I had this need, and used a small class for this:

To use it you can do something like




$http->open('www.yourdomain.com');

$status=$http->head('/pictures/logo.gif');

$http->close();

if(''==$status)

   echo "possible network problem, wrong URL, or no internet connection";

else if ("OK"==substr($status,-2))

   echo "file exists";

else

   echo "file does not exist";



and here is the class




	class httpClient {


		var $_socket;	//.. socket identifier

		var $_host;	//.. remote host eg. "www.domain.com"

		var $_file;	//.. remote file eg. "/pictures/logo.gif"


		function httpClient($host=NULL){


			$this->_socket="";

			$this->_host="";

			$this->_url="";


			if(NULL!=$host) {

				$this->_socket = $this->open($host);

			}

		}


		function open($host) {

			$this->_host=$host;

			$this->_socket = fsockopen($host,"80",$errno,$errstr);

			if(!$this->_socket){

				$this->_socket = fsockopen($host,"80",$errno,$errstr,30);

				if(!$this->_socket){

					die("<br /><br />$errstr ($errno)<br /><br />");

				}

			}

			return $this->_socket;

		}

		function close() {

			if($this->_socket) {

				fclose($this->_socket);

			}

		}


		function head($url) {

			$this->_url=$url;

			//$req = "HEAD ".$this->_url." HTTP/1.0\r\nHost: ".$this->_host."\r\nConnection: close\r\n\r\n";

			$req="";

			$req.="HEAD ".$this->_url." HTTP/1.0\r\n";

			$req.="Host: ".$this->_host."\r\n";

			$req.="Connection: keep-alive\r\n";

			$req.="\r\n";

			fputs($this->_socket, $req);

			$reply=trim(fgets($this->_socket,1024));

			return $reply;

		}

	}



I’ll try this but for my need the solution below works fine

I use it in my script like this way:





<?php

$im1='..'.Yii::app()->request->baseUrl.'/images/'.$model->id.'_3.jpg';

if (is_file($im1)){ ?><a href="<?php echo $im1 ?>"><img width="100px" height="100px" src="<?php echo $im1?>"/></a>


<?php }?>



@ Gustavo, mdomba, Trejder

thanks (+1)

I juste noticed that script bellow is not working (no image appears) on my linux hosting acount. D you knw why?

Was it working at all ?

If yes… then maybe the image has being moved… or baseUrl has changed… or there is no picture at all for the $model->id

or the path to the picture is wrong…

Check the value of $im1 that holds a valid path to an existing picture.

Yes it’s working on my local computer. the image also exists but anything is echoing