Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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_Html_Css_Apache_Centos - Fatal编程技术网

无法使用PHP上载文件

无法使用PHP上载文件,php,html,css,apache,centos,Php,Html,Css,Apache,Centos,我无法使用php将文件上载到运行httpd的CentOS 7服务器 我已经搜索了所有地方,但仍然无法找到无法上载文件的原因:( 表格如下: <div class="container"> <div class="row"> <div class="col-lg-4 col-lg-offset-4"> <h2 class="section-heading text-white text-center">

我无法使用php将文件上载到运行httpd的CentOS 7服务器

我已经搜索了所有地方,但仍然无法找到无法上载文件的原因:(

表格如下:

<div class="container">
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4">
            <h2 class="section-heading text-white text-center">Private Upload Page</h2>
            <hr class="light">
            <form class="form-horizontal" role="form" action="upload.php" method="post" enctype="multipart/form-data">
              <div class="form-group">
              <span class="text-white">Select directory to upload to:</span><br>
              <input class="form-control" type="text" name="dir" value="/var/www/html/Private/uploads" style="width: 100%;"><br>
              <span class="text-white">Select file to upload:</span><br>
              <input class="form-control" type="file" name="fileToUpload" id="fileToUpload" style="width: 100%;"><br>
              <input class="form-control btn btn-primary" type="submit" value="Upload File" name="submit" >
            </form>
        </div>
    </div>
</div>
以及:

以及在php.ini文件中打开文件写入

以及关闭防火墙,我仍然无法上传文件


我不熟悉网页设计,所以我确信这是我造成的问题。我查看的每个页面都指向权限,但即使允许使用777也无法解决我的问题。

您的代码看起来不错。您是否在
php.ini
上启用了
file\u uploads=On
?如果未搜索
file\u uploads指令
,并将其设置为On。

您的代码看起来不错。您是否在
php.ini
上启用了
file\u uploads=On
?如果没有,请搜索
file\u uploads指令
,并将其设置为On。

您应该在输入的目录名前加上根目录(即指定目录存在/将创建的目录)


另外,请确保使用客户端或服务器端验证时有一个尾随斜杠,因为用户可能会忘记输入它。

您应该在输入的目录名前加上根目录(即指定目录存在/将创建的目录)

 <form class="" id="add_downloadble" method="post" enctype="multipart/form-data" name="upload" >
                    <div class="control-group">
                        <label class="control-label" for="inputEmail">File:</label>
                        <div class="form-group">


            <input name="uploaded_file"  class="form-control" id="fileInput" type="file" required>

                            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
                            <input type="hidden" name="id" value="<?php echo $session_id ?>"/>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="name" Placeholder="File Name"  class="form-control" required>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="desc" Placeholder="Description"  class="form-control" required>
                        </div>
                    </div>


                  <script>
                  jQuery(document).ready(function($){                        
                      e.preventDefault();
                      var _this = $(e.target);
                      var formData = new FormData($(this)[0]);
                      $.ajax({
                        type: "POST",
                        url: "save.php",
                        data: formData,
                        success: function(html){                              
                          window.location = 'tambah_materi.php';
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                      });
                    });
                  });
                  </script> 


              </div>
              <div class="col-sm-6">

            <div class="alert alert-info">Centang kelas yang ingin di tambahkan file ini.</div>

                        <div class="pull-left">
                    Check All <input type="checkbox"  name="selectAll" id="checkAll" />
                      <script>
                      $("#checkAll").click(function () {
                        $('input:checkbox').not(this).prop('checked', this.checked);
                      });
                      </script>         
                    </div>
                            <table cellpadding="0" cellspacing="0" border="0" class="table" id="">

                          <thead>
                                  <tr>
                              <th></th>
                              <th>Nama Kelas</th>
                              <th>Subject Code</th>
                              </tr>

                          </thead>
                          <tbody>

                                      <?php $query = mysql_query("select * from teacher_class
                          LEFT JOIN class ON class.class_id = teacher_class.class_id
                          LEFT JOIN subject ON subject.subject_id = teacher_class.subject_id
                          where teacher_id = '$session_id' and school_year = '$school_year' ")or die(mysql_error());
                          $count = mysql_num_rows($query);


                          while($row = mysql_fetch_array($query)){
                          $id = $row['teacher_class_id'];

                          ?>                             
                          <tr id="del<?php echo $id; ?>">
                            <td width="30">
                              <input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
                            </td>
                            <td><?php echo $row['class_name']; ?></td>
                            <td><?php echo $row['subject_code']; ?></td>                                                                   
                          </tr>

                  <?php } ?>



                </tbody>
              </table>


                            </div>
            <div class="col-sm-12">
            <hr>
              <center>
              <div class="control-group">
                    <div class="controls">
                      <button name="Upload" type="submit" value="Upload" class="btn btn-success" /><i class="icon-upload-alt"></i>&nbsp;Upload</button>
                    </div>
              </div>
              </center>

               </form>    
另外,请确保使用客户端或服务器端验证时有一个尾随斜杠,因为用户可能会忘记输入它。


 <form class="" id="add_downloadble" method="post" enctype="multipart/form-data" name="upload" >
                    <div class="control-group">
                        <label class="control-label" for="inputEmail">File:</label>
                        <div class="form-group">


            <input name="uploaded_file"  class="form-control" id="fileInput" type="file" required>

                            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
                            <input type="hidden" name="id" value="<?php echo $session_id ?>"/>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="name" Placeholder="File Name"  class="form-control" required>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="desc" Placeholder="Description"  class="form-control" required>
                        </div>
                    </div>


                  <script>
                  jQuery(document).ready(function($){                        
                      e.preventDefault();
                      var _this = $(e.target);
                      var formData = new FormData($(this)[0]);
                      $.ajax({
                        type: "POST",
                        url: "save.php",
                        data: formData,
                        success: function(html){                              
                          window.location = 'tambah_materi.php';
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                      });
                    });
                  });
                  </script> 


              </div>
              <div class="col-sm-6">

            <div class="alert alert-info">Centang kelas yang ingin di tambahkan file ini.</div>

                        <div class="pull-left">
                    Check All <input type="checkbox"  name="selectAll" id="checkAll" />
                      <script>
                      $("#checkAll").click(function () {
                        $('input:checkbox').not(this).prop('checked', this.checked);
                      });
                      </script>         
                    </div>
                            <table cellpadding="0" cellspacing="0" border="0" class="table" id="">

                          <thead>
                                  <tr>
                              <th></th>
                              <th>Nama Kelas</th>
                              <th>Subject Code</th>
                              </tr>

                          </thead>
                          <tbody>

                                      <?php $query = mysql_query("select * from teacher_class
                          LEFT JOIN class ON class.class_id = teacher_class.class_id
                          LEFT JOIN subject ON subject.subject_id = teacher_class.subject_id
                          where teacher_id = '$session_id' and school_year = '$school_year' ")or die(mysql_error());
                          $count = mysql_num_rows($query);


                          while($row = mysql_fetch_array($query)){
                          $id = $row['teacher_class_id'];

                          ?>                             
                          <tr id="del<?php echo $id; ?>">
                            <td width="30">
                              <input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
                            </td>
                            <td><?php echo $row['class_name']; ?></td>
                            <td><?php echo $row['subject_code']; ?></td>                                                                   
                          </tr>

                  <?php } ?>



                </tbody>
              </table>


                            </div>
            <div class="col-sm-12">
            <hr>
              <center>
              <div class="control-group">
                    <div class="controls">
                      <button name="Upload" type="submit" value="Upload" class="btn btn-success" /><i class="icon-upload-alt"></i>&nbsp;Upload</button>
                    </div>
              </div>
              </center>

               </form>    
文件:

文件:

它与默认目录一起工作吗?move_uploads_file是否会自动创建不存在的目录?我只尝试使用$target_dir=“uploads/”但这不起作用,我应该为默认目录设置什么?并且move_uploaded_文件不会创建不存在的目录。请将其保留为空,以便它在
中拾取默认目录,否则
-此外,您应该预先设置根目录(即将创建输入的目录名的目录)添加到输入的任何目录名。并确保有一个带有客户端或服务器端验证的尾随斜杠。它是否与默认目录一起工作?移动\u上传的\u文件是否自动创建不存在的目录?我已尝试仅使用$target\u dir=“uploads/”但这不起作用,我应该为默认目录设置什么?并且move_uploaded_文件不会创建不存在的目录。请将其保留为空,以便它在
中拾取默认目录,否则
-此外,您应该预先设置根目录(即将创建输入的目录名的目录)您不需要将文件直接上载到
/var/www/html/Private/uploads/
在当前路径上创建临时文件夹并上载到该文件夹,然后将上载的文件移动到本地或云存储上的目标。您不需要ed将文件直接上载到
/var/www/html/Private/uploads/
在当前路径上创建一个临时文件夹并上载到该文件夹,然后将上载的文件移动到本地或云存储上的目标。我将添加最有可能的服务器端验证,但在大多数情况下,这是我的主域的内部测试Web服务器(这一个通过rsync下拉镜像)。因此验证主要用于学习目的。如果我的帖子符合好帖子的标准,请对其进行升级投票,以便我可以构建rep。我将添加最有可能的服务器端验证,但在大多数情况下,这是我主域的内部测试Web服务器(这篇文章通过rsync向下拉了一面镜子)。因此,验证主要是为了学习。如果我的文章符合好文章的标准,请对其进行升级投票,以便我能够构建代表。
chmod 777 /var/www
 <form class="" id="add_downloadble" method="post" enctype="multipart/form-data" name="upload" >
                    <div class="control-group">
                        <label class="control-label" for="inputEmail">File:</label>
                        <div class="form-group">


            <input name="uploaded_file"  class="form-control" id="fileInput" type="file" required>

                            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
                            <input type="hidden" name="id" value="<?php echo $session_id ?>"/>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="name" Placeholder="File Name"  class="form-control" required>
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="controls">
                            <input type="text" name="desc" Placeholder="Description"  class="form-control" required>
                        </div>
                    </div>


                  <script>
                  jQuery(document).ready(function($){                        
                      e.preventDefault();
                      var _this = $(e.target);
                      var formData = new FormData($(this)[0]);
                      $.ajax({
                        type: "POST",
                        url: "save.php",
                        data: formData,
                        success: function(html){                              
                          window.location = 'tambah_materi.php';
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                      });
                    });
                  });
                  </script> 


              </div>
              <div class="col-sm-6">

            <div class="alert alert-info">Centang kelas yang ingin di tambahkan file ini.</div>

                        <div class="pull-left">
                    Check All <input type="checkbox"  name="selectAll" id="checkAll" />
                      <script>
                      $("#checkAll").click(function () {
                        $('input:checkbox').not(this).prop('checked', this.checked);
                      });
                      </script>         
                    </div>
                            <table cellpadding="0" cellspacing="0" border="0" class="table" id="">

                          <thead>
                                  <tr>
                              <th></th>
                              <th>Nama Kelas</th>
                              <th>Subject Code</th>
                              </tr>

                          </thead>
                          <tbody>

                                      <?php $query = mysql_query("select * from teacher_class
                          LEFT JOIN class ON class.class_id = teacher_class.class_id
                          LEFT JOIN subject ON subject.subject_id = teacher_class.subject_id
                          where teacher_id = '$session_id' and school_year = '$school_year' ")or die(mysql_error());
                          $count = mysql_num_rows($query);


                          while($row = mysql_fetch_array($query)){
                          $id = $row['teacher_class_id'];

                          ?>                             
                          <tr id="del<?php echo $id; ?>">
                            <td width="30">
                              <input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
                            </td>
                            <td><?php echo $row['class_name']; ?></td>
                            <td><?php echo $row['subject_code']; ?></td>                                                                   
                          </tr>

                  <?php } ?>



                </tbody>
              </table>


                            </div>
            <div class="col-sm-12">
            <hr>
              <center>
              <div class="control-group">
                    <div class="controls">
                      <button name="Upload" type="submit" value="Upload" class="btn btn-success" /><i class="icon-upload-alt"></i>&nbsp;Upload</button>
                    </div>
              </div>
              </center>

               </form>    
        include('../session.php');
        //Include database connection details
        include("../dbConnector.php"); 
        $connector = new DbConnector();

        $errmsg_arr = array();
        //Validation error flag
        $errflag = false;

        $uploaded_by_query = mysql_query("select * from teacher where teacher_id = '$session_id'")or die(mysql_error());
        $uploaded_by_query_row = mysql_fetch_array($uploaded_by_query);
        $uploaded_by = $uploaded_by_query_row['firstname']."".$uploaded_by_query_row['lastname'];

        /* $id_class=$_POST['id_class']; */
        $name=$_POST['name'];





        //Function to sanitize values received from the form. Prevents SQL injection
        function clean($str) {
            $str = @trim($str);
            if (get_magic_quotes_gpc()) {
                $str = stripslashes($str);
            }
            return mysql_real_escape_string($str);
        }

        //Sanitize the POST values
        $filedesc = clean($_POST['desc']);
        //$subject= clean($_POST['upname']);

        if ($filedesc == '') {
            $errmsg_arr[] = ' file diskripsi belum di isi';
            $errflag = true;
        }

        if ($_FILES['uploaded_file']['size'] >= 1048576 * 5) {
            $errmsg_arr[] = 'file selected exceeds 5MB size limit';
            $errflag = true;
        }


        //If there are input validations, redirect back to the registration form
        if ($errflag) {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            session_write_close();
            ?>

           <script>
           window.location = 'downloadable.php<?php echo '?id='.$get_id;  ?>';
           </script>
           <?php exit();
        }
        //upload random name/number
        $rd2 = mt_rand(1000, 9999) . "_File";

        //Check that we have a file
        if ((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
            //Check if the file is JPEG image and it's size is less than 350Kb
            $filename = basename($_FILES['uploaded_file']['name']);

            $ext = substr($filename, strrpos($filename, '.') + 1);

            if (($ext != "exe") && ($_FILES["uploaded_file"]["type"] != "application/x-msdownload")) {
                //Determine the path to which we want to save this file      
                //$newname = dirname(__FILE__).'/upload/'.$filename;
                $newname = "../admin/uploads/" . $rd2 . "_" . $filename;
                $name_notification  = 'Add Downloadable Materials file name'." ".'<b>'.$name.'</b>';
                //Check if the file with the same name is already exists on the server
                if (!file_exists($newname)) {
                    //Attempt to move the uploaded file to it's new place
                    if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname))) {
                        //successful upload
                        // echo "It's done! The file has been saved as: ".$newname;        

                                    $id=$_POST['selector'];
                                                    $N = count($id);
                                                    for($i=0; $i < $N; $i++)
                                                    {           

                       /*  $qry2 = "INSERT INTO files (fdesc,floc,fdatein,teacher_id,class_id,fname,uploaded_by) VALUES ('$filedesc','$newname',NOW(),'$session_id','$id[$i]','$name','$uploaded_by')"; */
                        mysql_query("INSERT INTO files (fdesc,floc,fdatein,teacher_id,class_id,fname,uploaded_by) VALUES ('$filedesc','$newname',NOW(),'$session_id','$id[$i]','$name','$uploaded_by')");
                        mysql_query("insert into notification (teacher_class_id,notification,date_of_notification,link) value('$id[$i]','$name_notification',NOW(),'downloadable_student.php')")or die(mysql_error());

                      }

        }
        }
        }
        }

        /* mysql_close(); */
        ?>