PHP. Урок 12. Скрипт для загрузки файлов

Автор: | 12.09.2018
<?
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

if(isset($_FILES['file']['name']))
  {
  do{
    $path_parts =pathinfo($_FILES['file']['name']);
    $filename=generateRandomString();	
    $filename.=".".strtolower($path_parts['extension']);
  }while(file_exists("uploads/{$filename[0]}/{$filename[1]}/{$filename[2]}/".$filename));
  
  mkdir("uploads/{$filename[0]}/{$filename[1]}/{$filename[2]}/",0755,true);
  move_uploaded_file($_FILES['file']['tmp_name'],"uploads/{$filename[0]}/{$filename[1]}/{$filename[2]}/".$filename);
  ?>
  <a href=<?="uploads/{$filename[0]}/{$filename[1]}/{$filename[2]}/".$filename?> download="<?=$_FILES['file']['name']?>">Скачать файл</a>
  <?
  }

?>
<form method='post' enctype='multipart/form-data'> 
<input type='file' name='file'>
<input type='submit'>
</form>

 

Раздел: PHP