Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding filename to database as text

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I want to add a browse button where the user enters the filename to be inserted in the database. I have the upload area working, but need a more accurate way of adding the filename to the database. Can you add a browse button somehow to the filename text box? If not, what is another way I can get a more accurate name call.
 
you can just use a file field and ignore the $_FILES superglobal. most (but NB not all) browsers will send the actual filename entered in the headers and as such this data will be retrievable in the $_POST/$_GET superglobal.

but ... as i said ... this aint foolproof. not all browsers send it. alternatives are to write/use your own js file browsser or use a file field outside of the form tag and attach some js to copy the file name into a hidden field within the form tag. again not foolproof as a client can turn the js off.

however this is seldom a problem as file names and paths are typically only useful to php if they reside on machines that php can access. mostly servers cannot access clients. if it is the case that you want to allow the user to locate and store the location of a file on the server then the world is a different more rosier place. you can, for example, use a select box or a series of select boxes to allow users to choose a file, or an elementary file browser or just a list of your own devising.
 
I wrote some code just for this, however, like jp says, you can't always count on it when it's javascript, and with file fields, i've come to find out not all browsers work the same. I found out Safari doesn't even show the Path, it just a browse button and there is no input, it somehow just writes the filename to the page.

Anyway, this code will take the filename from file input and put it into another input.

onblur="doFilename();"

Code:
function doFilename(){
	var text = document.uploadfile.upload.value;
	var x = text.split("\\");
	var len = x.length - 1;
	if(len == 0){
		x = text.split("::");
		len = x.length - 1;
	}
	if(len == 0){
		x = text.split("/");
		len = x.length - 1;
	}
	if(len == 0){
		document.uploadfile.caption.value = text;
	} else {
		document.uploadfile.caption.value = x[len];
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top