Работа с файлами
15.03.2025
загрузка и выгрузка файлов на сайте

Форма для запроса файла

    <form action="" method="post" enctype="multipart/form-data">
        Выберите файл для загрузки на сайт:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <button class="button1 small" type="submit" value="upload_db" name="action">{PHP.L.fz_Button_Upload}</button>
    </form>

устанавливаем путь и проверить расширение файла ,  если нужно

$upload_dir = $cfg['plugins_dir'] . '/имя_плагина/upload/';
$target_file = $upload_dir . basename($_FILES["fileToUpload"]["name"]);
// проверка расширения у файла
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }

 

Выгрузка файла с сайта

$file = $upload_dir .'HelloWorld.xlsx';

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="'.basename($file).'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  readfile($file);

// Удаление файла на сервере после выгрузки
  if(is_file($file_name)){
      unlink($file_name);
  }
  exit;
}

работа с файлами