Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    filename from upload

    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?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    that doesnt help me... just fills my page with javascript errors.... is there a way to do it in php?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Did you change formname and elementname to the values that you are using?

    and integrate it in your script

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    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

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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
    Last edited by Mr J; 04-03-2004 at 07:38 PM.

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    so if i did....
    --------------------------
    PHP Code:
    <?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.

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    To get a file name, use $_FILES['uploadfieldname']['name']. You don't need javascript, plus that wont work.

  • #10
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    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

  • #11
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    It just gets the name of the file:

    http://www.php.net/manual/en/feature...ad.post-method


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •