A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearFile exists php code - page 1
User InfoPosts
File exists php code#1
<?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 NON EXISTING FILE then It will take every file as a non existing one too.

What's wrong? I believe someting in the inner if condition.



I mean, what is wrong with the entire code.

I just need to make an array with arrays in it, 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
)
)

posted date: 2008-12-13 19:34:00


Re: File exists php code#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-13 19:34:01


Re: File exists php code#3
Hmm, well that works in Linux (though I have to trim the filename $buffer first).

posted date: 2008-12-13 19:49:00


Re: File exists php code#4
Yeah, It works for me too if I have in ficheros.txtExistingfile.txtAnotherExistingfile.txtOrFakeFile.txtFakeFile2.txtBut If I combine both of them:Fakefile.txtExistingfile.txtIt won't work, the script in the last case takes both files as non existing ones.

posted date: 2008-12-13 19:55:00


Re: File exists php code#5
Same here. Also, a check for fgets() is needed as feof() usually still returns false after the last successful fgets().

posted date: 2008-12-13 19:58:00


Re: File exists php code#6
Hmm.. that seems very strange. Are you using the exact same code as you posted here? Does it matter at what position the non-existing file is in ficheros.txt?

posted date: 2008-12-13 20:03:00


Re: File exists php code#7
No, It does not matter at what position ;[

posted date: 2008-12-13 20:30:00


Re: File exists php code#8
Im not 100% why your wasn't but I played with it and this is working:<?phpfunction 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);?>The 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 ))On second thought after looking at some of your comments, try it using exact paths instead of just the file name.

posted date: 2008-12-13 20:34:00


Re: File exists php code#9
I know this is old, but I remember that trim fixed it.

posted date: 2011-03-01 13:28:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.