在php中给出图片的唯一名称

时间:2018-01-02 06:53:37

标签: php

我去给图片一个唯一的名字,我保存了2张图片,每个图片名称都是唯一的。

此代码工作正常,但当我再次刷新页面图片时,会给出相关名称和一个问题。

$video_type = trim($_GET['video_type']);
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $target_dir = "pictures/";
            $name1 = $_FILES["img_url1"]["name"];
            $name2 = $_FILES["img_url2"]["name"];
            $target_file = $target_dir . basename($_FILES["img_url1"]["name"]);
            $target_filez = $target_dir . basename($_FILES["img_url2"]["name"]);

            $videoFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            if (strtolower(end(explode(".",$name))) =="jpg") {
                $uploadOk = 1;
            } elseif(strtolower(end(explode(".",$name))) =="png" ) {
                $uploadOk = 1;
            }else {
                $uploadOk = 0;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                $_SESSION['error'] = "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["img_url"]["tmp_name"], $target_file) && move_uploaded_file($_FILES["img_urlz"]["tmp_name"], $target_filez)) {
                    $sql = "INSERT INTO picture (pic1, pic2, approved)
                    VALUES ('$name1', '$name2' , 'false')";

                    if ($db->query($sql) === TRUE) {
                        $_SESSION['success'] = 'picture uploaded successfully.';
                    } else {
                        $_SESSION['error'] = $sql . "<br>" . $db->error;
                    }
                } else {
                    $_SESSION['error'] = "Sorry, there was an error uploading your file.";
                }
            }
        }

4 个答案:

答案 0 :(得分:0)

尝试这个

//get picture extension
$ext = pathinfo($_FILES['file']['name'])['extension'];
 //generate the new random string for filename and append extension.
$name1 = generateRandomString().".$ext";

function generateRandomString($length = 10) {
  return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, $length);
}

$name1 = time().'_'.$_FILES['file']['name'];

答案 1 :(得分:0)

请更新您的代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Open Row Sample</title>
    <script src="sdk/scripts/VSS.SDK.js"></script>
</head>
<body>
    <script type="text/javascript">
        VSS.init({
            explicitNotifyLoaded: true,
            usePlatformScripts: true,
            usePlatformStyles: true       
        });
    </script>
    <h1>Open Row Sample</h1>
    <div id="grid-container"></div>
    <script type="text/javascript">
    VSS.require(["VSS/Controls", "VSS/Controls/Grids"],
    function (Controls, Grids) {
    var dataSource = [];
    dataSource.push({key: "VisualStudio", value: "https://www.visualstudio.com/"});
    dataSource.push({key: "Bing", value: "https://www.bing.com/"});

    var grid = Controls.create(Grids.Grid, $("#grid-container"), {
        height: "1000px",
        columns: [
            { text: "Property key", index: "key", width: 150 },
            { text: "Property value", index: "value", width: 600 }
        ],
        source: dataSource,
        openRowDetail: (index) => {
        var info = grid.getRowData(index);
        window.open(info.value);
    }
    });
    VSS.notifyLoadSucceeded();
    });
    </script>
</body>
</html>

然后将$ name1替换为$ new_name1&amp; $ name2 with $ new_name2; 这会给你一个独特的名字。

或者您可以使用:

//确保此imagePath以斜杠

结尾
$new_name1 = $location.time()."-".rand(1000, 9999)."-".$name1;
$new_name2 = $location.time()."-".rand(0, 9999)."-".$name2;

答案 2 :(得分:0)

您好,您可以将这两行代码替换为

$name1 = $_FILES["img_url1"]["name"] . uniqid();
$name2 = $_FILES["img_url2"]["name"] . uniqid();

将始终生成唯一的名称。

希望这有帮助。

答案 3 :(得分:0)

您可以使用内置的PHP

  

uniqid()

用于创建唯一ID并附加到您的图像路径。

$name1 = uniqid().$_FILES["img_url1"]["name"];
$name2 = uniqid().$_FILES["img_url2"]["name"];
$target_file = $target_dir . basename(uniqid().$_FILES["img_url1"]["name"]);
$target_filez = $target_dir . basename(uniqid().$_FILES["img_url2"]["name"]);

输出:

$name1 : 5a4b2e0ce944dfilename.ext
$name2 : 5a4b2e0ce948cfilename.ext
$target_file = 5a4b2e0ce944dfilename.ext
$target_filez = 5a4b2e0ce948cfilename.ext