如何为我的图像提供一个独特的名称

时间:2018-01-14 16:49:54

标签: php hash phpmyadmin

我正在创建一个社交网络,我想知道如何在文件夹和数据库中散列图像名称。首先,我想在文件夹中散列名称,然后在数据库中上传散列ID。我在上一篇文章中使用此代码> get_people(family) name age eyes hair 1 Alice 40 blue <NA> 2 Bob 20 blue <NA> 3 Charlie 18 brown <NA> 4 Dan 12 green <NA> 5 Erin 69 green <NA> 6 Frank 45 blue <NA> 7 George 24 blue <NA> 8 Harry 2 green <NA> 9 Ingrid 22 brown brown 10 Jack 29 brown <NA> 11 Karen 43 <NA> <NA> 12 Larry 21 blue <NA> 获得了一些帮助,但我不知道我的代码中的位置。有人能帮我吗 ?这是我的代码:

upload.php的:

hash_file('sha256', $filename);

1 个答案:

答案 0 :(得分:0)

用户Copy()

            <?php $username = $_SESSION['username'];
            $target_dir = "images/uploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

            // Check if image file is a actual image or fake image

            if(isset($_POST["change"])) {

            hash_file('sha256', $sql);

//randed name in Int
            $name_randed=mt_rand();
            //A FUNCTION TO GET THE Extension of the file

            function EXTENSION($sr){
            $path_parts = pathinfo($sr);
            $exte='.'.$path_parts['extension'];
            return $exte;
            ;}

            //end of extension

            //copy upload file

    copy($_FILES["fileToUpload"]["tmp_name"], $target_dir.$name_randed.$exte);

            //end

        //name you can store in DB

        $name_db=$new_name.$exte;
    //end
            $sql = "UPDATE users SET userPic = '".$name_db."' WHERE username = '" . $username . "'";

            $check = $conn->query($sql);

            if($check !== false) {
            echo "<a href = profile.php> Profile pciture has been changed </a>" . 
            $check["mime"] . ".";
            $uploadOk = 1;

            } else {
            echo "File is not an image.";
            $uploadOk = 0; 
            }
            } else {

            echo"did not change";
            }

    ;?>