简体   繁体   English

上载具有唯一名称的图像

[英]Upload image with unique name

So I am trying to upload some images to a folder on my server with the script below,but it saves every image as "image.jpg" and it overwrites the last uploaded image if I try to upload multiple ones.As you can see I commented the part that checks if the file already exists to let me upload a file and overwrite the latest one.Help!I need something modified to just randomize the name or get the original image name. 因此,我尝试使用以下脚本将一些图像上传到服务器上的文件夹中,但是它将每个图像另存为“ image.jpg”,并且如果我尝试上传多个图像,它会覆盖最后上传的图像。如您所见,评论了检查文件是否已存在的部分,以便我上传文件并覆盖最新的文件。帮助!我需要修改某些内容,以随机化名称或获取原始图像名称。

<?php
$target_dir = "wp/";
$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["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
/*if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "png" && $imageFileType != "jpg") {
    echo "Sorry, only PNG and JPG files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

A simple way to get a unique filename is to get the current Unix time in milliseconds and append (or prepend) that to the filename. 获取唯一文件名的一种简单方法是获取当前Unix时间(以毫秒为单位)并将其附加(或前置)到文件名中。 The command to use is microtime() . 使用的命令是microtime()

For example: 例如:

$target_file = $target_dir . microtime() . basename($_FILES["fileToUpload"]["name"]);

You could also try things like hashing the file to get a unique hash with low probability of collisions, but this is faster and just as effective. 您也可以尝试像对文件进行哈希处理那样获得具有低冲突概率的唯一哈希,但这更快且同样有效。

You can implement something like Windows's auto-file-renaming: 您可以实现类似于Windows的自动文件重命名功能:

$try = 1;
while($file_exists($target_file)) {
    $target_file = preg_replace('/(\(\\d+\))*(\.[^\\(\\)]+)$/',
        "({$try})\\2", $target_file);
    $try++;
}

This will replace the file "duplicate.jpg" with "duplicate(1).jpg", then "duplicate(2).jpg", and so on. 这会将文件“ duplicate.jpg”替换为“ duplicate(1).jpg”,然后替换为“ duplicate(2).jpg”,依此类推。

It will still be prone to unlikely race conditions, just as using uniqid() or microtime() (which, conflict-wise, are both better). 就像使用uniqid()microtime() (冲突的方式都更好),它仍然容易出现不太可能的竞争情况。

Otherwise, always use tempnam() . 否则,请始终使用tempnam() You can check out this answer . 您可以查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM