Jump to content

Image Upload Naming Problem


denhamd2

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.