New Posts  All Forums:Forum Nav:

PHP Help.

post #1 of 11
Thread Starter 
It's relatively simple, but requires some thinking skills.

Here is our TXT file:
Code:
icons.txt
Can you make PHP read all the files in a specified directory, then make it add each line of this in the text file and then save it?
Code:
smiley_filename.ext=+:smiley Description=+::smiley_code
Where smiley_filename.ext will be an image in the directory
smiley description will be the filename - but without the extension in it and
:smiley code is an increasing number by one increment. Like 1, 2, 3 and so on.

^^
post #2 of 11
Thread Starter 
^6^
post #3 of 11
What's the point?
And what would be more clear is this: Is that correct?
Code:
smiley_filename.ext=+:smiley_filename=+::id
And why have the "description" if you can just take smiley_filename.ext and split it via the period?
post #4 of 11
Thread Starter 
The point is to make a line for each image instead of having to type it all up.
These attributes are going to be used. The format I primarily gave is correct.
post #5 of 11
Quote:
Originally Posted by LyokoHaCk View Post
The point is to make a line for each image instead of having to type it all up.
These attributes are going to be used. The format I primarily gave is correct.
Yeah, but when reading it, it was hard to understand, so I rewrote what I thought I understood.

Here's my try:
For PHP 5 (code not tested):
Code:
// Set variables.
$dir = "/path/to/dir/";
$files = scandir($dir);
$i = 0;
$count = count(files); // Remember, normally minus one, except for the while case, we aren't doing <= or >=.

// Open file.
$file = "file.txt";
$fh = fopen($file 'w') or die("Error: Can't open file.");

// Begin while case.
While ($count > $i) {
$filename = explode(".", $file[$i]);
$file_contents .=  $file[$i]. "=+:" .$filename. "=+::" .$id. "\
";
$i++;
}
fwrite($fh, $file_contents);
fclose($fh);
Resources: http://us2.php.net/manual/en/function.scandir.php
http://www.tizag.com/phpT/filewrite.php

I still don't see the light...
post #6 of 11
Thread Starter 
No go. It somehow splits up the file name of the txt file O.o
Code:
f=+:Array=+::
i=+:Array=+::
l=+:Array=+::
e=+:Array=+::
n=+:Array=+::
a=+:Array=+::
m=+:Array=+::
e=+:Array=+::
s=+:Array=+::
.=+:Array=+::
t=+:Array=+::
x=+:Array=+::
t=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
=+:Array=+::
Goal is:
Code:
Image1.gif=+:Image1=+::1
Image2.gif=+:Image2=+::2
Image3.gif=+:Image3=+::3
etc..
post #7 of 11
Hm... Well, I noticed I forgot something. Try this:
Code:
// Set variables.
$dir = "/path/to/dir/";
$files = scandir($dir);
$i = 0;
$count = count(files); // Remember, normally minus one, except for the while case, we aren't doing <= or >=.

// Open file.
$file = "file.txt";
$fh = fopen($file 'w') or die("Error: Can't open file.");

// Begin while case.
While ($count > $i) {
$filename = explode(".", $file[$i]);
$file_contents .=  $file[$i]. "=+:" .$filename[0]. "=+::" .$id. "\
";
$i++;
}
fwrite($fh, $file_contents);
fclose($fh);
post #8 of 11
Thread Starter 
Still doesn't work. And I included the $ to the count.
post #9 of 11
Here is mine

Code:

<?php
$strDirectory = "/dir/";
$arFiles = scandir($strDirectory);
$iCounter = 0;
for($z = 0; $z< count($arFiles); $z++)
{
 $tFile = trim($arFiles[$z]);
 $arFile = explode('.',$tFile);
 if(strlen($arFile[1]) > 0)
 {
  $iCounter++;
  $strContent .= $arFile[0] . '.' . $arFile[1] . '=+:' . $arFile[0] . '=+::' . $iCounter . "\
";
 }
}
// Open file.
$oFile = "file.txt";
$hwndFile = fopen($oFile,'w') or die("Error: Can't open file.");
fwrite($hwndFile, $strContent);
fclose($hwndFile);
?>
EDIT: Just to give proper credit I copied the file write function from above
Main Rig
(15 items)
 
  
Reply
Main Rig
(15 items)
 
  
Reply
post #10 of 11
Thread Starter 
Thanks ^^
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding