如何在上传到服务器之前重命名每个文件到时间戳

use*_*875 5 php random upload timestamp file

我正在使用以下代码上传一些文件,但是,有些文件被取代,因为这些名称相似.我只是想知道如何更改名称,我试过但我发现自己弄乱了整个代码.

PHP

if (!empty($_FILES["ambum_art"])) {
    $myFile = $_FILES["ambum_art"];
    if ($myFile["error"] !== UPLOAD_ERR_OK) {
       echo "<p>An error occurred.</p>";
        exit;
    }
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }
     $success = move_uploaded_file($myFile["tmp_name"],UPLOAD_DIR . '/'.$name);
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    } else {
        $Dir = UPLOAD_DIR .'/'. $_FILES["ambum_art"]["name"];
    }
    chmod(UPLOAD_DIR .'/'. $name, 0644);
    unset($_SESSION['video']);
    $_SESSION['album_art'] = $Dir;
    $ambum_art_result = array();
    $ambum_art_result['content'] = $Dir;
    echo json_encode($ambum_art_result);

}
Run Code Online (Sandbox Code Playgroud)

我希望每个文件都能从这个变量中获得一些东西.

$rand_name = microtime().microtime().time().microtime();
Run Code Online (Sandbox Code Playgroud)

谢谢.

我不想首先检查文件是否存在,因为它添加到进程,我只想使用time()和一些随机字符串.得到一个独特的名字.

Pau*_*ell 4

请参阅此网站以获取文件上传的不错示例和说明。此外,此站点似乎是您找到此特定脚本的地方,或者如果没有提供很好的解释。

我已经修改了您的代码以包含一些注释,以便您和其他人可以更好地理解发生了什么。理论上,代码不应该像您所说的那样覆盖现有文件,但我添加了您需要更改的内容以将文件名设置为随机字符串。

此外,您还将原始文件名存储到会话中,而不是修改后的文件名。

define("UPLOAD_DIR", "/path/to/uploads/");

if (!empty($_FILES["ambum_art"])) 
{
    // The file uploaded
    $myFile = $_FILES["ambum_art"];

    // Check there was no errors
    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // Rename the file so it only contains A-Z, 0-9 . _ -
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    // Split the name into useful parts
    $parts = pathinfo($name);

    // This part of the code should continue to loop until a filename has been found that does not already exist.
    $i = 0;
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }

    // If you want to set a random unique name for the file then uncomment the following line and remove the above
    // $name = uniqid() . $parts["extension"];

    // Now its time to save the uploaded file in your upload directory with the new name
    $success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . '/'.$name);

    // If saving failed then quit execution
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }

    // Set file path to the $Dir variable
    $Dir = UPLOAD_DIR .'/'. $name;

    // Set the permissions on the newly uploaded file
    chmod($Dir, 0644);

    // Your application specific session stuff
    unset($_SESSION['video']);
    $_SESSION['album_art'] = $Dir; // Save the file path to the session
    $ambum_art_result = array();
    $ambum_art_result['content'] = $Dir;
    echo json_encode($ambum_art_result);

}
Run Code Online (Sandbox Code Playgroud)

阅读有关 PHP uniqid 的更多信息。

我还强烈建议进行一些文件类型检查,因为目前看来任何文件都可以上传。上面的第二个链接有一个标题为“安全注意事项”的部分,我建议仔细阅读该部分。