Page 1 of 1

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

Posted: Tue Jan 11, 2005 10:42 pm
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.

Posted: Tue Jan 11, 2005 10:58 pm
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()

Posted: Tue Jan 11, 2005 11:17 pm
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).

Posted: Tue Jan 11, 2005 11:47 pm
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.

Posted: Wed Jan 12, 2005 12:25 am
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

Posted: Wed Jan 12, 2005 3:43 pm
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. ;)