Thursday 1 May 2008

Making sure that theres no gaps in the filenames of image uploads.

The problem that needed to solved here was that users could upload there own images - and if the filename they were uploading has gaps or whitespace.
It took me a while to get my head around this one, eventhough the solution is fairly simple.
If you have solving the same problem here are a couple of links that may help. Firstly I posted this problem on a developer network forum. you can see that post at http://forums.devnetwork.net/viewtopic.php?f=1&t=82057 .
A tutorial that helps understand how file upload works in php is http://www.tizag.com/phpT/fileupload.php.

Any back to my code.

What I needed to do first was check to check if theres any spaces and replace with '_'

[code]
$img1 = str_replace ( ' ', '_', $img1 );

[/code]

then in move_uploaded_file change it to

[code]
$res = move_uploaded_file($_FILES['img1']['tmp_name'], $abpath .
$img1);
[/code]


staright forward, I have no idea now why I couldn't get my head around this straight away.

No comments: