为用户创建新目录

时间:2016-07-31 20:25:04

标签: php mysql html5

我正在尝试从db为用户创建一个新的目录,这样他们就可以将文件上传到他们自己的目录中,并使我可以轻松管理他们的文件。如果有人能指出我正确的方向,我将不胜感激。欢呼声。

<html>
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Upload</title>
    <link href="./style.css" rel="stylesheet" type="text/css">
    <script>
     function myFunction(file)
     {
      if(file.files.length>10)
      {
        alert("Maximum 10 files");
        document.getElementById("file").value='';
      }
     }
    </script>
</head>
<body>

<form action="" method="post" enctype="multipart/form-data" class='frms upload' id="upload" name="form1">
    <fieldset>
    <legend>Upload files</legend>
        <input type="file" id="file" name="files[]" multiple="multiple" required='required' onchange="myFunction(this)"/>

        <input type="submit" value="Upload" />



    <p align=left><b>Note : Allowed extensions are JPG, JPEG, GIF, PNG, BMP and ZIP.</b></p>
<?php
error_reporting(0);
$valid_formats = array("jpg", "png", "gif", "zip", "bmp","jpeg");
$max_file_size = 1024*500; 
$path = "uploads/"; 
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; 
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                echo $message[] = "<p style='color:red'>$name is too large!<br><span style='color:blue:'><a href='http://compressjpeg.com/'>Please compress file to below 500kb </a></span></p>";
                continue; 
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                echo $message[] = "<p style='color:red'>$name is not a valid format!<br>Please choose a valid file format</p>";
                continue; 
            }
            else{ 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $path1="uploads/".$_FILES["files"]['name'][$f];
                  copy( $_FILES["files"]['tmp_name'][$f], $path1.$name );


                    echo "<table><tr><td> " . $_FILES["files"]['name'][$f] . "</td><td>Uploaded Successfully!</td></tr>";
                    echo "<tr><td>Type:</td><td>" . $_FILES["files"]["type"][$f] . "</td></tr>";
                    echo "<tr><td>Size:</td><td>" . ($_FILES["files"]["size"][$f] / 1024) . " Kb</td></tr>";
                    echo "<tr><td>Stored in:</td><td>" .$path.$_FILES["files"]["name"][$f]."</td></tr></table><br>";
                $count++; 
            }
        }
    }
}
?> 

    </fieldset>

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是php文件,它将上传文件夹/ uploads / username中的所有文件。您可以使用此...并且为了在表格中显示文件,您可以使用ajax发布请求进行前端处理。     

$username = $_GET['username'];

$target_dir = "../uploads/" . $username . "/";
if (!file_exists($target_dir)) {
    mkdir($target_dir, 0777, true);
}
$files1 = scandir($target_dir);
$files2 = scandir($target_dir, 1);

print_r($files1);
print_r($files2);

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo $target_dir . "</br>";
echo $target_file . "</br>";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $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;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        header("Location: ../../pages/edit-user.php?p=" . $plate_num);
        // echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

?>