无法上传表单中的多个文件

时间:2016-05-05 17:19:38

标签: php html

如果这是转贴,我很抱歉。但是我没有找到正确答案就看到了很多问题 我正在尝试上传多个文件+一些信息,但如果我提交我的表格有2个图像就可以了,脚本运行完美,当我上传2或3个文件时,我得到所有表单元素的未定义索引。

up.php

   /////// Random name generator ////////
function random_name($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));

for ($i = 0; $i < $length; $i++) {
    $key .= $keys[array_rand($keys)];
}

   return $key;
}

 //sql//
  require("sql.php");
 //////////
 //!!! some vars !!!//
 //
     $total = count($_FILES['pimages']['name']);
 //
 $foldername = random_name(15);
  $target_dir = "../images/projects/".$foldername."/";
   $target_file = $target_dir . basename($_FILES["icon"]["name"]);
     $uploadyes = 1;
    $imageType = pathinfo($target_file,PATHINFO_EXTENSION);
   $saveicon = $target_dir . "icon." .$imageType;

    /////submited form vars /////

  $linkedid = $_POST['lid'];

  $date = date("y.m.d H:i:s");
  $name = $_POST['projectname'];
  $loc = $_POST['location'];
  $type = $_POST['type'];
  $des = $_POST['des'];
  $precara = $_POST['cara'];
  $client = $_POST['client'];
  $col = $_POST['cost'];
  $bua = $_POST['builtup'];

  ////////////////cara slice /////////////
  $caraxarray = explode("," , $precara);
  $cara = base64_encode(serialize($caraxarray));









  echo $imageType ;




  ///////////////////////// Start of the upload check ////////////////////
    if(isset($_POST['submit']) && !empty($name)) {
   $conn = mysqli_connect($servername, $username, $password, $dbname);
   // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  } 
 }










     // Check if $uploadyes is set to 0 by an error
    if (!isset($_POST['submit'])) {
       echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
   } else {
      mkdir($target_dir);



      if (move_uploaded_file($_FILES["icon"]["tmp_name"], $saveicon)) {
             //////////////////////////..........................//////////////////
      // Loop through each file
    $imgext = array();
    for($i=0; $i<=$total; $i++) {
     //Get the temp file path
    $tmpFilePath = $_FILES['pimages']['tmp_name'][$i];

    $x = $i + 1 ;
    if ($tmpFilePath != ""){
     //Setup our new file path
  $pimgex = $_FILES['pimages']['name'][$i];

  $pimageType = pathinfo($pimgex,PATHINFO_EXTENSION);

  $newFilePath = $target_dir ."img".$x.".".$pimageType;
  array_push($imgext , "$newFilePath");
      //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

       echo "yeaaaaaah";

     }
   }
 }
   $str = serialize($imgext);
   $sql1 = "INSERT INTO projects (date, name, type, location, icon, imgext, folder, linkedid)
    VALUES ('$date', '$name','$type', '$loc', '$saveicon' , '$str', '$foldername',   '$linkedid')";
 $sql2 = "INSERT INTO projectdetails (proname, prolocation, prodes, procara,      client, col, builtarea, linkedid)
 VALUES ('$name', '$loc','$des', '$cara', '$client' , '$col', '$bua',         '$linkedid')";
    mysqli_query($conn ,$sql1);
    mysqli_query($conn ,$sql2);
    mysqli_close($conn);
/////////////////...........................////////////////////////
        header("location:cp.php");
    } else {
    echo "Sorry, there was an error uploading your file.";
    }
}

projectsuploader.php

   $lkid = random_name(8);
 $tlink = random_name(6);
 require("sql.php");
 // Create connection
 $conn = mysqli_connect($servername, $username, $password, $dbname);
 // Check connection
 if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

   $sql1 = "SELECT id, date, name, type, location FROM projects";
   $sql2 = "SELECT id, titleen FROM projectsnav";
   $result = mysqli_query($conn, $sql1);
   $types = mysqli_query($conn, $sql2);



        //mysqli_close($conn);   

      ?>
      <!DOCTYPE html>
     <html>

   <h2>Projects Page</h2>

   <h5>projects</h5>
   <table>
   <tr>
   <td>#</td>
   <td>Date & Time</td>
   <td>Project name</td>
  <td>Project type</td>
   <td>Project location</td>
 <!--<td>View</td>
 <td>Edit</td>-->
   <td>Remove</td>
  </tr>
   <?php

  if (mysqli_num_rows($result) > 0) {
     // output data of each row .$row["id"]
     while($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>".$row["id"]."</td>";
        echo "<td>".$row["date"]."</td>";
        echo "<td>".$row["name"]."</td>";
        echo "<td>".$row["type"]."</td>";
        echo "<td>".$row["location"]."</td>";
        echo "<td><a href='../del.php?id=".$row['id']."'>Remove</a></td>         </tr>";
     }
    } else {
     echo "0 results";
    }
     mysqli_close($conn);
   ?>
   </table>
   <h4 id="addproject">Add Project</h3>
   <h4 id="addtype">Add Type </h4>
   <div id="frontlayer">
  <div id="addpro">
  <h2 style="text-align:center;"> add project </h2>
   <form method="POST" action="up.php" enctype="multipart/form-data" >
    <input type="hidden" name="MAX_FILE_SIZE" value="50000000">
   id:<input type="text" name="lid" value="<?php echo $lkid ; ?>" readonly>    <br>
    project name:<input type="text" name="projectname"><br>
    Type:<select name="type">

     <?php
     if (mysqli_num_rows($types) > 0){
     while($navrow = mysqli_fetch_assoc($types)) {
        echo "<option value='".$navrow['titleen']."'>".$navrow['titleen']."     </option>";
    }
    }else{
        echo "<option>PLEASE ADD TYPES TO DATABASE FIRST!!!!ERROR 0 TYPES IN       DATABASE</option>";
    }

   mysqli_close($conn);
   ?>

  </select>
<br>
  location:<input type="text" name="location"><br>
  icon:<input type="file" name="icon" id="icon"><br>
 images:<input type="file" name="pimages[]" id="pimages" multiple><br>
 <input type="hidden" name="sendfiles" value="Send Files" />
 <!--
 ------//////////////------
 ------//////////////------
 ------//////////////------
 -->
 description:<input type="text" name="des"><br>
 caracteristic:<input type="text" name="cara" data-role="tagsinput"><br>
 client:<input type="text" name="client"><br>
Collaborator:<input type="text" name="cost"><br>
Gross Area:<input type="text" name="builtup"><br>

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

</form>
</div>

对不起,如果它是坏的,我是乞丐,请提前感谢任何帮助

1 个答案:

答案 0 :(得分:0)

问题是有一个大文件(4mb图像)并且php无法发布此图像大小的图像,因此您必须在上传之前调整图像大小。 非常感谢Felippe Duarte。