我在php

时间:2017-03-22 13:22:54

标签: php database upload

我有这个表单,它接受文本输入,在它下面你可以上传图像,最多四个(但它是可选的,你可以选择不上传图像)。 然后将文本和图像或图像分配给数据库中当前登录的用户。 然而,我遇到了一个挑战,当我使用所有可用的图像上传输入时,它成功上传并分配给数据库。 但是当我上传少于4个1,2或3时,没有任何反应。请帮助检查下面的代码。

<?php 
include("ot_includes/session.php");

include ('ot_includes/profile_info_fetcher.php');
include ('ot_includes/header.php'); 

include ('ot_includes/db_connect.php');
include("ot_includes/forum_profile_header.php");




// Begining of news for m processor



$current_time= time();
$last_create_time_diff=($current_time - $get_last_create_time);

if (isset
  ($_POST["submit_topic"]) AND 

  !empty($_POST["topic_title"]) AND 

  !empty($_POST["topic_body"]) AND 

  !empty($_POST["topic_category"] ) 
  AND

  ( $last_create_time_diff > 1800 )


  AND 

  (strlen($_POST["topic_title"]) < 101)
  ) 
{

    $topic_create_time= time();
  $topic_title= $_POST['topic_title'] ;
  $topic_body= $_POST['topic_body'] ;
  $topic_category= $_POST['topic_category'] ;


//upload image 



//   if(!isset($_SESSION['login_user'])){
//       echo('<script> window.location = "../index.php";</script>');

// }


   if(isset($_FILES['upload_thread_image1'])){
      $errors= array();
      //for first image
      $file_name_1 = $_FILES['upload_thread_image1']['name'];
      $file_size_1 =$_FILES['upload_thread_image1']['size'];
      $file_tmp_1 =$_FILES['upload_thread_image1']['tmp_name'];
      $file_type_1=$_FILES['upload_thread_image1']['type'];
      $file_explode_1=explode('.',$_FILES['upload_thread_image1']['name']);
      $file_ext_1=strtolower(end( $file_explode_1));


      //for second image
      $file_name_2 = $_FILES['upload_thread_image2']['name'];
      $file_size_2 =$_FILES['upload_thread_image2']['size'];
      $file_tmp_2 =$_FILES['upload_thread_image2']['tmp_name'];
      $file_type_2=$_FILES['upload_thread_image2']['type'];
      $file_explode_2=explode('.',$_FILES['upload_thread_image2']['name']);
      $file_ext_2=strtolower(end( $file_explode_2));


   //for third image
      $file_name_3 = $_FILES['upload_thread_image3']['name'];
      $file_size_3 =$_FILES['upload_thread_image3']['size'];
      $file_tmp_3 =$_FILES['upload_thread_image3']['tmp_name'];
      $file_type_3=$_FILES['upload_thread_image3']['type'];
      $file_explode_3=explode('.',$_FILES['upload_thread_image3']['name']);
      $file_ext_3=strtolower(end( $file_explode_3));


        //for fourth image
      $file_name_4 = $_FILES['upload_thread_image4']['name'];
      $file_size_4 =$_FILES['upload_thread_image4']['size'];
      $file_tmp_4 =$_FILES['upload_thread_image4']['tmp_name'];
      $file_type_4=$_FILES['upload_thread_image4']['type'];
      $file_explode_4=explode('.',$_FILES['upload_thread_image4']['name']);
      $file_ext_4=strtolower(end( $file_explode_4));




      $current_time=time();
      $expensions= array("jpeg","jpg","png");

      if( 

        (in_array($file_ext_1,$expensions) === false) OR  
        (in_array($file_ext_2,$expensions) === false) OR  
        (in_array($file_ext_3,$expensions) === false) OR  
        (in_array($file_ext_4,$expensions) === false)  

       )

      {
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(
        ($file_size_1 > 2000000 ) OR
        ($file_size_2 > 2000000) OR
        ($file_size_3 > 2000000) OR
        ($file_size_4 > 2000000) 
        ){
         $errors[]='File size must not exceed 2 MB';
      }


      if(empty($errors)==true){
        $current_time=time();
        $file_name_1=$current_time.$get_username.$file_name_1;
        $file_name_2=$current_time.$get_username.$file_name_2;
        $file_name_3=$current_time.$get_username.$file_name_3;
        $file_name_4=$current_time.$get_username.$file_name_4;

         // $file_name=$file_name.$current_time;
         // $insert_profile_pic= mysqli_query($db_connect,"  oton_users SET user_profile_pic='$file_name' WHERE user_name='$user_check' ");


//            echo '




// <br>
// <div class="row" >


// <h2 align="center" class="alert alert-success"><p><i class="fa fa-5x fa-smile-o"></i></p>Your profile picture has been successfully updated !!!<br> Please click <a href="edit_profile.php"> <u> here</u></a> to go back</h2>


// </div>
// <hr>
// ';
//       }


//       else{
//          echo '




// <br>
// <div class="row" >


// <h2 align="center" class="alert alert-danger"><p><i class="fa fa-5x fa-frown-o"></i></p>Sorry there was an error updating your profile picture<br>Please make sure its less than 2MB and its either jpg,jpeg,or png format<br> Please click <a href="edit_profile.php"> <u> here</u></a> to go back</h2>


// </div>
// <hr>
// ';
//       }




move_uploaded_file($file_tmp_1,"ot_imgs/ot_thread_imgs/".$file_name_1);

  move_uploaded_file($file_tmp_2,"ot_imgs/ot_thread_imgs/".$file_name_2);  
  move_uploaded_file($file_tmp_3,"ot_imgs/ot_thread_imgs/".$file_name_3);
  move_uploaded_file($file_tmp_4,"ot_imgs/ot_thread_imgs/".$file_name_4);





if (empty($file_name_1) OR empty($file_name_2) or empty($file_name_3) or empty($file_name_4) ) {


$file_name_1=" " ;
$file_name_2=" " ;
$file_name_3=" " ;
$file_name_4=" " ;
}


  $insert_topic= "INSERT INTO oton_topics (topic_title,topic_body,topic_category,topic_create_time,topic_by,topic_image_1,topic_image_2,topic_image_3,topic_image_4) VALUES ('$topic_title','$topic_body','$topic_category','$topic_create_time','$get_username','$file_name_1','$file_name_2','$file_name_3','$file_name_4')";
  $topic_result=mysqli_query($db_connect,$insert_topic);

  $update_last_create_time=mysqli_query($db_connect,"UPDATE oton_users SET user_last_create_time='$topic_create_time' WHERE user_id='$get_user_id' ");




if ($topic_result AND $update_last_create_time ) {

    $topic_report= '<div class="alert alert-success" align="center"><i class="fa fa-2x fa-check-circle"></i> Your topic has been posted !!.View your posts  <a href ="view_thread.php?latest_post=1 "><u>here</u></a> </div>';




 } 

  } //from image conditions
}//from image conditions


}



elseif (isset
  ($_POST["submit_topic"]) OR 

  ($_POST["topic_title"] == " ") OR 

  ($_POST["topic_body"] == " ") OR 

  ($_POST["topic_category"] ==" ") 
  AND 

  ( $last_create_time_diff < 1800 )


  AND !$topic_result
  ) 
{


   $topic_report="<div class='alert alert-danger'><i class='fa fa-2x fa-warning'></i> There was an error,check the following<br>-Make sure no field is empty.<br>-You have to wait for 30 minutes from the last time you posted<br>-Make sure your title is less than or equal to 100 characters including spaces<br>-Kindly close this tab completely and create a topic in a new tab</div>";

}

//End of news form processor

unset($_POST);


?>


<!--Begin welcome section -->
 <section class="container">
        <div class="row">
            <div class="col-sm-12" align="center">
                <h2>Hello <?php echo "$get_username";  ?>!</h2>
                <p>What would you like to do at the moment ?</p>

                <?php echo "$quick_links";  ?>



            </div>


           </div>
           </section>

<hr>




<section class="container">

<?php echo $topic_report ;?>

<h1 class="page-header"><i class="fa fa-2x fa-plus"></i> Add New Topic</h1><br>


  <form  method="POST" action=""  enctype="multipart/form-data">
<br>
<div class="row" >


<div class='col-sm-offset-1 col-sm-10 '>


            <div class='form-group'>



      <label>Title</label>

<input class="form-control" align="center" type="text" name="topic_title" />
<br>

      <label>Category</label>
<select class="form-control" name="topic_category">
                      <option value=" ">Select...</option>
                      <option value="sports">Sports</option>
                      <option value="another">another</option>
                      <option value="another2">another2</option>
                      <option value="another3">another3</option>



                      </select> 

<label>Body</label>
<textarea id="tinymyceArea" class="form-control" type="text" name="topic_body" ></textarea>


<br>
<input type="file" name="upload_thread_image1" class="btn btn-danger" />
<input type="file" name="upload_thread_image2" class="btn btn-danger" />
<input type="file" name="upload_thread_image3" class="btn btn-danger" />
<input type="file" name="upload_thread_image4" class="btn btn-danger" />
<br>

<input class="form-control btn btn-danger btn-block" type="submit" name="submit_topic" value="Post Topic" />
   <br>



   </div>
        </div>
</div>


</form>

</section>




<?php include ('ot_includes/footer.php'); ?>

2 个答案:

答案 0 :(得分:0)

当您检查文件名是否为空时,如果file_name_2为空,那么您将所有文件重命名为&#34; &#34;(空)

if (empty($file_name_1) OR empty($file_name_2) or empty($file_name_3) or empty($file_name_4) ) {
    $file_name_1=" " ;
    $file_name_2=" " ;
    $file_name_3=" " ;
    $file_name_4=" " ;
}

您应该单独检查名称是否为空,并仅重命名这些文件名。

if (empty($file_name_1)) $file_name_1=" " ;
if (empty($file_name_2)) $file_name_2=" " ;
if (empty($file_name_3)) $file_name_3=" " ;
if (empty($file_name_4)) $file_name_4=" " ;

答案 1 :(得分:0)

感谢大家,我已经解决了这个问题,为了这里可能有类似问题的人,处理上传的代码就是这样。

   if(isset($_FILES['upload_thread_image1'])){
      $errors= array();
      //for first image
      $file_name_1 = $_FILES['upload_thread_image1']['name'];
      $file_size_1 =$_FILES['upload_thread_image1']['size'];
      $file_tmp_1 =$_FILES['upload_thread_image1']['tmp_name'];
      $file_type_1=$_FILES['upload_thread_image1']['type'];
      $file_explode_1=explode('.',$_FILES['upload_thread_image1']['name']);
      $file_ext_1=strtolower(end( $file_explode_1));




      $current_time=time();
      $expensions= array("jpeg","jpg","png");

      if( 

        (in_array($file_ext_1,$expensions) === false)

       )

      {
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(
        $file_size_1 > 2000000 

        ){
         $errors[]='File size must not exceed 2 MB';
      }


      if(empty($errors)==true){
        $current_time=time();
        $file_name_1=$current_time.$get_username.$file_name_1;

}



move_uploaded_file($file_tmp_1,"ot_imgs/ot_thread_imgs/".$file_name_1);
}






if(isset($_FILES['upload_thread_image2'])){
      $errors= array();
      //for second image
      $file_name_2 = $_FILES['upload_thread_image2']['name'];
      $file_size_2 =$_FILES['upload_thread_image2']['size'];
      $file_tmp_2 =$_FILES['upload_thread_image2']['tmp_name'];
      $file_type_2=$_FILES['upload_thread_image2']['type'];
      $file_explode_2=explode('.',$_FILES['upload_thread_image2']['name']);
      $file_ext_2=strtolower(end( $file_explode_2));





      $current_time=time();
      $expensions= array("jpeg","jpg","png");

      if( 

        (in_array($file_ext_2,$expensions) === false)

       )

      {
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(
        $file_size_2 > 2000000 

        ){
         $errors[]='File size must not exceed 2 MB';
      }


      if(empty($errors)==true){
        $current_time=time();
        $file_name_2=$current_time.$get_username.$file_name_2;




      }



      move_uploaded_file($file_tmp_2,"ot_imgs/ot_thread_imgs/".$file_name_2);
 }//End for image 2





 if(isset($_FILES['upload_thread_image3'])){
      $errors= array();

   //for third image
      $file_name_3 = $_FILES['upload_thread_image3']['name'];
      $file_size_3 =$_FILES['upload_thread_image3']['size'];
      $file_tmp_3 =$_FILES['upload_thread_image3']['tmp_name'];
      $file_type_3=$_FILES['upload_thread_image3']['type'];
      $file_explode_3=explode('.',$_FILES['upload_thread_image3']['name']);
      $file_ext_3=strtolower(end( $file_explode_3));









      $current_time=time();
      $expensions= array("jpeg","jpg","png");

      if( 

        (in_array($file_ext_3,$expensions) === false)

       )

      {
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(
        $file_size_3 > 2000000 

        ){
         $errors[]='File size must not exceed 2 MB';
      }


      if(empty($errors)==true){
        $current_time=time();
        $file_name_3=$current_time.$get_username.$file_name_3;
 }

move_uploaded_file($file_tmp_3,"ot_imgs/ot_thread_imgs/".$file_name_3);

}//End for Image 3


if(isset($_FILES['upload_thread_image4'])){
      $errors= array();

        //for fourth image
      $file_name_4 = $_FILES['upload_thread_image4']['name'];
      $file_size_4 =$_FILES['upload_thread_image4']['size'];
      $file_tmp_4 =$_FILES['upload_thread_image4']['tmp_name'];
      $file_type_4=$_FILES['upload_thread_image4']['type'];
      $file_explode_4=explode('.',$_FILES['upload_thread_image4']['name']);
      $file_ext_4=strtolower(end( $file_explode_4));






$current_time=time();
      $expensions= array("jpeg","jpg","png");

      if( 

        (in_array($file_ext_4,$expensions) === false)

       )

      {
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if(
        $file_size_4 > 2000000 

        ){
         $errors[]='File size must not exceed 2 MB';
      }


      if(empty($errors)==true){
        $current_time=time();
        $file_name_4=$current_time.$get_username.$file_name_4;
       }

move_uploaded_file($file_tmp_4,"ot_imgs/ot_thread_imgs/".$file_name_4);

}//end for image 4

然后我根据需要将每个图像变量输入到数据库中,即$ file_name_1,$ file_name_2 .......