Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP使用一个表单和两个不同的输入将两种不同的文件类型上载到两个不同的目录中_Php - Fatal编程技术网

PHP使用一个表单和两个不同的输入将两种不同的文件类型上载到两个不同的目录中

PHP使用一个表单和两个不同的输入将两种不同的文件类型上载到两个不同的目录中,php,Php,我的目标是使用两个不同的将两个不同文件类型的不同文件上传到同一表单中,然后将它们放在两个单独的目录中 我目前有$\u文件[“photo”]在$\u文件[“profileLink”]未就位的情况下工作 为$\u文件[“profileLink”]添加功能后,它停止工作,包括数据库的插入,但报告说它已成功添加了详细信息 我这样做的方式可能是错误的,而且比它应该的更复杂,所以我愿意改进 注意事项: *最大上传文件数设置为4 *upload_max_filesize设置为4G(甚至不要接近该大小) 下面是

我的目标是使用两个不同的
将两个不同文件类型的不同文件上传到同一表单中,然后将它们放在两个单独的目录中

我目前有
$\u文件[“photo”]
$\u文件[“profileLink”]
未就位的情况下工作

$\u文件[“profileLink”]
添加功能后,它停止工作,包括数据库的插入,但报告说它已成功添加了详细信息

我这样做的方式可能是错误的,而且比它应该的更复杂,所以我愿意改进

注意事项: *最大上传文件数设置为4 *upload_max_filesize设置为4G(甚至不要接近该大小)

下面是PHP代码,但请注意与
$\u文件[“profileLink”]
相关的任何内容都会破坏这一点,我故意没有检查该文件,因为我想测试它是否有效

<?php

if (isset($_POST['convoy_add']) && $_POST['convoy_add'] == "Add") {
    if ($ConvoyPerms['new-convoy'] == '1' || $staffPerms['dev'] == '1') {
        $cname = $_POST['cname'];
        $server = $_POST['server'];
        $startdate = $_POST['startdate'];
        $starttime = $_POST['starttime'];
        $stpoint = $_POST['startpoint'];
        $startcomp = $_POST['startcomp'];
        $endpoint = $_POST['endpoint'];
        $endcomp = $_POST['endcomp'];
        $profile = $_POST['profile'];

        #image handling
        // Check if file was uploaded without errors
        if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0 && isset($_FILES["profileFile"]) && $_FILES["profileFile"]["error"] == 0){

            //Image
            $imgAllowed = array("jpg" => "video/mp4", "image/jpg", "jpeg" => "image/jpeg", "png" => "image/png");
            $filename = $_FILES["photo"]["name"];
            $filename2 = $_FILES["profileFile"]["name"];
            $filetype = $_FILES["photo"]["type"];
            $filesize = $_FILES["photo"]["size"];

            // Verify file extension (Image)
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            if(!array_key_exists($ext, $imgAllowed)) die("Error: Please select a valid file format.");

            // Verify file size - 2GB maximum
            $maxsize = 2000000 * 1024 * 1024;
            if($filesize > $maxsize) die("Error: File size is larger than the Allowed limit.");

            $files = array();

            // Verify MYME type of the file
                if (in_array($filetype, $imgAllowed)) {
                    // Check whether file exists before uploading it
                    if (file_exists("upload/" . $_FILES["photo"]["name"])) {
                        echo $_FILES["photo"]["name"] . " already exists.";
                    } else {
                        try {
                            $host = $_SERVER['HTTP_HOST'];

                            $id = uniqid();
                            $ext = pathinfo($filename, PATHINFO_EXTENSION);
                            $ext2 = pathinfo($filename2, PATHINFO_EXTENSION);
                            move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $id . "." . $ext);
                            move_uploaded_file($_FILES["profileFile"]["tmp_name"], "upload/" . "test" . "." . $ext2);
                            $url = "https://" . $host . "/hub/convoycontrol/upload/$id.";

                            $query = $db->prepare("INSERT INTO convoys (eventname, server, startcity, startcompany, endcity, endcompany, startdate, starttime, image, profilelink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                            $query->execute(array($cname, $server, $stpoint, $startcomp, $endpoint, $endcomp, $startdate, $starttime, $url . $ext, $profile));


                            echo '<div class="alert alert-success" role="alert"><a href="#" class="alert-link">Convoy details successfully added!</a></div>';
                        }
                        catch (PDOException $e)
                        {
                            echo '<div class="alert alert-danger" role="alert"><a href="#" class="alert-link">Convoy details failed to added!</a></div>';
                        }
                    }
                } else {
                    echo "Error: There was a problem uploading your file. Please try again.";
                }
        } else{
            echo "Image Error: " . $_FILES["photo"]["error"];
        }
    }
}
?>

下面是带有输入字段的HTML

<?php

$json = file_get_contents('https://api.truckersmp.com/v2/servers');
$data = json_decode($json);

echo '<form action=new_convoy method=post enctype=multipart/form-data>';
  echo '<tr>';
  echo '<td>'."<input class='form-control' type=text autocomplete='off' name=cname value=''</td>";
  echo '<td>'."<select name=server>";
  foreach($data->response as $name) {
        echo"<option value='$name->shortname'>$name->shortname</option>";
  }
  echo'</select>';
  echo '<td>'."<input class='inputdate' type=date autocomplete='off' name=startdate value=''</td>";
    echo '<td>'."<input class='inputtime' type=text autocomplete='off' id=time placeholder=Time name=starttime value=''</td>";
  echo '<td>'."<input class='form-control' type=text autocomplete='off' name=startpoint value=''</td>";
echo '<td>'."<input class='form-control' type=text autocomplete='off' name=startcomp value=''</td>";
  echo '<td>'."<input class='form-control' type=text autocomplete='off' name=endpoint value=''</td>";
echo '<td>'."<input class='form-control' type=text autocomplete='off' name=endcomp value=''</td>";
echo '<td>'."<input class='form-control' type='file' name='profileFile'</td>";
  echo '<td>'."<input class='form-control' type='file' name='photo'</td>";

  echo '<td>'."<input class='btn btn-primary btn-outline' type=submit name='convoy_add' value=Add".' </td>';

  echo '</tr>';
  echo '</form>';

echo '</table>
</div>';
?>

经过更多的思考和测试,我终于让脚本按要求工作了

我在相关的地方添加了评论,以帮助其他人了解正在发生的事情,因此他们希望能够利用这些来为他们的工作做出贡献

<?php
if (isset($_POST['convoy_add']) && $_POST['convoy_add'] == "Add") {
    //Checks against the session `$_SESSION['con_perms'];` and `$_SESSION['perms'];` for the users permissions
    if ($ConvoyPerms['new-convoy'] == '1' || $staffPerms['dev'] == '1') {
        $cname = $_POST['cname'];
        $server = $_POST['server'];
        $startdate = $_POST['startdate'];
        $starttime = $_POST['starttime'];
        $stpoint = $_POST['startpoint'];
        $startcomp = $_POST['startcomp'];
        $endpoint = $_POST['endpoint'];
        $endcomp = $_POST['endcomp'];

        //Image and Rar file handling
        // Check if files was uploaded without errors
        if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0 && isset($_FILES["profileLink"]) && $_FILES["profileLink"]["error"] == 0){

            //Allowed file types
            $filesAllowed = array("jpg" => "video/mp4", "image/jpg", "jpeg" => "image/jpeg", "png" => "image/png", "rar" => "application/octet-stream");

            //Properties of the image file being uploaded
            $filename = $_FILES["photo"]["name"];
            $filetype = $_FILES["photo"]["type"];
            $filesize = $_FILES["photo"]["size"];

            //Properties of the Rar file being uploaded, ("rar" => "application/octet-stream")
            $filename2 = $_FILES["profileLink"]["name"];
            $filetype2 = $_FILES["profileLink"]["type"];
            $filesize2 = $_FILES["profileLink"]["size"];

            // Verify file extension (Image)
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            $ext2 = pathinfo($filename2, PATHINFO_EXTENSION);
            if(!array_key_exists($ext, $filesAllowed)) die("Error: Please select a valid image format.");
            if(!array_key_exists($ext2, $filesAllowed)) die("Error: Please select a valid rar format.");

            // Verify file size - 2GB maximum
            $maxsize = 2000000 * 1024 * 1024;
            if($filesize > $maxsize) die("Error: Image size is larger than the Allowed limit.");
            if($filesize2 > $maxsize) die("Error: Rar size is larger than the Allowed limit.");

            // Verify MYME type of the files
            if (in_array($filetype, $filesAllowed) && in_array($filetype2, $filesAllowed)) {
                // Check whether file exists before uploading it
                if (file_exists("upload/" . $_FILES["photo"]["name"])) {
                    echo $_FILES["photo"]["name"] . " already exists.";
                } else {
                    try {
                        $host = $_SERVER['HTTP_HOST'];

                        //Used for unique ID of the image being stored and inserted into the database
                        $id = uniqid();

                        $ext = pathinfo($filename, PATHINFO_EXTENSION);
                        move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $id . "." . $ext);
                        $url = "https://" . $host . "/hub/convoycontrol/upload/$id.";

                        //Using to allow for unique timestamp of folder without duplicating IDs
                        $timezone = date("d-m-Y").date("h:i:s");

                        //Check if directory exists
                        if (!file_exists("upload/profiles/$timezone/")) {
                            mkdir("upload/profiles/$timezone/", 0777, true);
                        }

                        $ext2 = pathinfo($filename2, PATHINFO_EXTENSION);
                        move_uploaded_file($_FILES["profileLink"]["tmp_name"], "upload/profiles/$timezone/$filename2.".$ext2);
                        $url2 = "https://" . $host . "/hub/convoycontrol/upload/profiles/$timezone/$filename2.";

                        $query = $db->prepare("INSERT INTO convoys (eventname, server, startcity, startcompany, endcity, endcompany, startdate, starttime, image, profilelink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                        $query->execute(array($cname, $server, $stpoint, $startcomp, $endpoint, $endcomp, $startdate, $starttime, $url . $ext, $url2 . $ext2));


                        echo '<div class="alert alert-success" role="alert"><a href="#" class="alert-link">Convoy details successfully added!</a></div>';
                    }
                    catch (PDOException $e)
                    {
                        echo '<div class="alert alert-danger" role="alert"><a href="#" class="alert-link">Convoy details failed to added!</a></div>';
                    }
                }
            } else {
                echo "Error: There was a problem uploading your file. Please try again.";
            }
        } else{
            //Displays errors upon failure to upload
            echo "Image Error: " . $_FILES["photo"]["error"];
            echo "Image Error: " . $_FILES["profileLink"]["error"];
        }
    }
}
?>


当您发布表单时,只需处理控制器中的文件,并发送/执行您需要对其执行的任何操作。我目前没有在本项目中使用Laravel,如果您能再解释一下,因为您提到“控制器”时我不明白。这与Laravel无关,当你发布你的表格时,它会进入一个文件,对吗?我把那个文件称为控制器我以为你指的是一个框架,是的,它会自动发布,但我不确定如何处理控制器内的文件,因为这是唯一需要/处理这些文件的区域。您的html标记缺少结束标记,应该类似于
。您的代码太长,看起来也有点不完整(
$Coveryperms
?)所以最好的方法就是一行一行地打印并检查。