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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts

    Replaces spaces with underscores in upload form...

    Hi,

    I currently have the following:

    PHP Code:
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 
    How can this be modified so that spaces in the filename are replaced with an underscore?
    Last edited by Daniellez; 10-12-2009 at 07:48 PM.

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    PHP Code:
    $targetFile =  str_replace(' ','_',$targetPath) . $_FILES['Filedata']['name']; 
    :P

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Hi, I need to keep the function that replaces the slashes. Perhaps I should have asked "how do I merge the two?"

    Or can that be added below? Unfortunately, I can't try it until later this evening.

  • #4
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    various ways

    easy/lazy way:

    PHP Code:
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 

    $targetFile =  str_replace(' ','_',$targetPath) . $_FILES['Filedata']['name']; 
    edit: do you perhaps mean
    PHP Code:
    $targetFile =  str_replace('//','/',$targetPath $_FILES['Filedata']['name']); 

    $targetFile =  str_replace(' ','_',$targetPath $_FILES['Filedata']['name']); 
    instead? don't think php will like the original.

  • Users who have thanked met for this post:

    Daniellez (10-12-2009)

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Posts
    156
    Thanks
    47
    Thanked 0 Times in 0 Posts
    Update: The issue manifested itself when you attempted to download a file with spaces in the filename. After a bit of research however, I found another way. Instead of patching this in the upload process, I made the following changes in the download code:

    I changed this:
    PHP Code:
    header("Content-Disposition: attachment; filename=$filename"); 
    To this:
    PHP Code:
    header("Content-Disposition: attachment; filename=\"".$filename."\";" ); 
    I found the fix by viewing the code here. I'm not sure why/how adding the quotes works but it does! :)


  •  

    Posting Permissions

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