...

View Full Version : filename from upload



boeing747fp
04-03-2004, 02:16 PM
i need to know how to get C:\Documents and Settings\.....\image.jpg to just be image.jpg when i upload an image... because i want the filename to be added to my database. but since the upload isnt always from C:\Documents and Settings\... it could be from M:\Pics\... and i need to remove all those first parts of the uploaded filename thing. any way to do that?

Mr J
04-03-2004, 03:48 PM
Please try the following


<script>

full_path=document.formname.elementname.value
short_path=full_path.substring(full_path.lastIndexOf("/")+1,full_path.length)
alert(short_path)

</script>

boeing747fp
04-03-2004, 06:06 PM
that doesnt help me... just fills my page with javascript errors.... is there a way to do it in php?

Mr J
04-03-2004, 06:19 PM
Did you change formname and elementname to the values that you are using?

and integrate it in your script

boeing747fp
04-03-2004, 06:26 PM
so u mean like
-------------------------
<script>
full_path=document.form1.file.<?php echo $_POST['file']; ?>
short_path=full_path.substring(full_path.lastIndexOf("/")+1,full_path.length)
alert(short_path)
</script>
-------------------------
this doesnt work... it says that that path is null or doesnt exist

Mr J
04-03-2004, 06:36 PM
Sorry but I don't know about PHP

This works for javascript, I've referenced a couple of divs so you can see


<script language="javascript">
<!--
function upload(){
full_path=document.f1.image_file.value
short_path=full_path.substring(full_path.lastIndexOf("\\")+1,full_path.length)

document.getElementById("temp1").innerHTML="Full path = "+full_path
document.getElementById("temp2").innerHTML="Short path = "+short_path
}
// -->
</script>

<form name="f1" ENCTYPE="multipart/form-data" >
<INPUT type="file" name="image_file">
<INPUT type="button" value="Apply" onclick="upload()">
</form>

<div id="temp1"></div>
<div id="temp2"></div>

This is what I get

Full path = C:\WINDOWS\Desktop\matrix.htm
Short path = matrix.htm

Nightfire
04-03-2004, 06:53 PM
When you upload using php, you don't get any info about the users file directory. The file they upload will be stored in your tmp directory as 1245TMP or similar. The only thing you'll recieve is the filename itself. So I don't understand your problem at all

boeing747fp
04-03-2004, 07:29 PM
so if i did....
--------------------------

<?php
$con = mysql_connect("localhost","user","pass");
mysql_select_db("user_db",$con);
$filename = "<script>
full_path=document.form1.file.value
short_path=full_path.substring(full_path.lastIndexOf("\")+1,full_path.length)";

$sql = "INSERT INTO `files` (`filename`) VALUE ('$filename')";
.......
this will add the short filename into the db? i have the php upload code above this code and it works great. but in my php script that pulls results from the database, it checks the filename and finds the file in the directory it uploads to.

Nightfire
04-03-2004, 07:39 PM
To get a file name, use $_FILES['uploadfieldname']['name']. You don't need javascript, plus that wont work.

boeing747fp
04-03-2004, 08:03 PM
ok
but doesnt $_FILES['uploadfieldname']['name'] give you the C:\Documents......... ? i dont want that in the database because then it will be looking for the directory C:\.... and then everything won't work

Nightfire
04-03-2004, 08:13 PM
It just gets the name of the file:

http://www.php.net/manual/en/features.file-upload.php#features.file-upload.post-method



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum