Home >PHP Framework >YII >Use php in yii2 to determine whether the image exists
In our daily development, we often need to judge whether the picture exists. If it exists, it will be displayed. If it does not exist, the default picture will be displayed. So what judgments do we use? Today we will look at a few commonly used methods.
Using php in yii2 to determine whether the image exists
1. file_exists() function
file_exists() function checks whether a file or directory exists.
Returns true if the specified file or directory exists, otherwise returns false.
eg: file_exists(path); The parameter path must be path, not url, otherwise false will always be returned;
Note:
1. In any upper-level directory of the file, if you only have write permission, it will report that the file does not exist;
2. In any upper-level directory of the file, if you only have read permission, it will also report that the file does not exist;
3. When When all upper-level directories have execution permissions, the reported files exist and everything is normal.
Explanation that when file_exists() determines whether a file exists, it recursively determines whether each directory has execution permissions.
2. file_get_contents() function
file_get_contents - Read the entire file into a string
If it fails, file_get_contents() will return FALSE.
If you want to open a URL with special characters (for example, spaces), you need to use urlencode() for URL encoding.
However, if this function has many requests and the file is relatively large, it may time out and fail to respond, causing the server to hang up.
To set the timeout of the file_get_contents function, you can use the timeout of resource $context Parameters, the code is as follows:
$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>10, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context); echo $html;
3. Curl method
Function implemented:
1.Remote acquisition and collection of content
2. Implement FTP upload and download of PHP web version
3. Implement simulated login: go to an email system, curl can simulate cookies
4. Implement interface docking (API) and data transmission Etc.: Send text messages through a platform that captures and delivers the transmitted information.
5. Implement simulated cookies, etc.: Some attributes can only be operated when logged in.
How to use the CURL function:
PHP does not support CURL by default. You need to enable this function in php.ini
;extension=php_curl. Remove the semicolon in front of dll
1 The first step in the entire operation is to use the curl_init() function to initialize
2. Use the curl_setopt() function to set options.
3. After setting, execute the transaction curl_exec($curl);
4 Finally close curl_close();
curl compatible with get and post methods;
function curl($url, $type = 'get', $post_data = null, $second = 30) { $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // //设置header curl_setopt($ch, CURLOPT_HEADER, false); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ('post' == $type) { curl_setopt($ch, CURLOPT_POST, 1); //开启POST curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //POST数据 } $output = curl_exec($ch); curl_close($ch); return $output; //返回或者显示结果 }
Recommended related articles and tutorials: yii tutorial
The above is the detailed content of Use php in yii2 to determine whether the image exists. For more information, please follow other related articles on the PHP Chinese website!