Jump to content


Adding something to the end of a line if it is not there to begin with.

  • Please log in to reply
2 replies to this topic

#1 Jnitrofish

  • New Members
  • Pip
  • Newbie
  • 1 posts
  • LocationSoutheast Texas

Posted 08 July 2006 - 03:02 AM

This is pretty simple I'm guessing, its normally the simple ones that confuse me.  :P

I am trying to get code that will add something to to the end of a line (like a forward slash) if it does not find that something there already.

In my case what I am doing is having a user submit form data to a PHP script. There are 2 important fields in this form; 1. a URL/directory field, 2. a file field. Now in the script there are a few cases where the 2 field's post data is being used together and I found that if the user forgets to add a forward slash to the end of the directory name (or to the beginning of the file name, and they are not suppost to do that anyway) that my fwrite() edits the wrong file/creates a new one.

$URL = $_POST['URL'];
$FileName = $_POST['FileName'];
$Content = "Blah";

if (is_dir($URL)) {
fwrite(fopen($URL.$FileName, "a"), $Content);
fclose(fopen($URL.$FileName, "a"));
chmod($URL.$FileName, 0755);
} else {
mkdir($URL, 0755);
fwrite(fopen($URL.$FileName, "a"), $Content);
fclose(fopen($URL.$FileName, "a"));
chmod($URL.$FileName, 0755);

What happens with out a slash included on the end of the post data from the field 'URL'? mkdir ends up creating a new directory named "/testdir" and then fwrite creates a new file named "/testdirtestfile.php" and not a file in the directory "/testdir" named "testfile.php".

So, I cant figure out how to make sure that the user added a slash to the end of the name they put in the URL field, and at least echo up a error if they forgot the slash, if not add the slash for them.

If I could get some opinions on this, that would be great.

Thanks as always,

#2 Kurt

  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 08 July 2006 - 03:06 AM

Im not good with regular expressions (thats one method of doing this) but you could tr this:

die('Please enter a /');

That's not that secure for various reasons so I suggest using regular expressions.

#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 08 July 2006 - 09:24 AM

$string = 'abc/def';
	$string .= '/';
echo $string;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users