【问题标题】:PHP upload a file to selected subdirectoryPHP将文件上传到选定的子目录
【发布时间】:2017-10-25 12:33:14
【问题描述】:

我有一个表单可以上传到它所在的目录,但我需要它从一个选择上传到子目录或子目录的子目录。但不确定如何使用选择信息。

我的文件夹结构是4层文件夹

Produce
Produce/Meat
Produce/Meat/Beef
Produce/Meat/Beef/Portions
Produce/Meat/Beef/Packaged
Produce/Vegtables
Produce/Vegetables/Fresh
Produce/Vegetables/Fresh/Local etc,.

这是我的表格

<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data">
<br>
        <label for="fileSelect"><strong>There is a 10MB limit Filesize limit.</strong> Allowed file types are pictures, MSWord, MSExcel, PDF, and plain text. Navigate and choose:</label>
        <input type="file" name="file" id="fileSelect"><br><br>
        Upload to:
        <select name="folder"><option value="this" selected>This folder</option><option value="BBB">Meat</option><option value="CCC">Meat/Beef</option><option value="DDD">Meat/Beef/Portions</option><option value="EEE">Meat/Beef/Packaged</option><option value="FFF">Vegetables</option><option value="GGG">Vegetables/Fresh</option><option value="HHH">Vegetables/Fresh/Local</option><option value="III">Vegetables/Fresh/Packaged</option></select>
        <input class="button" type="submit" name="action" value="Upload to Shared Folder"><br><br>
    </form>

那么upload_file.php就有这个PHP代码

<?php

if(isset($_FILES["file"]["error"])){
    if($_FILES["file"]["error"] > 0){
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else{
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "pdf" => "application/pdf", "txt" => "text/plain");
        $filename = $_FILES["file"]["name"];
        $filetype = $_FILES["file"]["type"];
        $filesize = $_FILES["file"]["size"];

        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");

        // Verify file size - 10MB maximum
        $maxsize = 200000 * 60;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");

        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("general/" . $_FILES["file"]["name"])){
                echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
            } else{
                move_uploaded_file($_FILES["file"]["tmp_name"], "" . $_FILES["file"]["name"]);
                echo "The file was uploaded successfully.</br></br>";
            } 
        } 
        else{
            echo "Error: There was a problem uploading the file - please try again."; 
        }
    }
} else{
    echo "Error: Invalid parameters - something is very wrong with this upload.";
}
?>

有没有办法可以获取选择值并上传到选定的子目录?

【问题讨论】:

  • 使用 isset 和 if equal to 值,并将文件移动到选择的选择,或者更好的是开关/case php.net/manual/en/control-structures.switch.php
  • @Fred-ii- 不确定该怎么做,是选择值吗?我可以使选择值成为实际的文件夹名称。文件夹名称永远不会改变。
  • 我在下面贴了一些东西给你看看;评论会太长。

标签: php upload directory subdirectory


【解决方案1】:

有很多方法可以做到这一点,所以这里有一种方法:

检查是否设置了 POST 数组(用于下拉选择),然后在内部检查它是否等于选项中的值,然后将文件移动到所选文件夹中:

if(isset($_POST['folder'])){

    if($_POST['folder'] == 'this'){

    // move_uploaded_file to said folder

    }

    if($_POST['folder'] == 'BBB'){

    // move_uploaded_file to said folder

    }

    // ... do the same for the others below


}

您还可以设置默认上传文件夹。


编辑:

注意:在// My snippet START --- // My snippet END 中查找我原来的 sn-p。

<?php

if(isset($_FILES["file"]["error"])){
    if($_FILES["file"]["error"] > 0){
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else{
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "pdf" => "application/pdf", "txt" => "text/plain");
        $filename = $_FILES["file"]["name"];
        $filetype = $_FILES["file"]["type"];
        $filesize = $_FILES["file"]["size"];

        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");

        // Verify file size - 10MB maximum
        $maxsize = 200000 * 60;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");

        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("general/" . $_FILES["file"]["name"])){
                echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
            } else{

// My snippet START 
    if(isset($_POST['folder'])){

        if($_POST['folder'] == 'this'){

        // move_uploaded_file to said folder

        $uploaddir = '/var/www/uploads/Produce/';
        $uploadfile = $uploaddir . basename($_FILES['file']['name']);

        move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
                    echo "The file was uploaded successfully.</br></br>";

        }

        if($_POST['folder'] == 'BBB'){

        // move_uploaded_file to said folder

        $uploaddir = '/var/www/uploads/Produce/Meat/';
        $uploadfile = $uploaddir . basename($_FILES['file']['name']);

        move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
                    echo "The file was uploaded successfully.</br></br>";

        }

        // ... do the same for the others below


    }

// My snippet END

            }
        }
        else{
            echo "Error: There was a problem uploading the file - please try again."; 
        }
    }
} else{
    echo "Error: Invalid parameters - something is very wrong with this upload.";
}
?>

【讨论】:

  • 我可以做到这一点,但不知道把它放在我上面的代码中的什么地方,或者在你有“//将上传文件移动到所述文件夹”的地方使用什么 PHP 对我来说是新的。
  • @Kilisi 您可以将整个 sn-p 放在您拥有move_uploaded_file 的else{ move_uploaded_file....} 中,您基本上会复制该方法并相应地更改上传文件夹。
  • @Kilisi 重新加载我的答案并在 Edit: 下查看。您可以继续使用其他文件夹并按照我所做的方法进行操作。我使用了/var/www/uploads/Produce/,但您需要更改它们以匹配您服务器的系统路径。
  • 看起来合乎逻辑,我会尝试一下,无论如何应该足以让我行动起来,干杯。
  • @Kilisi 这只是一个例子。您将需要使用您的 cpanel 向您显示的系统路径,就像您在 cmets 中发布的那样。确保它以斜杠结尾,如我的回答所示。
猜你喜欢
  • 2015-12-06
  • 2014-11-10
  • 1970-01-01
  • 2019-10-17
  • 2020-02-21
  • 2018-01-01
  • 2014-11-16
  • 1970-01-01
  • 2021-05-25
相关资源
最近更新 更多