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

また、2 つの異なる文字列が同じ出力を持つ 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 は常に同じ入力に対して同じ出力を提供するため、同じファイルを 2 回アップロードすると、そのハッシュは既に使用されています。

ランダムな名前だけが必要な場合は、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 に答える