0

我有这个烦人的问题:

下面你会看到我凌乱的上传脚本,每当我上传视频文件时,我都会使用 md5 函数来命名它,因此我的数据库/文件夹中没有同名的视频文件。

问题是当我多次将给定文件上传到数据库时,它每次都以相同的 md5 文件名存储。如果您能帮我修复这个小错误,我将不胜感激。我可能累了或其他什么。我尝试了一百种不同的解决方案,但没有任何解决方法。

这是我的烂摊子:

<?php

class Upload_model extends CI_Model {

    var $gallery_path;
    var $videos_path;
    var $thumbnail;
    var $video_name;
    var $upload_data;
    var $file_name;
    var $name;
    var $videos_folder = "http://localhost/upload/videos/";

    //////////////////////////////


    function Upload_model() {

        parent::__construct();
        $this->videos_path = realpath(APPPATH . '..\videos');
//        $this->returnFromDatabase();
    }

    function do_upload() {

        $name = $_FILES['userfile']['name']; // get file name from form
        $fileNameParts = explode(".", $name); // explode file name to two part
        $fileExtension = end($fileNameParts); // give extension
        $fileExtension = strtolower($fileExtension); // convert to lower case
        $encripted_pic_name = md5($name) . "." . $fileExtension;  // new file name
        $config['file_name'] = $encripted_pic_name; //set file name


        $config = array(
            'allowed_types' => 'avi|mp4|flw|mov',
            'upload_path' => $this->videos_path,
            'file_name' => $encripted_pic_name
        );

        $this->load->library('upload', $config);



        if ($this->upload->do_upload()) {


            $this->upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
            $this->file_name = $this->upload_data['file_name'];


            $this->getThumbImage('C:\\xampp\\htdocs\\upload\\videos\\' . $encripted_pic_name);


            $insert_data = array(
                'name' => $encripted_pic_name,
                'path' => 'C:\\xampp\\htdocs\\upload\\videos\\' . $encripted_pic_name,
                'thumb_path' => 'C:\\xampp\\htdocs\\upload\\videos\\' . $encripted_pic_name . "_t.jpeg",
                'uploaded_by' => 'admin'
            );

            $this->db->insert('videos', $insert_data); //load array to database 

            redirect('/welcome');
        }
    }

    function getVideoInformation($videoPath) {
        $movie = new ffmpeg_movie($videoPath, false);

        $this->videoDuration = $movie->getDuration();
        $this->frameCount = $movie->getFrameCount();
        $this->frameRate = $movie->getFrameRate();
        $this->videoTitle = $movie->getTitle();
        $this->author = $movie->getAuthor();
        $this->copyright = $movie->getCopyright();
        $this->frameHeight = $movie->getFrameHeight();
        $this->frameWidth = $movie->getFrameWidth();
        $this->pixelFormat = $movie->getPixelFormat();
        $this->bitRate = $movie->getVideoBitRate();
        $this->videoCodec = $movie->getVideoCodec();
        $this->audioCodec = $movie->getAudioCodec();
        $this->hasAudio = $movie->hasAudio();
        $this->audSampleRate = $movie->getAudioSampleRate();
        $this->audBitRate = $movie->getAudioBitRate();
    }

    function getAudioInformation($videoPath) {
        $movie = new ffmpeg_movie($videoPath, false);

        $this->audioDuration = $movie->getDuration();
        $this->frameCount = $movie->getFrameCount();
        $this->frameRate = $movie->getFrameRate();
        $this->audioTitle = $movie->getTitle();
        $this->author = $movie->getAuthor();
        $this->copyright = $movie->getCopyright();
        $this->artist = $movie->getArtist();
        $this->track = $movie->getTrackNumber();
        $this->bitRate = $movie->getBitRate();
        $this->audioChannels = $movie->getAudioChannels();
        $this->audioCodec = $movie->getAudioCodec();
        $this->audSampleRate = $movie->getAudioSampleRate();
        $this->audBitRate = $movie->getAudioBitRate();
    }

    function getThumbImage($videoPath) {
        $movie = new ffmpeg_movie($videoPath, false);
        $this->videoDuration = $movie->getDuration();
        $this->frameCount = $movie->getFrameCount();
        $this->frameRate = $movie->getFrameRate();
        $this->videoTitle = $movie->getTitle();
        $this->author = $movie->getAuthor();
        $this->copyright = $movie->getCopyright();
        $this->frameHeight = $movie->getFrameHeight();
        $this->frameWidth = $movie->getFrameWidth();

        $capPos = ceil($this->frameCount / 4);

        if ($this->frameWidth > 120) {
            $cropWidth = ceil(($this->frameWidth - 120) / 2);
        } else {
            $cropWidth = 0;
        }
        if ($this->frameHeight > 90) {
            $cropHeight = ceil(($this->frameHeight - 90) / 2);
        } else {
            $cropHeight = 0;
        }
        if ($cropWidth % 2 != 0) {
            $cropWidth = $cropWidth - 1;
        }
        if ($cropHeight % 2 != 0) {
            $cropHeight = $cropHeight - 1;
        }

        $frameObject = $movie->getFrame($capPos);


        if ($frameObject) {
            $imageName = $this->file_name . "_t.jpeg";
            $tmbPath = "C:\\xampp\\htdocs\\upload\\videos\\" . $imageName;
            $frameObject->resize(120, 90, 0, 0, 0, 0);
            imagejpeg($frameObject->toGDImage(), $tmbPath);
        } else {
            $imageName = "";
        }


        return $imageName;
    }

}
4

4 回答 4

1

md5 在同一个字符串上使用时总是会返回相同的值,所以上传同名文件最终会得到相同的哈希值,在文件名中添加一个随机字符串

$encripted_pic_name = md5(microtime() . $name) . '.' . $fileExtension

您还需要注意 md5() 可能会发生冲突,其中两个不同的字符串将具有相同的输出。不过,出于您的需要,我不会太担心这一点。

于 2013-05-03T10:45:53.937 回答
1

重要的是,CI 的上传类有一个名为encrypt_name. 您可以将其设置为 true 并默认加密您的文件名,而无需执行其他操作。看看:http
://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html 另外,由于您使用的是CI,请使用上传类,当CI提供的类是时不要自己编写很容易使用。

于 2013-05-03T10:49:29.990 回答
0

在 md5 之前,您需要在名称上附加一些随机字符(尽管即使那样,它仍然有可能出现重复名称)。

例如:

$encripted_pic_name = md5($name . $randomStringHere) . "." . $fileExtension;  // new file name

md5 将始终为相同的输入提供相同的输出,因此如果您两次上传相同的文件,那么是的,您已经使用了该哈希。

如果您只想要随机名称,那么您甚至不需要使用 MD5,并且可以在每次上传文件时动态创建一个随机字符串(a、b、c 等到 z,然后是 aa、ab、ac 等) .

于 2013-05-03T10:42:39.683 回答
0

如果文件名相同,那么它将返回相同md5 hash 为此您必须encrypt unique name or number

您可以使用

$encripted_pic_name = md5(strtotime()) . "." . $fileExtension;  // new file name

或者

$encripted_pic_name = md5(uniqid()) . "." . $fileExtension;  // new file name
于 2013-05-03T11:01:49.837 回答