在这种情况下,我如何将多个图像上传到远程服务器?循环或重复上传过程


How do i upload multiple images to remote server in this circumstances?Looping or repeat upload procedure?

我是php的新手,正在尝试创建一个表单,该表单允许我上传几个文本字段,然后上传3个图像,然后将图像上传到远程服务器,并将图像的名称保存到数据库中,以便稍后提取并显示。(附言:忽略SQL注入问题,我只是还没有时间。谢谢)

目前我正在本地主机上测试这个。我的问题是我应该循环它,还是应该只上传3次?下面是我试过的。但循环试用并没有上传文件,它只是直接进入无效文件。此外,我想在上传之前确保所有3个文件都是有效的文件,我是否必须先循环验证,然后再循环上传?

请给我一些建议,谢谢你抽出时间。

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';

if ($_POST)
{
 //get form data

 $Listingname = addslashes(strip_tags($_POST['Listingname']));
 $Location = addslashes(strip_tags($_POST['Location']));
 $nobed = addslashes(strip_tags($_POST['nobed']));
 $zip = addslashes(strip_tags($_POST['zip']));
 $price = addslashes(strip_tags($_POST['price']));
 $username=addslashes(strip_tags($_POST[$_SESSION['username']]));
 if (!$Listingname||!$nobed||!$nobed||!$zip||!$price)
    echo "Please fill out all fields"; 
    else 
    for($i=0;$i<count($_FILES["image"]["name"]);$i++)
    {$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 400000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"][$i] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"][$i] . " already exists please add another file, or change the. ";
      }
    else
      {
        $photo=$_FILES["file"]["name"][$i];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/$photo");
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"][$i];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
    {
       $username=$_SESSION['username'];
           //register into database
            mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES 
                ('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error());

            echo "Listing Added";

       }
    }
 }

else
{
?>
<form action="Submitlisting2.php" method="post"
enctype="multipart/form-data">
Listing Name:<br />
<input type='text' name='Listingname'><p />
Location:<br />
<input type='text' name='Location'><p />
Number of Beds:<br />
<input type='test' name='nobed'><p />
Zip:<br />
<input type='text' name='zip'><p />
Price:<br />
<input type='text' name='price'><p />

<label for="file">Pic1(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file"><br>
<label for="file">Pic2(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file"><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
}

?>

尝试不循环(这会变得非常长,尝试只上传了2张图像,这是我想循环的一个原因)

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';

if ($_POST)
{
 //get form data

 $Listingname = addslashes(strip_tags($_POST['Listingname']));
 $Location = addslashes(strip_tags($_POST['Location']));
 $nobed = addslashes(strip_tags($_POST['nobed']));
 $zip = addslashes(strip_tags($_POST['zip']));
 $price = addslashes(strip_tags($_POST['price']));
 $username=addslashes(strip_tags($_POST[$_SESSION['username']]));
 if (!$Listingname||!$nobed||!$nobed||!$zip||!$price)
    echo "Please fill out all fields"; 
    else 
    {$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 400000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    }

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists please add another file, or change the. ";
      }
  }
    }
}
    else
      {$allowedExts1 = array("gif", "jpeg", "jpg", "png");
$temp1 = explode(".", $_FILES1["file1"]["name1"]);
$extension1 = end($temp1);
if ((($_FILES1["file"]["type"] == "image/gif")
|| ($_FILES1["file"]["type"] == "image/jpeg")
|| ($_FILES1["file"]["type"] == "image/jpg")
|| ($_FILES1["file"]["type"] == "image/pjpeg")
|| ($_FILES1["file"]["type"] == "image/x-png")
|| ($_FILES1["file"]["type"] == "image/png"))
&& ($_FILES1["file"]["size"] < 400000)
&& in_array($extension1, $allowedExts1))
  {
  if ($_FILES1["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES1["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES1["file"]["name"] . "<br>";
    echo "Type: " . $_FILES1["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES1["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES1["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES1["file"]["name"]))
      {
      echo $_FILES1["file"]["name"] . " already exists please add another file, or change the. ";
      }
    }
  }
else
    { $photo=$_FILES["file"]["name"];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/$photo");
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
      else
  {
  echo "Pic 1 Invalid file" and die("Unable to upload pic1");
  }
    } 
      {
        $photo1=$_FILES1["file"]["name"];
      move_uploaded_file($_FILES1["file"]["tmp_name"],
      "upload/$photo1");
      echo "Stored in: " . "upload/" . $_FILES1["file"]["name"];
      }

else
  {
  echo "Pic 2 Invalid file" and die("Unable to upload Pic2");
  }

    {
       $username=$_SESSION['username'];
           //register into database
            mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES 
                ('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error());

            echo "Listing Added";

       }

else
{
?>
<form action="Submitlisting2.php" method="post"
enctype="multipart/form-data">
Listing Name:<br />
<input type='text' name='Listingname'><p />
Location:<br />
<input type='text' name='Location'><p />
Number of Beds:<br />
<input type='test' name='nobed'><p />
Zip:<br />
<input type='text' name='zip'><p />
Price:<br />
<input type='text' name='price'><p />

<label for="file">Filename(File must be exceed 4mb):</label>
<input type="file" name="file" id="file"><br>
<label for="file">Filename(File must be exceed 4mb):</label>
<input type="file" name="file1" id="file1"><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
}

?>

如果我说的是更好的用户体验,那么循环比重复上传要好得多,对于重复上传,你可以/应该使用ajax上传。