You would start by writing a function that consolidates multiple spaces into a single space. Then, at the two places where you are moving the uploaded file now, you would instead -
Test if the source file is a valid uploaded file, for security purposes. Move_uploaded_file() did this test for you, but you are no longer using move_uploaded_file().
Read the contents of the source file into a string.
Call the function that consolidates the spaces.
Write the resulting string to the destination file.
// consolidate multiple spaces to a single space
function consolidate_spaces($string)
{
$previous_string = '';
// loop until the strings are the same, no changes
while($string != $previous_string)
{
$previous_string = $string; // remember the starting value
$string = str_replace(' ', ' ', $string); // remove multiple spaces
}
return $string;
}
Example code for where the ‘master’ file move_uploaded_file() statement is at now -
if (is_uploaded_file($_FILES["fileToUpload"]["tmp_name"]))
{
$string = file_get_contents($_FILES["fileToUpload"]["tmp_name"]);
$string = consolidate_spaces($string);
file_put_contents($target_file_master,$string);
} else {
echo "The tmp file was not a valid uploaded file."; // this means that something managed to place a file onto the server and set the $FILES ... ['tmp_name'] variable to it before your code was executed.
}
if($fileType != "csv" ) {
echo "Sorry, only CSV files are allowed. ";
}
else {
if (isset($_POST['master'])) {
(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_master));
}
With
if (is_uploaded_file($_FILES["fileToUpload"]["tmp_name"]))
{
$string = file_get_contents($_FILES["fileToUpload"]["tmp_name"]);
$string = consolidate_spaces($string);
file_put_contents($target_file_master,$string);
} else {
echo "The tmp file was not a valid uploaded file."; // this means that something managed to place a file onto the server and set the $FILES ... ['tmp_name'] variable to it before your code was executed.
}
Have you considered fixing the source of the csv file? The upload process is not adding the spaces. Whatever is generating the file is doing so. Fix that and the problem goes away.
It is really not a problem the extra white spaces it is browser explorer that omit the extra white space and when comparing browser list box with master.csv file it gives error as the browser list box extra white spaces is omitted but in file it is still there. So what I need is to read the master.csv file in an array then remove extra white spaces and then write file