Hello people!
I would like to know how to remove white space and replace them with a "_" on the file uploaded by this script.

<?php

// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('xxxxxx', 'xxxxxxx', 'xxxxxxxx', 'xxxxxxxx');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }
 $userid = $_SESSION['loginid']; //login session
$target = "f/".$userid."_"; 
$target = $target . basename( $_FILES['uploaded_file']['name']); 
        // Gather all required data
		$description = $_POST['description'];
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
     // delete this line   $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);
 

	if($description)
{
	if (strlen($description)>400)
		echo "Description can't be more than 400 characters!<br><a href='index.php?page=user_files2'>[Go back]</a>";
		else
		{

        // Create the SQL query
       //add userid to your database delete the data entity.
        $query = "
            INSERT INTO `userfile2` (
              `userid`,  `name`, `mime`, `description`, `size`, `created`
            )
            VALUES ('{$userid}',
                '{$name}', '{$mime}', '{$description}', {$size}, NOW()
            )";
 
        // Execute the query
        $result = $dbLink->query($query);
		
 
        // Check if it was successfull
        if($result) {
            move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target);
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
}
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }
$file1 = $_FILES['uploaded_file']['name'];
    // Close the mysql connection
    
}
else {
    echo 'Error! Your file was not sent!';
}
 
// Echo a link back to the main page
echo '';
}
$dbLink->close();
?>
<br>
<a href="f/<?php echo "" . $userid . "_" . $file1?>">Download</a><br>
<p><font color="#000000"><b>Direct Link:</b></font></p>
<input type="text" value="www.xxxx.com/f/<?php echo "" . $userid . "_" . $file1?>" class="button1">

I have no clue how to do it and i just hope some of you got a snippet, my list for the user's files wont work without it!
/peace

See str_replace().

I might be real bad at understanding but i just dont see how to implent that into my code :/

Member Avatar for diafol

The str_replace() function takes 4 parameters: search, replace, subject, count

search = term to search for, in your case: " "
replace = term to replace search term with, in your case: "_"
subject = the string itself (filename): $file
count = no of instances - you can leave this out

$file = str_replace(" ","_",$file);

The str_replace() function takes 4 parameters: search, replace, subject, count

search = term to search for, in your case: " "
replace = term to replace search term with, in your case: "_"
subject = the string itself (filename): $file
count = no of instances - you can leave this out

$file = str_replace(" ","_",$file);

Thanks Ardav, thou i dont want to bother you with my noobness but i tried something like this;

$replace = $_FILES['uploaded_file']['name'];
$replace = str_replace(" ","_",$replace);

before the injection to mysql but it would not work in any direction i did it. :/

Member Avatar for diafol

Try echoing out $replace before doing anything to see if it is empty or not.

$replace = $_FILES['uploaded_file']['name'];
echo "before: " . $replace . "<br />";
$replace = str_replace(" ","_",$replace);
echo "after: " . $replace;

@pritaeas
Sorry mate, I seem to have hijacked a solution you were giving. That was rude.

commented: Never mind, you were online, I wasn't +7

Cant make it work into my code, so i'll just forget about it.
Frustrating! Thanks anyways.

Member Avatar for P0lT10n

Has you wrote enctype="multipart/form-data" on your form ?

Has you wrote enctype="multipart/form-data" on your form ?

Yes i have

Member Avatar for diafol

What came out of the code I suggested (the echoing)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.