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
https://forums.phpfreaks.com/topic/17511-image-upload-naming-problem/
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

Archived

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

×
×
  • 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.