Easy PHP question
Im forgetting some things here, can someone please help me refresh my mind here about this code?
<?php
//test if file exist
$fileexist = $filename . '.txt';
if (file_exists($fileexist) == true)
{
$filename = $filename . '1';
}
echo file_exists($fileexist) ? \"File Already Exists, new filename: $filename\" : 'File Created';
?>
Im experimenting on a php read, write property, and I came upon a roadblock.
Written above should be a code that will check a directory if a certain file exists, if it does it continually appends numbers to filenames.
Example, on the first file create I used the filename "chris" .. so a file "chris.txt" is created, if I use the same filename "chris" then it would check if "chris.txt" exists on the directory, it does this time so it appends "1" to the filename now it save "chris1.txt" I clearly know how to do this until this point... Now for the third time i use again a filename "chris" , it checks again and knows "chris.txt" exist so it appends "1" again instead I want it to check if "chris1.txt" , "chris2.txt" and so on exists, and append something that is not a duplicate of the original, because if that happens, it overwrited the whole file that has the similar filename to file being saved.
Any help appreciated here...
Thanks in advance
demonhale posted this at 03:28 — 10th June 2006.
He has: 3,278 posts
Joined: May 2005
Kinda figured to non-duplicate but its messy... if anyone got an idea for a cleaner version, it would be good...
I used a filecheck with the getcw command, then if it returns true I apend "1" then "11"
It works but what i intended was "1" then "2" ,
well anyways, it works at the moment so no problem...
adjroth posted this at 15:33 — 10th June 2006.
He has: 33 posts
Joined: Jun 2006
i added a while loop to keep trying, starts at 1 and keeps going up until file_exists returns true.
$fileexist = $filename . '.txt';
if (file_exists($fileexist))
{
$newfile = 0;
while (file_exists($fileexist))
{
$newfile = $newfile + 1;
$filename = $filename.''.$newfile;
$fileexist = $filename . '.txt';
}
}
echo file_exists($fileexist) ? "File Already Exists, new filename: $filename" : 'File Created';
kb posted this at 18:31 — 10th June 2006.
He has: 1,380 posts
Joined: Feb 2002
If you're wanting it to return "2" instead of "11", why not add them together instead of concatenating?
demonhale posted this at 04:56 — 11th June 2006.
He has: 3,278 posts
Joined: May 2005
@adjroth
Thanks for the rewrite, actually I used the same mehotd as you, using the while loop, the difference is i append filenames like this "test.txt" if duplicate "test1.txt" if duplicate "test11.txt" and so on...
What happens with yours is like this, "test.txt" if duplicate "test1.txt" then becomes "test12.txt" then "test123.txt" ... I think i need to clear $newfile = 0 again at the end... But really good help...
Anyways, another question, whats the security issue if I saved a file to html directly rather than on a txt file, and allowing html codes on the textarea,? Any light on this...?
adjroth posted this at 05:04 — 11th June 2006.
He has: 33 posts
Joined: Jun 2006
yes, you're right. what i mean to do was this ...
checks for "test.txt" then "test1.txt" then "test2.txt" and so on ...
$fileexist = $filename . '.txt';
if (file_exists($fileexist))
{
$newfile = 0;
while (file_exists($fileexist))
{
$newfile = $newfile + 1;
$fileexist = $filename.$newfile.'.txt'; //if you get an error here replace with $fileexist = $filename.''.$newfile.'.txt';
}
}
echo file_exists($fileexist) ? "File Already Exists, new filename: $filename" : 'File Created';
adjroth posted this at 05:13 — 11th June 2006.
He has: 33 posts
Joined: Jun 2006
If this is a forum, profile entry, or any other related where other users will view these files, then there is a high security risk. Users may use script, iframe, or activex to run harmful software from your site, or run videos or music.
You may want to use "strip_tags" to strip the html tags. more information on this function can be found here http://us3.php.net/manual/en/function.strip-tags.php
demonhale posted this at 05:26 — 11th June 2006.
He has: 3,278 posts
Joined: May 2005
@adjroth
been a great help so far,
The code is correct at checking the files, thats why i said i arrived at the same code using the while earlier, the problem lies in writing the concatenate, im figuring it out now by using string.drop command ...
Thanks for the additional tip, Ill check it out...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.