jonoc33 Posted November 14, 2007 Share Posted November 14, 2007 Hi guys. I have a script that shows a list of uploaded files, with a button saying Delete. <?PHP $folder = "../files/upload/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "-<a href=$folder$file><label title=file>$file</label></a>"; echo " [<a href=filedelete.php?file=$file>Delete</a>]<br />"; } ?> Now something is very weird, because if you attempt to delete a file that has a space in it, it appears as eg: filename: de_dust A.jpg file comes out as: de_dust I need some way to replace the space with that %20 thing that normally shows up in links if there is a space in it. Much thanks, Jono Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 14, 2007 Share Posted November 14, 2007 You can use trim($file, " "); to remove the whitespace Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 Didn't seem to work. I may have put it in the wrong place. Although, I don't want it to get rid of the space, I need it to keep it there but to still be able to delete it off, if you know what I mean. eg: Uploaded File: de_dust A.jpg Changes to: de_dust%20A.jpg The %20, which i've seen, acts as a space. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 use urlencode http://php.net/urlencode Quote Link to comment Share on other sites More sharing options...
Orio Posted November 14, 2007 Share Posted November 14, 2007 urlencode(). This will make the necessary replacements, and there's no need to decode it on the receiving since PHP does that automatically. Orio. Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 I messed around with urlencode, and I can't seem to get it working. Can someone give me an example using the script I showed above? (Yeah, i'm pretty noobie when it comes to this stuff) Quote Link to comment Share on other sites More sharing options...
Orio Posted November 14, 2007 Share Posted November 14, 2007 <?php echo "<a href=filedelete.php?file=".urlencode($file).">Delete</a><br />"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 try <?PHP $folder = "../files/upload/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "-<a href=$folder".urlencode($file)."><label title=file>$file</label></a>"; echo " [<a href=filedelete.php?file=".urlencode($file).">Delete</a>]<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 Came up as de_dust+a.jpg, which didn't seem to work. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 14, 2007 Share Posted November 14, 2007 So use rawurlencode() instead of urlencode(). Orio. Quote Link to comment Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 Thankyou. That worked Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.