File exists php code

<?php

function data_info($data)
{
    if ($data) {
        while (!feof($data)) {
            $buffer = fgets($data);
            if (file_exists($buffer)) {
            $bufferArray[$buffer]['Exists'] = (file_exists($buffer));
            $bufferArray[$buffer]['Readable'] = (is_readable($buffer));
            $bufferArray[$buffer]['Writable'] = (is_writable($buffer));
            $bufferArray[$buffer]['Size'] = (filesize($buffer));
            } else {
                $bufferArray[$buffer]['Exists'] = "No";
            }
        }
        print_r($bufferArray);
    } else {
        echo "The file could not be opened";
    }
}

$data = fopen("D:/xampp/htdocs/Practice/ficheros.txt", "r");
data_info($data);

?>

      

If I have this: ficheros.txt: ExistingFile.txt ExistingFile2.txt ExistingFile3.txt ... ... It works, but if I have at least 1 NOT EXISTING FILE then it will accept every file as non-existent.

What happened? I believe I will be in the inner if condition.


I mean what's wrong with all the code.

I just need to create an array with arrays, a good result would be:

    array
(
    'text.txt' => array
        (
        'exists' => true,
        'readable' => true,
        'writable' => true,
        'Size' => 64
        ),

    'document.doc' => array
        (
        'exists' => false
        ),

    'photo.jpg' => array
        (
    'exists' => true,
    'readable' => true,
    'writable' => false,
    'size' => 354915
        )
)

      

+1


source to share


3 answers


Hmm, ok that works on Linux (although I need to truncate the filename first $buffer

).



+4


source


Yes, it works for me too if I have in ficheros.txt

Existingfile.txt
AnotherExistingfile.txt

      

or

FakeFile.txt
FakeFile2.txt

      



But if I combined both of them:

Fakefile.txt
Existingfile.txt

      

It won't work, the script in the latter case accepts both files as non-existent.

0


source


I'm not 100% why you didn't have it, but I played around with it and this works:

<?php

function data_info($data)
{
    if (!$data){return "The file could not be opened";}
    while (!feof($data))
     {
        $buffer = implode('',fgetcsv($data));//fgetcsv will only return an array with 1 item so impload it
        if(file_exists($buffer))
         {
            $bufferArray[$buffer]['Exists'] = (file_exists($buffer));
            $bufferArray[$buffer]['Readable'] = (is_readable($buffer));
            $bufferArray[$buffer]['Writable'] = (is_writable($buffer));
            $bufferArray[$buffer]['Size'] = (filesize($buffer));
         }
        else
         {
            $bufferArray[$buffer]['Exists'] = "No";
         }
     }
    print_r($bufferArray);
}

$data = fopen("c:/file.txt", "r");
data_info($data);

?>

      

Results from the file I used:

Array
(
    [c:/messageService.log] => Array
        (
            [Exists] => 1
            [Readable] => 1
            [Writable] => 1
            [Size] => 0
        )

    [c:/setup.log] => Array
        (
            [Exists] => 1
            [Readable] => 1
            [Writable] => 1
            [Size] => 169
        )

    [c:/fake1.txt] => Array
        (
            [Exists] => No
        )

    [c:/fake2.txt] => Array
        (
            [Exists] => No
        )

)

      

Second, after looking at some of your comments, try using exact paths instead of the filename.

0


source







All Articles