【php中判断文件存在是用file_exists还是is_file的整理_PHP教程】教程文章相关的互联网学习教程文章

PHP如果array_key_exists,变量等于数组值:如何?【代码】

我组合了两个数组来创建以下数组,名为$group_wages_array:Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )我试图测试数组键是否匹配X,设置一个变量作为它的值.这就是我所拥有的: 注意:整个事情都是在while循环中执行的,因此$thegroup [‘group_id’]的值将会改变.我为这个例子设置了它的值为“6”.$thegroup['group_id'] = "6" // This particular group (for simplicity)if (array_key_exists($thegroup['group_id'], ...

PHP:if(function_exists(‘curl_init’))有效吗?【代码】

我有一些代码,如果存在curl_init(),我只想执行.我正在使用此代码检查它是否存在:if(function_exists('curl_init')) {$c = curl_init();curl_setopt($c, CURLOPT_URL, 'http://www.my-site.ext/' . $data . '/');curl_setopt($c, CURLOPT_HEADER, 0);curl_exec($c);curl_close($c); }if()返回true,但是内部代码抛出此错误:Fatal error: Call to undefined function curl_init() in /var/www/wp-content/plugins/my-plugin-name/in...

php – Reflection :: hasMethod vs. method_exists性能【代码】

从性能的角度来看,我应该选择:$className = 'Foobar'; $methodName = 'method';这个$Reflected = new ReflectionClass($className); $result = $Reflected->hasMethod($methodName);过度$result = method_exists($className,$method);为什么? 对于这种情况,我没有使用ReflectionClass的任何其他属性或方法.解决方法:我用1,000,000个循环测量了它.它可能不具代表性. 需要21秒:$reflector = new ReflectionClass($module); $refle...

php – in_array()和array_key_exists()之间有什么区别?

我在这里有疑问.实际上,in_array()和array_key_exists()之间有什么区别.我试图找到这个,但我仍然困惑.我能得到一个样品吗? 谢谢.解决方法:首先,PHP中不存在array_exist().它是array_key_exists(). 区别: in_array()检查数组中是否存在值(检查值,而不是键)并返回true,否则返回false. 而: array_key_exists()检查数组中是否存在给定的键或索引(检入键,而不是值)并返回true,否则返回false. 选择给定的手册示例.跑到最后,你会清楚地...

php – function_exists每次都返回false【代码】

我正在尝试检查一个函数是否存在但是我的if仍然是假的 我试着像这样调用函数,其中$function是函数名:if (function_exists($this->module->$function)) {$this->module->$function($vars); } else {echo 'no'; }变量模块定义为应该调用函数的类:$this->module = $module; $this->module = new $this -> module;我在这里错过了什么吗?谢谢!解决方法:可以搞清楚:使用method_exists()解决了我的问题method_exists($this->module,$...

php – class_exists正在调用spl_autoload_register【代码】

我为自动加载类创建了一个简单的脚本,但是当我使用class_exists执行spl_autoload_register时,例如:<?php function autoLoadClass($name) {echo 'spl_autoload_register: ', $name, '<br>'; }spl_autoload_register('autoLoadClass');class_exists('Foo'); class_exists('Bar'); class_exists('Foo\\Bar');输出:spl_autoload_register: Foospl_autoload_register: Barspl_autoload_register: Foo\Bar是对的吗?有没有办法让“spl_...

php – 我不明白file_exists和scandir发生了什么【代码】

我正在帮助一位朋友为他的一个剧本编写一个有点合理的缓存功能.它主要使用9种不同的SQL查询来获取排行榜数据(其中一种非常繁琐) 我提出了为它创建缓存的想法,从这开始:// Cache directory$cacheDir = "/cache";// Path to cache directory$cachePath = "/var/www/html/test";// Better safe than sorryif (!file_exists($cachePath . $cacheDir)){mkdir($cachePath . $cacheDir, 0777, true);}// Cache rebuild switches$monsterC...

PHP和file_exists – 澄清文档的注释

我正在查看file_exists() PHP函数的文档. 文档在某些时候写道:The check is done using the real UID/GID instead of the effective one.那是什么意思?我不知道. 有人可以向我解释一下吗?解决方法:有效的UID / GID是软件正在使用的UID / GID(例如,由root执行的软件可以将其UID / GID更改为守护程序:守护程序,因此有效的UID / GID也会更改.真正的UID / GID是进程所有者的UID / GID,它在进程启动后永远不会更改. 因此,使用进程所...

php – file_exists,相对路径不起作用【代码】

file_exists不工作..也尝试过realpath ..同样的问题 首先检查文件是否存在.. file_exists返回false但无论如何都加载了文件chdir(__DIR__.'/../..');$file = 'frontend.php';echo "$file\n"; if(file_exists($file)){echo "File found\n"; } else{echo "File not found\n"; }require $file;产量frontend.php File not found Contents of frontend.php解决方法:正如php.net/file_exists所说,file_exists()函数需要:Path to the file...

php – 为什么我们需要function_exists?【代码】

为什么我们需要为用户定义的函数检查function_exists?内部或核心PHP函数看起来没问题,但如果用户自己知道并定义了一个函数,那么为什么需要检查它的存在? 以下是自定义用户定义的功能if( !function_exists( 'bia_register_menu' ) ) {function bia_register_menu() {register_nav_menu('primary-menu', __('Primary Menu'));}add_action('init', 'bia_register_menu'); }谢谢解决方法:要确保没有两次注册相同的功能,这将导致错误....

PHP函数中的PHP function_exists / class_exists等效【代码】

我正在寻找asp.net/c#中这两个函数的替代方法.if(function_exists('foo')) {$returned = foo($bar); } if(class_exists('foo')) {$fooclass = new foo($bar); }解决方法: Assembly assembly = Assembly.LoadFile("assemblyAddress");bool containClass = assembly.GetTypes().Where(x=>x.Name == "ClassName").Count() > 0;bool containmethod = assembly.GetTypes().Where(x => x.GetMethods().Count(p => p.Name == "MethodName"...

php – file_exists()与scandir()的in_array() – 哪个更快?【代码】

假设我们有一个这样的循环:foreach($entries as $entry){ // let's say this loops 1000 timesif (file_exists('/some/dir/'.$entry.'.jpg')){echo 'file exists';} }我假设这必须访问HDD 1000次并检查每个文件是否存在. 这样做呢?$files = scandir('/some/dir/'); foreach($entries as $entry){ // let's say this loops 1000 timesif (in_array($entry.'.jpg', $files)){echo 'file exists';} }问题1:如果这次访问硬盘一次,那...

php – file_exists和包含相对路径的路径(“/../”)【代码】

当我在/a/path/to/a/../file.php之类的路径上使用file_get_contents时,它获取内容就好了.如果我先调用file_exists(或is_file或realpath),则返回值表示该文件不存在.这似乎是什么问题? 编辑:以下是从评论到答案浓缩的一些其他信息: >我使用php 5.5.6运行Mac OS X 10.9,因此安全模式应该不是问题(it was removed in version 5.4)>我尝试通过调用clearstatcache清除文件现金(true,$dir1)>有问题的文件大小为362字节,但我在几个位置...

PHP--数组函数 array_fill、array_filter、array_flip、array_key_exists、array_keys

1、array_fill():通过指定的索引顺序及个数生成数组:print_r(array_fill(30,1,'seo'));2、array_filter():数组过滤函数,通过回调函数的方式返回新数组,如果回调函数返回true,数组元素返回到新数组中:$arry = array('class1'=>'seo课程','class2'=>'web课程','weburl'=>'http://www.zymseo.com');function callback($v){if(strpos($v,'课程')){return true;}else{return false;}}print_r(array_filter($arry,'callback'));3、...

php – 什么是更快的file_exists或数据库检索?【代码】

我正在调整我们为我们网站存储的图像的方式.对于每个用户,我都希望看到他们是否有个人资料图片,我是通过检查文件是否存在于他们的文件夹结构中来做到这一点.这比在数据库表中存储/检索图像的名称更快? 我当前的file_exists代码如下所示:$gender = ($gender == 1) ? 'female' : 'male';$filename = SITE_ROOT . $this->img_url . $user_id . 'medium_thumb.jpg';if (file_exists($filename)) {$filename = $this->img_url . $user...