这是我的代码:
elseif (is_dir($dir)) {
$scanning = scandir($dir);
$okdir;
// $response["scan"] = $scanning;
foreach ($scanning as $key=> $value) {
$splitscan = explode("_",$scanning[$key]);
if ($splitscan[0] == $searchvalue ){
$okdir[] = $scanning[$key];
if (count($okdir)>1){
$response["okdir"] = $okdir;
$response["result"] = "more";
}
else {
$okdir = $okdir[0];
}
}
}
当函数继续执行else语句时,我没有问题,但是当条件需要验证if语句if(count($okdir>1))
时,我得到了PHP内部错误。这是什么意思?那if陈述永远是不正确的?我做错了什么?
谢谢您提供的任何帮助
这是错误日志答案:
PHP致命错误:未捕获错误:字符串不支持[]运算符
这是整个功能:
private function pdf(){
$response;
$dir = "../Pdf/";
$searchvalue = $this->params["utentericercato"];
if (!isset($_COOKIE["idutente"])){
$response["result"] = "nosession";
$utente->setUteLogged(false);
}
// Open a known directory, and proceed to read its contents
elseif (is_dir($dir)) {
$scanning = scandir($dir);
$okdir = array();
// $response["scan"] = $scanning;
foreach ($scanning as $key=> $value) {
$splitscan = explode("_",$scanning[$key]);
if ($splitscan[0] == $searchvalue ){
$okdir[] = $scanning[$key];
if (count($okdir)>1){
$response["okdir"] = $okdir;
$response["result"] = "more";
}
else {
$okdir = $okdir[0];
}
}
}
if (empty($okdir)){
$response["result"]= "noutente";
}
else {
$newdir = $dir.$okdir;
$nomeutente = preg_replace("/[^a-zA-Z\s]/", "", $okdir);
$response["dir"] = $newdir;
$response["nomeutente"] = $nomeutente;
if (is_dir($newdir)){
$dh = array_diff(scandir($newdir),array(".",".."));
$arr = is_array($dh);
$x = 0;
while($x < sizeof($dh)){
foreach($dh as $key=>$value){
if ($key[$value] != "." && $key[$value] != ".."){
$response["files"][$key] = $value;
$response["result"] = "success";
}
$x = $x + 1;
}
}
}
}
}
else {
$response["result"] = "nodirectory";
}
Responder::giveResponse($response);
}
答案 0 :(得分:2)
您的第一次迭代将变量设置为字符串。任何后续迭代都将尝试将数据“推”入字符串,就像它是数组一样。
$scanning[0] = "b";
$okdir = "a"; // just add [] after $okdir
$okdir[] = $scanning[0];
var_export($okdir);
我想我建议您删除:
else {
$okdir = $okdir[0];
}
只会带来麻烦。
然后...
if (count($okdir)>1){
$response["okdir"] = $okdir;
$response["result"] = "more";
}
应放置在循环之后,以便仅执行一次。
如果这是我的项目,我会做类似chdir($dir);
然后是glob("{$searchvalue}_*")
的事情–在同一步骤中抓取并过滤。
答案 1 :(得分:1)
我尚未对其进行测试,但是,这种更清洁的方法应该可以工作。至少它应该使您了解如何以结构化方式完成任务。
private function pdf()
{
$response = [];
$dir = "../Pdf/";
$searchvalue = $this->params["utentericercato"];
if (is_dir($dir))
{
chdir($dir);
$okdir = glob("{$searchvalue}_*", GLOB_ONLYDIR);
switch (count($okdir))
{
case 0:
$response['result'] = 'noutente';
break;
case 1:
$nomeutente = preg_replace("/[^a-z\s]/i", "", $okdir[0]);
$response["dir"] = $dir . $okdir[0];
$response["nomeutente"] = $nomeutente;
chdir($okdir[0]);
$response["files"] = glob('*');
if(!empty($response["files"]))
{
$response["result"] = "success";
}
break;
default:
$response["okdir"] = $okdir; // or $okdir[0] if you do not want an array but the first item
$response["result"] = "more";
}
}
else
{
$response["result"] = "nodirectory";
}
Responder::giveResponse($response);
}
答案 2 :(得分:0)
以下是错误日志答案:PHP致命错误:未捕获错误:字符串不支持[]运算符
要解决以上错误,请将$okdir
定义为数组。
第3行应从$okdir;
更改为$okdir = array();
答案 3 :(得分:0)
我找到了解决方案,旧代码不起作用:
elseif (is_dir($dir)) {
$scanning = scandir($dir);
$okdir = array();
// $response["scan"] = $scanning;
foreach ($scanning as $key=> $value) {
$splitscan = explode("_",$scanning[$key]);
if ($splitscan[0] == $searchvalue ){
$okdir[] = $scanning[$key];
if (count($okdir)>1){
$response["okdir"] = $okdir;
$response["result"] = "more";
}
else {
$okdir = $okdir[0];
}
}
}
if (empty($okdir)){
$response["result"]= "noutente";
}
新工作代码:
elseif (is_dir($dir)) {
$scanning = scandir($dir);
$okdir = array();
// $response["scan"] = $scanning;
foreach ($scanning as $key=> $value) {
$splitscan = explode("_",$scanning[$key]);
if ($splitscan[0] == $searchvalue ){
$okdir[] = $scanning[$key];
}
}
if (count($okdir)>1){
$response["okdir"] = $okdir;
$response["result"] = "more";
Responder::giveResponse($response);
exit;
}
if (empty($okdir)){
$response["result"]= "noutente";
}