在php中上传时重命名文件
Posted
技术标签:
【中文标题】在php中上传时重命名文件【英文标题】:Rename file when upload in php 【发布时间】:2013-09-21 02:32:01 【问题描述】:我现在正在制作简单的文件上传脚本。一切正常,但是当我上传另一个相同的文件时 - 脚本只会覆盖它。所以,我想要的是能够将每个上传的文件重命名为随机名称。 (例如 12jfisfhassa .extension)。
这是我目前所拥有的:
<html>
<head>
<title>File Uploader v1.0</title>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<center>
<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<td>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<b>Please choose a file:</b><br/>
<input type="file" name="fileup"/><br/>
<br/>
<input type="submit" name='submit' value="Upload"/>
</tr>
</form>
</table>
</body>
</html>
<?php
$uploadpath = 'upload/'; // directory to store the uploaded files
$max_size = 103000000; // maximum file size, in KiloBytes
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png', 'rar', 'zip', 'exe'); // allowed extensions
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1)
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']); // gets the file name
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext); // gets extension
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']); // gets image width and height
$err = ''; // to store the errors
// Checks if the file has allowed type, size, width and height (for images)
if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
// If no errors, upload the image, else, output the errors
if($err == '')
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath))
echo '<font color="green"><b>Success!</b></font>';
echo '<br/>File: <b>'. basename( $_FILES['fileup']['name']). '</b>';
echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
echo '<br/><br/>File path: <input type="text" value="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'" readonly>';
else echo '<b>Unable to upload the file.</b>';
else echo $err;
?>
</center>
是的,我不使用 mysql。 请帮忙? :)
【问题讨论】:
所以更改$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
以生成一个唯一的名称,而不是盲目地使用上传者提供的任何内容...您的验证代码也相当可怕,您只是假设上传成功。跨度>
这个答案可能对你有帮助***.com/questions/11155058/…
@JasonOOO 我想我迷路了:/你能帮帮我吗?
【参考方案1】:
添加这段代码并测试它:
while(file_exists($uploadpath))
//get filename without suffix
$rootname = basename($_FILES['fileup']['name'], $type);
//add $i to the file name between rootname and extension (suffix)
$uploadpath = "upload/".$rootname."-$i.".$type;
$i++;
像这样添加它:
if($err == '')
$i = 1;
while(file_exists($uploadpath))
//get filename without suffix
$rootname = basename($_FILES['fileup']['name'], $type);
$uploadpath = "upload/".$rootname."-$i.".$type;
$i++;
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath))
echo '<font color="green"><b>Success!</b></font>';
echo '<br/>File: <b>'. basename( $_FILES['fileup']['name']). '</b>';
echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
echo '<br/><br/>File path: <input type="text" value="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'" readonly>';
else echo '<b>Unable to upload the file.</b>';
【讨论】:
没用。它也会更改文件扩展名。我怎么能看到,它使文件的原始名称加倍。 @JasonOOO @berdyev 我漏掉了一个点,你能再测试一下吗? 它有效。但它使文件名加倍。例如:当我第一次上传时:9gag.png 结果:/upload/9gag.png 当我第二次尝试时。结果:/upload/9gag.png9gag.-1.png 而且看起来不太好。我希望它是随机的? 现在很好用。不是我想要的,但我觉得还可以。非常感谢!顺便说一句,您知道在上传文件时如何应用 Ajax 百分比吗? @JasonOOO【参考方案2】: if(isset($_POST['submit']))
$newname=date('Ymdhis');
$file1=$_FILES['file1']['name']=$newname.".jpg";
$tmp_name=$_FILES['file1']['tmp_name'];
$destination=__DIR__."\images\\";
move_uploaded_file($tmp_name, $destination.$file1);
echo "<pre>";
print_r($_FILES);
//this is example is only for jpg you can check extension and can rename the same
【讨论】:
【参考方案3】:您需要计算一个不时变化的$uploadpath
。例如,您可以将会话令牌与当前时间(以毫秒为单位)结合起来。
【讨论】:
以上是关于在php中上传时重命名文件的主要内容,如果未能解决你的问题,请参考以下文章
wordpress上传图片时重命名--修改插件时遇到的一些问题