在php中上传时重命名文件

木瓜

我正在制作简单的文件上传脚本。一切正常,但是当我上传相同的另一个文件时,脚本会覆盖它。因此,我想要的是能够将每个上传的文件重命名为随机名称。(例如12jfisfhassa .extension)。

这是我到目前为止的内容:

<html>
<head>
<title>File Uploader v1.0</title>
<link rel='stylesheet' href='style.css'>
</head>
<body>
<center>
<table width="300" 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 width="100%" 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。请帮忙?:)

杰森OOO

添加此代码并对其进行测试:

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>';
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

重命名文件上传 PHP

来自分类Dev

如何在Wordpress中上传时重命名图像

来自分类Dev

多个图像重命名并在PHP中上传

来自分类Dev

多个图像重命名并在PHP中上传

来自分类Dev

在php中上传和重命名图片时出错

来自分类Dev

上传时如何重命名文件

来自分类Dev

Java文件上传在上传时重命名文件

来自分类Dev

Java文件上传在上传时重命名文件

来自分类Dev

重命名文件,然后在S3中上传文件

来自分类Dev

使用PHP重命名和上传文件时出错-未定义索引

来自分类Dev

如何在添加/删除动态字段中上传时重命名图像

来自分类Dev

在 Windows 命令行中上传到 FTP 之前重命名本地文件

来自分类Dev

重命名照片名称(如果已在PHP中上传[具有相同名称])

来自分类Dev

用PHP上传多个文件-重命名文件

来自分类Dev

Froala编辑器,上传时重命名文件

来自分类Dev

上传时如何在c#中重命名文件?

来自分类Dev

Codeigniter重命名上传文件

来自分类Dev

在php中上传文件时未定义的变量

来自分类Dev

无法在PHP中上传文件

来自分类Dev

在php中上传rtf文件

来自分类Dev

使用django在两个不同的文件夹中上传并重命名两个不同的文件?

来自分类Dev

Dropzonejs-上传之前重命名文件

来自分类Dev

Django文件上传和重命名

来自分类Dev

上载时重命名文件

来自分类Dev

上载时重命名文件

来自分类Dev

在Laravel中上传文件时出错

来自分类Dev

在yii Framework中上传文件时出错

来自分类Dev

“错误#2038”:在Flex中上传文件时

来自分类Dev

在yii Framework中上传文件时出错