du7133 2019-02-14 21:57
浏览 25

如何制作需要权利的上传表格

So I have a Php function in a form for file upload and I want to force people, that want to upload files, to log in. So if someone clicks on upload, an like in-window-pop-up comes up and he is forced to log himself in, or the upload will be canceled.

How do I do that?

index.html

<!DOCTYPE HTML>
<html>
<head>
  <title> File-Upload </title>
  <link style="text/css" rel="stylesheet" href="design.css"/>
</head>
<body>
<div class="form">
  <form action="upload.php" method="post" enctype="multipart/form-data">
    <input class="select" type="file" name="datei"><br>
    <input class="upload" type="submit" value="Hochladen">
  </form>
</div>
</body>
</html>

upload.php

<?php
$upload_folder = 'uploads/';
$filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);
$extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));


$allowed_extensions = array('png', 'jpg', 'jpeg', 'gif', 'zip', 'rar', 'txt');
if(!in_array($extension, $allowed_extensions)) {
 die("Ungültige Dateiendung. Nur png, jpg, jpeg, gif, zip, rar und txt-Dateien sind erlaubt");
}

$max_size = 1000*1024; //500 KB
if($_FILES['datei']['size'] > $max_size) {
 die("Bitte keine Dateien größer 1000kb hochladen");
}

$new_path = $upload_folder.$filename.'.'.$extension;

if(file_exists($new_path)) {
 $id = 1;
 do {
 $new_path = $upload_folder.$filename.'_'.$id.'.'.$extension;
 $id++;
 } while(file_exists($new_path));
}

move_uploaded_file($_FILES['datei']['tmp_name'], $new_path);
echo 'Bild erfolgreich hochgeladen: <a href="'.$new_path.'">'.$new_path.'</a>';
?>

I'm sorry for the German, but I think you can imagine what that would be in English.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入