Jump to content

Archived

This topic is now archived and is closed to further replies.

denhamd2

Image Upload Naming Problem

Recommended Posts

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]

Share this post


Link to post
Share on other sites
[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]


Regards
Liam

Share this post


Link to post
Share on other sites
Thanks but that didn't work. It still leaves the space in the image.

Anyone got any ideas?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.