Page 1 of 1

Replace space with underscore, then upload file

Posted: Fri Dec 18, 2009 10:57 pm
by azylka
Hey guys. I've got a nagging problem. I've got a small file uploading site and I want to replace the spaces in a file name with underscores and then upload that file with the new name. In what way can I do this best? Please include some code, because I don't really understand the script very well. The one thing I think I know is that I need to use

Code: Select all

$replaced = str_replace(" ","_",$target);
but the only thing I don't know is where to substitute it in my script. Any help?

Here's my code:

Code: Select all

<?php
$yn = $_POST['yn'];
$email = $_POST['email'];
$target = "../../uploads/";
$target2 = basename( $_FILES['uploaded']['name']);
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "Please make sure that this is your file: <b>$target2</b><br><br>
<table>
<tr><td><form action=\"../create\" method=\"GET\">
<input name=\"email\" value=\"$email\" type=\"hidden\" />
<input name=\"uploaded\" value=\"$target2\" type=\"hidden\" />
<input name=\"yn\" value=\"$yn\" type=\"hidden\" />
<input type=\"submit\" value=\"Yes, continue uploading\" />
</form></td>
<td> 
<form action=\"../../cancel\" method=\"GET\">
<input name=\"email\" value=\"$email\" type=\"hidden\">
<input name=\"yn\" value=\"$yn\" type=\"hidden\">
<input name=\"file\" value=\"../$target2\" type=\"hidden\" />
<input type=\"submit\" value=\"No, cancel upload\" />
</form></td></tr>
</table> ";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

Re: Replace space with underscore, then upload file

Posted: Fri Dec 18, 2009 11:02 pm
by requinix
azylka wrote:The one thing I think I know is that I need to use

Code: Select all

$replaced = str_replace(" ","_",$target);
but the only thing I don't know is where to substitute it in my script. Any help?
If you had to guess where it would go, where would you put it?

Re: Replace space with underscore, then upload file

Posted: Fri Dec 18, 2009 11:03 pm
by pbs
Use below given function to upload file.

Code: Select all

 
function uploadFile($fieldName, $destPath, $uniqid)
{
    if ($_FILES["$fieldName"]["name"] != "")
    {
         $source = $_FILES["$fieldName"]["tmp_name"];
         $fileName = $uniqid;
         $destination = $destPath.$fileName;
         copy($source, $destination);
         return $fileName;
    }
}
 
$path = "../../uploads/";
$fileName1 = str_replace(" ","_",$_FILES['uploaded']['name']);
$fname1 = uploadFile("uploaded", $path, "1_".$fileName1);
 

Re: Replace space with underscore, then upload file

Posted: Sat Dec 19, 2009 8:17 am
by azylka
tasairis wrote:
azylka wrote:The one thing I think I know is that I need to use

Code: Select all

$replaced = str_replace(" ","_",$target);
but the only thing I don't know is where to substitute it in my script. Any help?
If you had to guess where it would go, where would you put it?
I think I'll replace uploaded with $replaced and try that. I'll update this later after I have.