简体   繁体   English

PHP的scandir函数问题

[英]PHP problems with scandir function

dirdel.php: dirdel.php:

<?php
//The name of the folder.
$dir = 'images';
//Get a list of all of the file names in the folder.
$arraydir = scandir($dir, 2);
print_r($arraydir);

//Loop through the file list.
foreach ($arraydir as $key => $value) { 
unlink($arraydir[2]); 
}  
?>

Array outputs: 数组输出:

Array ( [0] => . 
        [1] => .. 
        [2] => ana.png 
        [3] => ban.png 
        [4] => ing.png 
        [5] => maca.png 
        [6] => no.png 
        [7] => pret.png )

Warning: unlink(ana.png): No such file or directory in C:\\phpdesktop- chrome-57.0-rc-php-7.1.3\\www\\dirdel.php on line 10 警告:unlink(ana.png):第10行的C:\\ phpdesktop-chrome-57.0-rc-php-7.1.3 \\ www \\ dirdel.php中没有此类文件或目录

To investigate the error, I also tried something like: 为了调查错误,我还尝试了类似的方法:

require 'images/';

Output: 输出:

Warning: require(C:\\phpdesktop-chrome-57.0-rc-php-7.1.3\\www\\images): failed to open stream: Permission denied in C:\\phpdesktop-chrome-57.0-rc- php-7.1.3\\www\\dirdel.php on line 2 警告:require(C:\\ phpdesktop-chrome-57.0-rc-php-7.1.3 \\ www \\ images):无法打开流:C:\\ phpdesktop-chrome-57.0-rc- php-7.1.3中的权限被拒绝第2行上的\\ www \\ dirdel.php

I want to delete the file "ana.png" represented by: "$arraydir[2]" (the file is found in www/images) 我想删除由“ $ arraydir [2]”表示的文件“ ana.png”(该文件位于www / images中)

I already searched in several places, but I did not find anything, that help me to fix this problem. 我已经在几个地方搜索过,但是没有找到任何东西可以帮助我解决此问题。

Is there any solution for this? 有什么解决办法吗?

Alternatives are valid, as long as they respect the structure of the arrays: 替代方法有效,只要它们尊重数组的结构即可:

Array ( [0] => . 
        [1] => .. 
        [2] => ana.png 
        [3] => ban.png 
        [4] => ing.png 
        [5] => maca.png 
        [6] => no.png 
        [7] => pret.png )

Thanks for your attention. 感谢您的关注。

The file is in the images folder but you do not add that to the unlink() function parameter. 该文件位于images文件夹中,但是您没有将其添加到unlink()函数参数中。

So try this instead 所以试试这个

unlink($dir . '/' . $arraydir[2]);

Actually if you run your code you'll always unlink only index 2 of your array. 实际上,如果您运行代码,则始终将仅取消链接数组的索引2。 You have to make use of the variables and references that you are using in your foreach loop. 您必须使用在foreach循环中使用的变量和引用。 I suggest you try the below mentioned code: 我建议您尝试以下提到的代码:

<?php
    //The name of the folder.
    $dir = 'images';

    //Get a list of all of the file names in the folder.
    $arraydir = scandir($dir, 2);
    print_r($arraydir);

    //Loop through the file list.
    foreach ($arraydir as $key => $value) {
        if ($arraydir[$key] == 'ana.png' && file_exists($dir . DIRECTORY_SEPARATOR . $arraydir[$key])) {
            unlink($dir . DIRECTORY_SEPARATOR . $arraydir[$key]);
            break;
        } 
    }  
?>

Hope this helps. 希望这可以帮助。

When you first tried to delete the file ana.png, the path used was relative to your current directory, so the file was not found. 首次尝试删除文件ana.png时,使用的路径是相对于当前目录的,因此找不到该文件。 What resulted in the first error. 导致第一个错误的原因。

To solve that, you should either give an absolute path name , 为了解决这个问题,您应该给一个绝对路径名,

$prefix = 'C:\\phpdesktop-chrome-57.0-rc-php-7.1.3\\www\images\\';
$filename = $prefix . $arraydir[2];
unlink($filename)

or, concatenate your file name with its directory name $dir . '/' . $arraydir[2] 或者,将您的文件名与其目录名$dir . '/' . $arraydir[2] $dir . '/' . $arraydir[2]

$dir = 'images'; /*Assuming your current directory is 'C:\phpdesktop-chrome-57.0-rc-php-7.1.3\www\'*/
unlink($dir . $arraydir[2]);

As for the second error, it seems that you don't have write permissions on the folder 'C:\\phpdesktop-chrome-57.0-rc-php-7.1.3\\www\\images'; 至于第二个错误,似乎您对文件夹“ C:\\ phpdesktop-chrome-57.0-rc-php-7.1.3 \\ www \\ images”没有写权限; you should change the files and directory permissions. 您应该更改文件和目录权限。 I would suggest following this tutorial about setting about file permissions on windows 10 我建议遵循有关在Windows 10上设置文件权限的本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM