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 Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/ 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 Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391243 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. Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391248 Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 use urlencode http://php.net/urlencode Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391250 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. Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391251 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) Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391254 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. Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391258 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391259 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. Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391264 Share on other sites More sharing options...
Orio Posted November 14, 2007 Share Posted November 14, 2007 So use rawurlencode() instead of urlencode(). Orio. Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391266 Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 Thankyou. That worked Link to comment https://forums.phpfreaks.com/topic/77279-solved-replacing-a-space-with-20/#findComment-391267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.