How do I check if a string has a \ in it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

How do I check if a string has a \ in it?

Post by Seona »

Hi guys,

OK, I originally had a piece of code that checked if the value passed by a form field was nothing (ie, formfield = ""). This worked well until I found out just now that I had to include an enctype in my form so that the file upload would happen. A side effect of this is apparently that the empty file fields are no longer "". So if I don't have all of them filled, it overwrites what's in the database with an empty string. Since this is an edit form, I'd prefer not to have to tell them to re-enter files that they have already entered and which don't need to be changed.

So I figured that the best way to deal with this is to now check if the contents of the form field contain a "\" - there's guarenteed to be one of them in pretty much any file path as far as I've ever seen. Is this the best idea? If so, how do I do it? I can't find anything in the documentation to suggest that there is a "contains" function or something similar (ie, formfield contains "\").

Or can anyone suggest a better way to do this?

Cheers,

Seona.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're still getting the full original path of the file being uploaded? :? well. you can check if a string exists inside another string with strpos()
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

No, don't worry, it's not that problem again. :) This migth make it a little clearer. What I have is:

Code: Select all

if ($p_Image != "") {
		$ImagePath = "_images/db_titles/" . $_FILESї'Image']ї'name'];
	} else {
		$sqlquery = "SELECT * FROM $table WHERE ID = $p_ID";
		$result = mysql_query($sqlquery,$link);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$ImagePath = $rowї"TitleImage"];
	}
The idea is that if the field has data in it then it gets the details of the file and adds the correct path prefix. If the field is empty, it gets the data from the database and uses that instead. This way, the update query just sets that field in the record to $ImagePath and I don't have to worry about overwriting the original data if I'm only updating one image out of the three.

This worked fine before, but now I'm guessing that with the addition of the enctype to the form, the file inputs are no longer being sent as empty strings if they aren't actually used. Therefore, it always runs the first option.

What I need is a different check that I can perform on these fields to see if the user has actually selected a new file to upload (and therefore add to the database).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's been a while since I've dealt directly with file uploads, but I seem to remember using empty() against the $_FILES array, or the $_FILES['Image'] element.. potentially, you can check the $_FILES['Image']['error'] code return.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

Here is some of my code i am using for file uploads.. :D
It's only part, but it'll get you an idea how i handle file uploads.. :D

Code: Select all

if (!empty($_FILESї'mp3file']ї'name']))
				{
					if (empty($err)) //Checks for other fields, like name, email if empty then go on!
						{
						if (is_uploaded_file($_FILESї'mp3file']ї'tmp_name']))
							{
	    					$filetype = $_FILESї'mp3file']ї'type'];
							$filesize = $_FILESї'mp3file']ї'size'];
							$filename = $_FILESї'mp3file']ї'name'];
							if($filetype != "audio/mpeg") 
								{
								//Bestand is geen mp3 file!
								$err++;
								$mid=1;
								}
								else
								{
								if (!ereg(".mp3",$filename))
									{
									$err++;
									$mid=3;
									}
								}
							if (empty($err))
								{
								if (!is_dir($bestands_locatie.$filename))
									{
									if (file_exists($bestands_locatie.$filename))
										{
											unlink($bestands_locatie.$filename);										}
									}

etc. etc. :D
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Thanks. :) That empty() thing worked a treat and is probably a lot more fool-proof than the method I was considering (although knowing my luck I'll just encounter a better class of fool...).

Hopefully this ends my trials and tribulations with file uploads. If not, I'll be posting again. ;)
Post Reply