Jump to content

Remove space from file name automatically


zahid

Recommended Posts

My problem is that whenever i wanna download a file it shows theres no file named (suppose file name "new file" then it will show "example.com/file/new+file" not available.Those files creates this problem which files name have space.But those file's name which don't have a space download successfully.So how can i make all the space into a underscore? is there any procedure to make such script or should I make all file into a inverted comma if so then how to do that?Thanks in advanceZahid Choudhury

Link to comment
Share on other sites

Something is using the wrong URL escape scheme... A space is supposed to be turned to a + in the query string, but in the actual URL (before the ?) it should change to %20. What's the (relevant) PHP on the download page look like?

Link to comment
Share on other sites

Something is using the wrong URL escape scheme... A space is supposed to be turned to a + in the query string, but in the actual URL (before the ?) it should change to %20. What's the (relevant) PHP on the download page look like?
I actually dont understood what you are trying to say.What i did, I make my apache server file list into a php file which shows the list of files & when clicked it starts download.I dont understand this also
$file_name2 = preg_replace('/\s/', '_', $file_name1);$result = rename ($file_name1, $file_name2);
should i put this code in to my php file? where should i place the paths?
Link to comment
Share on other sites

You asked how to change the spaces into an underscore. The first line of code I showed you does that. It uses a regular expression in which the escape sequence \s represents a space. The second line actually renames the file.Yes, it should go into a PHP script. You could quickly write a script that would rename ALL your files in a batch job and you'd never have the problem again. Somehow I thought that's what you wanted.I only mentioned paths to remind you that if the files to be renamed are in a different directory than your script, then you will have to use a path to refer to the files. You might do something like this:$file_name_1 = $my_path . $file_name_1;I don't know your directory structure, so I can't do much more than that.

Link to comment
Share on other sites

I actually dont understood what you are trying to say.
You seem to be using urlencode on the filename to make it computer-friendly, but your server doesn't understand a plus sign (inside the filename) as a space. To get around this, use the fully standards-compliant rawurlencode and your file will be found regardless of any spaces.However, it's really not a good idea to put a space inside a filename anyway. But the above will prevent this problem in case you happen to have such a file on your server.
Link to comment
Share on other sites

yea, or you could str_replace.

function replace_space($search, $replace, $file){ $file = str_replace($search, $replace, $file); return $file;}

//execute the functionreplace_space(' ','_', 'hello world.txt');//It should come out like 'hello_world.txt'

Link to comment
Share on other sites

Or just

//execute the functionstr_replace(' ','_', 'hello world.txt');//It should come out like 'hello_world.txt'

(onno just "renamed" the function)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...