简体   繁体   English

如何使用 php 从服务器上的文件名中去除斜杠

[英]How to stripslashes from the names of file on server with php

Sequel to my question here: Addslashes displays as forward slashes in php ,我的问题的续集: Addslashes 在 php 中显示为正斜杠

I want to strip slashes from the filenames of files i have on my server.我想从我的服务器上的文件的文件名中删除斜杠。 the slashes were added during file upload (magic_quotes).斜线是在文件上传期间添加的(magic_quotes)。

Please how can i go about this?请问我怎么能 go 关于这个? thanks谢谢

Here you go:这里是 go:

<?php
$path         =  '/path/to/files/dir/';
$file_types   =  'txt,doc,pdf';

foreach (glob($path.'*.{'.$file_types.'}', GLOB_BRACE) as $filename){
    if(rename($filename , stripslashes($filename))){
        echo 'Renamed file from '.$filename.' to '.stripslashes($filename).'<br />';
    } else{
        echo 'Failed to rename file from '.$filename.' to '.stripslashes($filename).'<br />';
    }
}
?>

Change path to files and the comma separated list of file types.更改文件的路径和以逗号分隔的文件类型列表。

Update with asker's code on the comments:更新评论中的提问者代码:

$dir='cv'; 
if(is_dir($dir)){ 
    if ($dh = opendir($dir)) { 
        while (false !== ($file = readdir($dh))) { 
            if ($file != "." && $file != "..") { 
                $file2 = $dir."/".$file; $newfile=$dir."/".stripslashes(urldecode($file)); 
                if(rename($file2, $newfile)){
                    echo "renamed from $file2 to $newfile <br>";
                } else{
                    echo "error renaming from $file2 to $newfile <br>";
                } 
            } 
        } 
        closedir($dh); 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM