-3

Possible Duplicate:
Getting the names of all files in a directory with PHP

I am trying to build a thing where i can scan all the .txt files in a directory then explode them into an array, the seperation would be a "|", then insert them into a sql DB

Community
  • 1
  • 1
  • You want to insert the name of your files in your DB ? Or the content of your files ? – Magus Nov 30 '12 at 09:17
  • there is not a constant name...all the text files are different. I have tried a bunch of things, but none seem to work. and there are about 7 of the |'s. For now i would just like to be able to search all the text files and a given directory. – FireAtEnemy225 Nov 30 '12 at 09:23
  • you want to insert the file names? The text file contents? Or the files themselves as blobs? – WebChemist Nov 30 '12 at 09:34
  • i want to insert the file contents. I figured i could do it using an array with the explode function. – FireAtEnemy225 Nov 30 '12 at 09:37
  • getting the file names in a directory has been asked many times before, like this one: http://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php – WebChemist Nov 30 '12 at 09:37
  • Ok... but I still don't understand the purpose of the pipe, as in explode you remove the delimiter, pipe as seperator would only make sense if you imploded the array into a string. Are you trying to say you wish to get all text content from all files, combine that using a pipe seperator and insert as a single database record? – WebChemist Nov 30 '12 at 09:40
  • YES. that is what i am saying. Sorry for the mis explaining. – FireAtEnemy225 Nov 30 '12 at 09:42
  • Make an empty array to store the text. Do a foreach loop on the filename array, inside the loop you fopen each file, read its contents, assign a new text array element & close the fopened file. After the loop, implode with | and insert that string. But I would advise you make each text file content its own database record and NOT mash them all together in a single pipe delimited record. And this is a comment not an answer because I'll explain the logic but I'm not going to code this for you. Its important you learn to use basic PHP functions. PHP.net is your friend – WebChemist Nov 30 '12 at 09:57
  • I am trying to go by your logic, what do you mean by assign a new text array. You mean make a new array? like: foreach($test as $value) { fopen("./file.txt", "r"); $value = array(); } or what? – FireAtEnemy225 Nov 30 '12 at 10:16
  • I mean you create an empty array() to store the text you will read while looping through the other file name array. Read up on array_push and the file text assignment part might make more sense http://php.net/manual/en/function.array-push.php – WebChemist Nov 30 '12 at 10:26

2 Answers2

0

Please try this with your values

$files_names="";
//path to directory to scan
$directory = "path_to_directory";

//get all text files with a .txt extension.
$files = glob($directory . "*.txt");

//print each file name
foreach($files as $files_names)
{
$files_names."|".=$files_names;
}
Elby
  • 1,624
  • 3
  • 23
  • 42
  • Yeah that code is not quite right, error on line 11 – Geert Nov 30 '12 at 09:28
  • That is awesome. It lists all of the text files in the directory, but then how would I have it search inside the file to then seperate each part of the line using the |? – FireAtEnemy225 Nov 30 '12 at 09:30
0

you can use above answer, but simplified

$file_names = implode('|', glob('*.txt'));
Igor Mancos
  • 314
  • 6
  • 15