Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-重命名文件以禁止重复_Php_File_Integer_Renaming - Fatal编程技术网

PHP-重命名文件以禁止重复

PHP-重命名文件以禁止重复,php,file,integer,renaming,Php,File,Integer,Renaming,所以我使用这个脚本将一个文件上传到一个目录并实时显示 <?php function UploadImage($settings = false) { // Input allows you to change where your file is coming from so you can port this code easily $inputname = (isset($settings['inpu

所以我使用这个脚本将一个文件上传到一个目录并实时显示

<?php
    function UploadImage($settings = false)
        {
            // Input allows you to change where your file is coming from so you can port this code easily
            $inputname      =   (isset($settings['input']) && !empty($settings['input']))? $settings['input'] : "fileToUpload";
            // Sets your document root for easy uploading reference
            $root_dir       =   (isset($settings['root']) && !empty($settings['root']))? $settings['root'] : $_SERVER['DOCUMENT_ROOT'];
            // Allows you to set a folder where your file will be dropped, good for porting elsewhere
            $target_dir     =   (isset($settings['dir']) && !empty($settings['dir']))? $settings['dir'] : "/uploads/";
            // Check the file is not empty (if you want to change the name of the file are uploading)
            if(isset($settings['filename']) && !empty($settings['filename']))
                $filename   =   $settings['filename'] . "sss";
            // Use the default upload name
            else
                $filename   =   preg_replace('/[^a-zA-Z0-9\.\_\-]/',"",$_FILES[$inputname]["name"]);
            // If empty name, just return false and end the process
            if(empty($filename))
                return false;
            // Check if the upload spot is a real folder
            if(!is_dir($root_dir.$target_dir))
                // If not, create the folder recursively
                mkdir($root_dir.$target_dir,0755,true);
            // Create a root-based upload path
            $target_file    =   $root_dir.$target_dir.$filename;
            // If the file is uploaded successfully...
            if(move_uploaded_file($_FILES[$inputname]["tmp_name"],$target_file)) {
                    // Save out all the stats of the upload
                    $stats['filename']  =   $filename;
                    $stats['fullpath']  =   $target_file;
                    $stats['localpath'] =   $target_dir.$filename;
                    $stats['filesize']  =   filesize($target_file);
                    // Return the stats
                    return $stats;
                }
            // Return false
            return false;
        }
?>

<?php
    // Make sure the above function is included...
    // Check file is uploaded
    if(isset($_FILES["fileToUpload"]["name"]) && !empty($_FILES["fileToUpload"]["name"])) {
        // Process and return results
        $file   =   UploadImage();
        // If success, show image
        if($file != false) { ?>
            <img src="<?php echo $file['localpath']; ?>" />
        <?php
            }
    }
?>

" />
我担心的是,如果一个人上传一个与另一个人同名的文件,它会覆盖它。我如何从
url
中删除文件名,然后添加一个随机字符串来代替文件名

说明:当有人上传图片时,它当前显示为

www.example.com/%filename%.png

我希望它显示为

www.example.com/randomstring.png

使图像几乎不可能相互覆盖

谢谢你的帮助,
一个php noob

在注释中,我在文件名的末尾添加了一个时间戳,如下所示:

if(isset($settings['filename']) && !empty($settings['filename']))
                $filename   =   $settings['filename'] . "sss";
            // Use the default upload name
            else
                $filename   =   preg_replace('/[^a-zA-Z0-9\.\_\-]/',"",$_FILES[$inputname]["name"]) . date('YmdHis');

感谢您的帮助

最好将文件与
时间戳
日期
一起保存为
$filename=$settings['filename'].date('YmdHis');
。使用这种方式,您将始终获得一个唯一的文件名您可以使用时间戳(time())并添加一个randomint(mt_rand())因此,几乎不可能获得相同的文件名