gpt4 book ai didi

php - 重命名上传的文件以避免覆盖

转载 作者:行者123 更新时间:2023-12-04 06:33:42 32 4
gpt4 key购买 nike

我有这个简单的脚本,它工作正常,但目前会覆盖具有重复名称的文件。我怎样才能避免这种情况?

<?php
// Configuration - Your Options
$allowed_filetypes = array('.mp3'); // These will be the types of file that will pass the validation.
$max_filesize = 1048576; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './uploads/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

if(!in_array($ext,$allowed_filetypes))

header('Location: http://www.website.com/five/error');

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)

header('Location: http://www.website.com/five/error');

if(!is_writable($upload_path))

header('Location: http://www.website.com/five/error');

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))

// echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
header('Location: http://www.website.com/five/sent');

else

header('Location: http://www.website.com/five/error');

?>

最佳答案

添加 if (file_exists($upload_path.$filename))上传前,设置 $filename如果有的话。

关于php - 重命名上传的文件以避免覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086265/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com