Page 1 of 2

How do I get part of a returned string?

Posted: Tue Jan 04, 2005 5:49 pm
by Seona
Hi guys,

Well, I've searched through the documentation, but it's difficult to look for something when you're not sure what it actually is. :) So hopefully someone here can make sense of my explanation and point me in the right direction.

I have a form which, amongst other things, has a field where a user can select an image to be added to their entry. What I need to do is isolate the part of the value in that field to only get whatever is after the last "\". That way I can prepend the correct directory structure to the front of it before sticking it in the database and not have to worry about the path from the user's computer.

While we're on the subject, where can I find information about how to upload a file to the server via PHP?

Cheers,

Seona.

Posted: Tue Jan 04, 2005 6:05 pm
by feyd
What kind of field is this? A file field, or textbox? Anyway, there are a number of ways to get the filename from a given path: [php_man]basename()[/php_man], [php_man]substr()[/php_man] and [php_man]preg_match()[/php_man].

As for information on uploading: http://www.php.net/manual/en/features.file-upload.php

moved to php - code.

Posted: Tue Jan 04, 2005 6:12 pm
by Seona
Great, basename() seems to be exactly what I need. Thanks.

Thanks for the info on file uploading, too. That will be this afternoon's project. :)

Posted: Tue Jan 04, 2005 6:16 pm
by feyd
the file-upload only gives you the filename though.. so I'm not sure where you are getting the entire path.

Posted: Tue Jan 04, 2005 9:57 pm
by Seona
Hmm... maybe all is not as good as I thought... I am using the following line of code:

Code: Select all

<?php
$ImagePath = "_images/db_titles/" . basename($p_Image);
?>
To the best of my knowledge (and my understanding of the link you posted to the docs on basename() ) this should create a variable called ImagePath which contains the text that I've entered there plus the filename and extension of what was in the file field of the form (which is named Image). What I am getting, however, is the text I entered followed by the full contents of that field - thus giving the full location of the file on my hard drive. Not a useful situation.

What am I doing wrong?

Cheers,

Seona.

Posted: Tue Jan 04, 2005 10:09 pm
by feyd
can you post your form code please? The whole code would potentially be more helpful.

From what I've seen, no browser sends the full path to the server in a file upload field. It just sends the filename. This is for security reasons, and that there should be no reason for the server to know the full path that file had on your machine.

Posted: Tue Jan 04, 2005 11:37 pm
by Seona
Sure, here's the code:

Code: Select all

&lt;form action="act_addCategory.php" method="post" name="loginForm" id="loginForm"&gt;
    &lt;label for="CategoryName"&gt;Category Name:&lt;/label&gt;
    &lt;input name="CategoryName" type="text" id="CategoryName" /&gt;
    &lt;label for="Image"&gt;Title Image:&lt;/label&gt;
    &lt;input name="Image" type="file" id="Image" /&gt;
    &lt;input type="submit" value="Add Category" /&gt;
&lt;/form&gt;
The contents of act_addCategory.php are as follows:

Code: Select all

<?php
	// declare some relevant variables
	$DBhost = "localhost";
	$DBuser = "username";
	$DBpass = "password";
	$DBName = "thisDB";
	$table = "category";
	// this converts the form variables somehow
	import_request_variables('p', 'p_');
	// connect to database
	$link = mysql_connect($DBhost,$DBuser,$DBpass);
	@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 
	
	// Set the correct file path for the image
	$ImagePath = "_images/db_titles/" . basename($p_Image);
	
	
	/*****************************/
	/*    add details to the database    */
	/*****************************/
	// create the query to add data to the client_site table
	$sqlquery = "INSERT INTO $table VALUES ('','" . $p_CategoryName . "','" .  $ImagePath . "')";
	// run the query
	$result = mysql_query($sqlquery,$link);
	
	
	/*****************************/
	/*      send user back to menu      */
	/*****************************/
	
	header('Location: http://dare2.com.au/admin/siteAdmin.php');
?>

It's fair enough to say that the full path is not sent in a file upload, but as well as uploading the actual file (which I haven't gotten to yet) I need to put the file's name into the database. The file being uploaded is an image that relates to the category being created (the title graphic, in this case) and I want to put a reference in the database to where the image lives on the server so that when the category is referenced by the page it is a simple matter to call the appropriate graphic and add it to the page.

So even if I don't need to do this for the actual upload, I still need to get just the filename for the sake of entering it into the database.

Hope that makes things a little clearer.

Cheers,

Seona.

Posted: Tue Jan 04, 2005 11:48 pm
by feyd
only the filename is transmitted.

Posted: Tue Jan 04, 2005 11:58 pm
by Seona
Huh? So how do you explain that the result is being entered into the database as (for example):

_images/db_titles/G:\Clients\Dare2\_images\prices\title_prices.gif

If only the filename is being transmitted, then there shouldn't be all that other garbage.

I'm not sure if you just skimmed what I wrote before and missed a bit, but I'm not dealing with uploading the file yet. I have what is essentially a text string: the path to the file the user wants to upload. The aim here is to just get the last bit (in the above example, title_prices.gif) so that I can put this piece of text ("title_prices.gif") into the database.

I'm getting very confused here. :(

Posted: Wed Jan 05, 2005 12:20 am
by feyd
I have no idea how your getting the full path in that upload.

Posted: Wed Jan 05, 2005 12:23 am
by Seona
Fair enough. Thanks for taking the time to look. :)

Anyone else out there have any ideas??

Posted: Wed Jan 05, 2005 1:04 am
by feyd

Code: Select all

preg_replace('#&#1111;/\\\\\\\\]?(.*?)$#', '\\\\1', $path);

Posted: Wed Jan 05, 2005 1:11 am
by Seona
Err... OK, I'll confess that you've stumped me. I'm fairly new to PHP, so I'm not too sure what that code does or where I should put it.

Is it meant to take the place of the basename() bit?

Posted: Wed Jan 05, 2005 1:15 am
by feyd
yes. $path is your $p_image.. it uses a regular expression to extract the filename. sorry I didn't explain that. :)

Posted: Wed Jan 05, 2005 1:21 am
by Seona
OK, I tried that with the following code:

Code: Select all

<?php
	$ImageName = preg_replace('#[/\\\\]?(.*?)$#', '\\1', $p_Image);
	$ImagePath = "_images/db_titles/" . $ImageName;
?>
Was that the correct way to do it? Because I still seem to be getting the same result. :( I'm sure I'm missing something really obvious somewhere.