denhamd2 Posted August 14, 2006 Share Posted August 14, 2006 I have an image upload form and script which uploads images into the specified directory on my server. However when I upload an image with spaces in the name it causes problems when I want to access it in my browser. Is there any way of replacing spaces with "_"? Here is my code:Upload Form:[CODE]<form name="form1" method="post" action="image_add_2-new.php" enctype="multipart/form-data"> <input type="file" name="imagefile"> <br> <input type="submit" name="Submit" value="Submit"></form>[/CODE] Script on the next page which uploads the photo to the server:[CODE]if(isset( $Submit )) { //If the Submitbutton was pressed do: copy ($_FILES['imagefile']['tmp_name'], "../images/photos/".$_FILES['imagefile']['name']) or die ("Could not copy"); echo ""; echo "The photo <b>".$_FILES['imagefile']['name']."</b>"; echo " (Size: ".$_FILES['imagefile']['size']."kb)"; echo " has been successfully added for this property."; } else { echo "<br><br>"; echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>"; } ?> [/CODE] Link to comment https://forums.phpfreaks.com/topic/17511-image-upload-naming-problem/ Share on other sites More sharing options...
shocker-z Posted August 14, 2006 Share Posted August 14, 2006 [code]change to[code]if(isset( $Submit )) { //If the Submitbutton was pressed do: $filename=str_replace(' ', '_',$_FILES['imagefile']['name']);copy ($_FILES['imagefile']['tmp_name'];, "../images/photos/".$filename) [/code] or die ("Could not copy"); echo ""; echo "The photo <b>".$filename."</b>"; echo " (Size: ".$_FILES['imagefile']['size']."kb)"; echo " has been successfully added for this property."; } else { echo "<br><br>"; echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>"; } ?> [/code] RegardsLiam Link to comment https://forums.phpfreaks.com/topic/17511-image-upload-naming-problem/#findComment-74481 Share on other sites More sharing options...
denhamd2 Posted August 14, 2006 Author Share Posted August 14, 2006 Thanks but that didn't work. It still leaves the space in the image.Anyone got any ideas? Link to comment https://forums.phpfreaks.com/topic/17511-image-upload-naming-problem/#findComment-74489 Share on other sites More sharing options...
AndyB Posted August 14, 2006 Share Posted August 14, 2006 Probably the space is %20 (ASCII equivalent of a space) not "just a space". Try/add:[code]$filename=str_replace('%20', '_',$_FILES['imagefile']['name']);[/code] Link to comment https://forums.phpfreaks.com/topic/17511-image-upload-naming-problem/#findComment-74496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.