doubu1853 2015-06-16 12:32
浏览 105
已采纳

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

So I am using this script to upload a file to a directory and show it live.

<?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
            }
    }
?>

The thing I am worried about is that if a person uploads a file with the same name as another person, it will overwrite it. How would I go along scraping the filename from the url and just adding a random string in place of the file name.

Explanation: When someone uploads a picture, it currently shows up as

www.example.com/%filename%.png.

I would like it to show up as

www.example.com/randomstring.png

to make it almost impossible for images to overwrite each other.

Thank you for the help, A php noob

  • 写回答

1条回答 默认 最新

  • douqie6454 2015-06-16 13:10
    关注

    As contributed in the comments, I added a timestamp to the end of the filename like so:

    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');
    

    Thank you for the help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题