php上传后无法删除文件名中的空格

时间:2018-07-05 08:55:48

标签: php upload rename

我有一个有效的php上传脚本,但是在上传后重命名文件的行被忽略/不起作用。

这是相关部分:

        if (!file_exists($uploaddir . $_FILES["file"]["name"]))
        {
            // Proceed with file upload
            if (is_uploaded_file($_FILES['file']['tmp_name']))
            {
                //File was uploaded to the temp dir, continue upload process
                if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name']))
                {
                    //chown ($uploaddir . $_FILES['file']['name'], 'www-data');
                    //chmod ($uploaddir . $_FILES['file']['name'], 0755);
                    $name = str_replace(" ", "_", $uploaddir . $_FILES['file']['name']);
                    // uploaded file was moved and renamed succesfuly. Display a message.
                    // Now log the uploaders IP adress date and time
                    $date = date("m/d/Y"); 
                    $time = date("h:i:s A");                
                    $fp = fopen($log,"ab"); 
                    fwrite($fp,"$ip | ".$_FILES['file']['name']." | $date | $time | OK"."\r\n"); 
                    fclose($fp); 

                    $to = 'mail@sjn.net';
                    $subject = 'Upload Completed'; 
                    $headers = "From: mail@sjn.net\nContent-Type: text/html; charset=iso-8859-1";

                    ob_start();
                ?>

                    <p>A file has been succesfully uploaded! - https://www.sjn.net/upload/<?php echo $_FILES['file']['name']; ?></p>

                <?php
                    $message = ob_get_clean();
                    $mail_sent = @mail( $to, $subject, $message, $headers );
                    echo "Upload erfolgreich! Link zur Datei:<br><input size='50' type='text' value='https://www.sjn.net/upload/".$_FILES['file']['name']."' id='myInput'><button onclick='myFunction()'>in Zwischenablage</button><script>function myFunction() {var copyText = document.getElementById('myInput');copyText.select();document.execCommand('copy');}</script>";

                }

如您所见,我已经尝试使用权限,但没有更改。 没有重命名文件替换空格是什么?

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,我刚刚更改了空间,并在move_uploaded_file()函数之前使用下划线替换了该空间,并使用了该变量。如果适合您,请尝试使用它。

   if (!file_exists($uploaddir . $_FILES["file"]["name"]))
    {
        if (is_uploaded_file($_FILES['file']['tmp_name']))
        {
            $filename = str_replace(' ', '_', $_FILES['file']['name']);

            if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename))
            {
                $name = str_replace(" ", "_", $uploaddir . $filename);

                $date = date("m/d/Y"); 
                $time = date("h:i:s A");                
                $fp = fopen($log,"ab"); 
                fwrite($fp,"$ip | ".$filename." | $date | $time | OK"."\r\n"); 
                fclose($fp); 

                $to = 'mail@sjn.net';
                $subject = 'Upload Completed'; 
                $headers = "From: mail@sjn.net\nContent-Type: text/html; charset=iso-8859-1";

                ob_start();
            ?>

                <p>A file has been succesfully uploaded! - https://www.sjn.net/upload/<?php echo $filename; ?></p>

            <?php
                $message = ob_get_clean();
                $mail_sent = @mail( $to, $subject, $message, $headers );
                echo "Upload erfolgreich! Link zur Datei:<br><input size='50' type='text' value='https://www.sjn.net/upload/".$filename."' id='myInput'><button onclick='myFunction()'>in Zwischenablage</button><script>function myFunction() {var copyText = document.getElementById('myInput');copyText.select();document.execCommand('copy');}</script>";

            }
        }

    }