在这里拖了很多问题而且找不到解决方案,所以......
我们为孩子们创建PHP练习来学习。有时,他们必须创建文件夹,这些文件夹可能会保持空白如果孩子们跳过创建文件夹,那么PHP会引发令人讨厌的错误。喜欢使用类似的代码显示自定义错误:
$file = 'mydir';
if(is_dir($file)) {
die("<p>folder does not exist, please return and create as instructed...</p>");
} else {
$file=fopen($file,"r");
}
所以回顾一下。函数检查目录是否存在,如果脚本继续,它不会死亡并显示简单消息,直到创建目录。 我不希望PHP创建目录,如果它不存在,孩子们必须学会遵循步骤:)
即使目录存在,我的代码也不会继续。我做错了什么?
答案 0 :(得分:0)
你输错了条件: 以下是正确的条件。
$file = 'mydir';
if(is_dir($file))
{
//DIRECTORY EXISTS
}
else
{
//DIRECTORY DOES NOT EXIST
}
答案 1 :(得分:0)
建议:当双引号不必要时使用单引号,它会按原样处理内容(也就是说它不会影响它),这更简单,因为你不必使用&#34; \&#34;对于控制字符(&#34; \&#39;&#34;如果你想在字符串中使用单引号),它会使你的代码更有效,因为PHP不必检查转义字符
$location = 'mydir'; //Better call it location as directory =/= file.
if(is_dir($location))
{
//$fout = fopen($file, 'rb'); //'rb' for binary mode, please use this instead.
$fout = fopen($location . '/' . $file, 'rb'); //I assumed you wanted to write the file in the directory you were testing for.
}
else
{
die('<p>folder does not exist, please return and create as instructed...</p>');
}