Jump to content


Photo

Image Upload Naming Problem


  • Please log in to reply
3 replies to this topic

#1 denhamd2

denhamd2
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 14 August 2006 - 02:10 PM

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:
<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>


Script on the next page which uploads the photo to the server:
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>"; 
        }
		
		?> 


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 14 August 2006 - 02:15 PM

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>"; 
        }
		
		?>



Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 denhamd2

denhamd2
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 14 August 2006 - 02:32 PM

Thanks but that didn't work. It still leaves the space in the image.

Anyone got any ideas?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 August 2006 - 02:53 PM

Probably the space is %20 (ASCII equivalent of a space) not "just a space".  Try/add:

$filename=str_replace('%20', '_',$_FILES['imagefile']['name']);

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users