Welcome Guest, Not a member yet? Register   Sign In
Changing name of the uploaded file.
#1

[eluser]DADE[/eluser]
Hello everyone,

I got simple form:

Code:
<?
    echo form_open_multipart('home/upload');
    echo form_input('artist', 'artist').'<br>';
    echo form_input('title', 'title').'<br>';
    echo form_upload('userfile').'<br>';
    echo form_submit('upload', 'upload').'<br>';
    echo form_close();
?&gt;

Than I check for the spaces in the "artist" and "title" fields, and replace them with "_" symbol.

Code:
$artist = trim($this->input->post('artist'));
$title = trim($this->input->post('title'));

$fartist = str_replace(' ', '_', $artist);
$ftitle = str_replace(' ', '_', $title);

$fulltitle = $fartist.'-'.$ftitle.'.mp3';

Here's the result I get:
P.S: For example lets say we've entered "P.O.D" as "artist" and "Boom" as "title"
Code:
echo $fulltitle // Gives me: P.O.D-Boom.mp3

But:

Code:
$config = array(
            'allowed_types' => 'mp3',
            'file_name' => $fulltitle,
            'upload_path' => './music/'
        );
$this->load->library('upload', $config);        
$this->upload->do_upload();

Returns file named:
Code:
P.O_.D-Boom_.mp3
Why?

Thank you.

P.S: CI 2.0.2


ADDED:

Even If I'll do this:
Code:
$test = $this->input->post('test'); // Test = a.s.d.f.

$config = array(
            'allowed_types' => 'mp3',
            'file_name' => $test.'.mp3',
            'upload_path' => './music/',
            'remove_spaces' => TRUE, // True of false I'm still getting a.s_.d_.f_._.mp3
        );
$this->load->library('upload', $config);        
$this->upload->do_upload();

I don't understard why this underscores appears...
#2

[eluser]DADE[/eluser]
Really no ideas? :/
#3

[eluser]DADE[/eluser]
Well, I found solution.

system/libraries/Upload.php file, _prep_filename() function, line 994

Code:
protected function _prep_filename($filename)
    {
        if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
        {
            return $filename;
        }

        $parts        = explode('.', $filename);
        $ext        = array_pop($parts);
        $filename    = array_shift($parts);

        foreach ($parts as $part)
        {
            if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
            {
                $filename .= '.'.$part.'_'; // Line 994.
            }
            else
            {
                $filename .= '.'.$part;
            }
        }

        $filename .= '.'.$ext;

        return $filename;
    }
#4

[eluser]ecitron[/eluser]
Typically, PHP does not alter the names of variables when they are passed into a script. However, it should be noted that the dot (period, full stop) is not a valid character in a PHP variable name. For the reason, look at it:

&lt;?php
$varname.ext; /* invalid variable name */
?&gt;
Now, what the parser sees is a variable named $varname, followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

For this reason, it is important to note that PHP will automatically replace any dots in incoming variable names with underscores.


Try this:
Code:
&lt;?php
var_dump($_POST);
$data = file_get_contents('php://input');
var_dump($data);

?&gt;
&lt;form method="POST" action=""&gt;
&lt;input name="th.is" type="text" /&gt;
&lt;/form&gt;

http://lists.openidenabled.com/pipermail...00395.html

OR


Code:
&lt;?php
// Function to fix up PHP's messing up POST input containing dots, etc.
function getRealPOST() {
    $pairs = explode("&", file_get_contents("php://input"));
    $vars = array();
    foreach ($pairs as $pair) {
        $nv = explode("=", $pair);
        $name = urldecode($nv[0]);
        $value = urldecode($nv[1]);
        $vars[$name] = $value;
    }
    return $vars;
}
?&gt;

Or use a str replace after passing the data again... I think that you're stuck with doing the job twice.

Post the final result if you manage to get it work.

EDIT: Nice Wink




Theme © iAndrew 2016 - Forum software by © MyBB