从上传的文件名中删除空格和逗号

After using this code for a while, I've found that people are uploading files with spaces and commas.

I've tried the function reg_replace, and the filename uploaded is in fact edited having spaces replaced with underscores as supposed.

I would also need to tell the final file name to the user, so I would need to have that file name inside a textfield that is echoed if upload is successful. This last part is what is missing.

How could this be done within the following context?

<?php

$target_dir = "extra_images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check if file already exists
if (file_exists($target_file)) {
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 3750000) {
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
    $uploadOk = 0;
}

//Check for pdf format
if (!empty($_FILES['fileToUpload']['tmp_name'])) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
    if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) {

        $uploadOk = 0;
        echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>";

        //exit();

    }} //this bracket was missing I think


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
// if everything is ok, try to upload file
} else {

    $target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces...

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

        echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form.";
    } else {
        echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
    }
}
echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
exit();

?>

You can use str_replace() to change space into underscore.

$str = 'hai welcome';
$newstr = str_replace(' ', '_', $str);
echo $newstr;

Now you got output without space;

Well, I´ve managed to put it to work, it now saves the filename to the database which was one of my other needs. The user ip capture also works but only with the $_SERVER['REMOTE_ADDR']; variable I wonder how to use the get_client_ip(). Tried to save the $ipaddress to the db field but did´t worked... Thank you for your ideas and help.

<?php

                $target_dir = "images_folder/";
                $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
                $target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces...
                $uploadOk = 1;
                $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

                // Function to get the client IP address
                function get_client_ip() {
                    $ipaddress = '';
                    if ($_SERVER['HTTP_CLIENT_IP'])
                        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
                    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
                        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
                    else if($_SERVER['HTTP_X_FORWARDED'])
                        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
                    else if($_SERVER['HTTP_FORWARDED_FOR'])
                        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
                    else if($_SERVER['HTTP_FORWARDED'])
                        $ipaddress = $_SERVER['HTTP_FORWARDED'];
                    else if($_SERVER['REMOTE_ADDR'])
                        $ipaddress = $_SERVER['REMOTE_ADDR'];
                    else
                        $ipaddress = 'UNKNOWN';
                    return $ipaddress;
                }
                $ipaddress2=$_SERVER['REMOTE_ADDR'];

                // Check if file already exists
                if (file_exists($target_file)) {
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
                    $uploadOk = 0;
                }
                // Check file size
                if ($_FILES["fileToUpload"]["size"] > 3750000) {
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
                    $uploadOk = 0;
                }

                //Check for pdf format
                if (!empty($_FILES['fileToUpload']['tmp_name'])) {
                    $finfo = finfo_open(FILEINFO_MIME_TYPE);
                    $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
                    if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) {

                        $uploadOk = 0;
                        echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>";

                        //exit();

                    }} //this bracket was missing I think


                // Check if $uploadOk is set to 0 by an error
                if ($uploadOk == 0) {
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
                // if everything is ok, try to upload file
                } else {



                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

                        $cleanFilename=basename("$target_file");

                        // Connects to your Database
                        mysql_connect("localhost", "db_username", "db_pass") or die(mysql_error()) ;
                        mysql_select_db("db_name") or die(mysql_error()) ;

                        //Writes the information to the database
                        mysql_query("INSERT INTO tableName (filename,ipaddress)
                        VALUES ('$cleanFilename', '$ipaddress2')") ;


                        echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\" $cleanFilename \" class=\"form-control input-sm\" style=\"width:320px;\" /></span> And paste it in an empty Extra image field above and save the form. If you see the image after saving you've done right. $cleanFilename";
                    } else {
                        echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
                    }
                }
                echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
exit();
?>