D
D
Daniil Sukhikh2018-04-11 06:10:40
PHP
Daniil Sukhikh, 2018-04-11 06:10:40

How to change the file name when uploading to the site?

There is a form for uploading files to the site, but when you upload files with Russian names, the names change to scribbles. I want the file name to be changed to a new random one and so that it does not repeat itself, the file name is sent to the database after loading.
Download form:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="css.css">
  <title>PHP</title>
  <meta charset="utf-8">
</head>
<body>

  <?php
if(isset($_FILES) && $_FILES['inputfile']['error'] == 0){ // Проверяем, загрузил ли пользователь файл
$destiation_dir = dirname(__FILE__) .'/'.$_FILES['inputfile']['name']; // Директория для размещения файла
$bdf = move_uploaded_file($_FILES['inputfile']['tmp_name'], $destiation_dir ); // Перемещаем файл в желаемую директорию
if ($bdf == true) {
  $connection = mysqli_connect('localhost', 'root', '', 'gebd');
  $name = $_FILES['inputfile']['name'];
  mysqli_query($connection, "INSERT INTO `images` ('id') VALUES ('$name')");
}
}
  ?>
<form method="post" action="index.php" enctype="multipart/form-data">
<label for="inputfile">Upload File</label>
<input type="file" id="inputfile" name="inputfile"></br>
<input type="submit" value="Click To Upload">
</form>

</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ildar Saribzhanov, 2018-04-11
@Bluz

The solution to your problem is move_uploaded_file.
Specify the desired name as the second parameter (full path, with the name). I think you will figure out how to generate a random string /

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question