Javascript 如何让用户在不刷新页面的情况下从刚刚上传的多个图像中选择一个图像?

Javascript 如何让用户在不刷新页面的情况下从刚刚上传的多个图像中选择一个图像?,javascript,php,jquery,image-uploading,Javascript,Php,Jquery,Image Uploading,我有一个表单,用户可以上传多个图像。我需要一种方法,使用户能够选择一个图像从他刚刚上传的图像作为他的主要图片,然后再提交表单 我已经使用JavaScript文件阅读器加载了预览。我试图通过JavaScript向用户选择的图像添加一个名称,这样就可以在PHP脚本中作为post元素访问它。但它不能工作,因为它不能作为文件访问。在发布这个问题之前,我已经花了整整3天的时间。这将是一个巨大的帮助,任何人都可以告诉我如何解决这个问题 表格如下: <input type="file" name="fi

我有一个表单,用户可以上传多个图像。我需要一种方法,使用户能够选择一个图像从他刚刚上传的图像作为他的主要图片,然后再提交表单

我已经使用JavaScript文件阅读器加载了预览。我试图通过JavaScript向用户选择的图像添加一个名称,这样就可以在PHP脚本中作为post元素访问它。但它不能工作,因为它不能作为文件访问。在发布这个问题之前,我已经花了整整3天的时间。这将是一个巨大的帮助,任何人都可以告诉我如何解决这个问题

表格如下:

<input type="file" name="files[]" multiple="true" id="file">
<div class="list"></div>

但是我可以做任何事情。

我认为您的问题在于从文件列表中选择特定文件的方式:

$main_img_path = $_FILES['selectImage'];
我已经有一段时间没有使用PHP了,但是在我看来,如果已经在服务器上循环文件,为什么不在循环时检查主映像呢?类似这样的内容(假设$\u POST['selectImage']包含所选图像的临时文件名):

$total=count($_FILES['FILES']['name']);
//循环浏览每个文件

对于($i=0;$i,如果没有看到一些代码,我将很难提供帮助。;)谢谢,我已经上传了代码。很抱歉,我认为代码可能太多余了。谢谢,我在下面给出了一个可能的答案。
    $total = count($_FILES['files']['name']);
    // Loop through each file
    for($i=0; $i<$total; $i++) {
      //Get the temp file path
      $tmpFilePath = $_FILES['files']['tmp_name'][$i];
      //Make sure we have a filepath
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "gig_pics/" . $_FILES['files']['name'][$i];
        //Upload the file into the temp dir
        move_uploaded_file($tmpFilePath, $newFilePath);
        $file_sql = "INSERT INTO `gig_pics`(id,gig_id,pic) VALUES ('','$gid','$newFilePath')";
        $file_res = mysqli_query($conn,$file_sql);
      }
    }
    $main_img_path = $_POST['selectImage'];
    $main_img_path = $_FILES['selectImage'];
$main_img_path = $_FILES['selectImage'];
$total = count($_FILES['files']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['files']['tmp_name'][$i];
  //Make sure we have a filepath
  if ($tmpFilePath != ""){

    if ($tmpFilePath === $_POST['selectImage']) {
        // This is the selected image
    }
    //Setup our new file path
    $newFilePath = "gig_pics/" . $_FILES['files']['name'][$i];
    //Upload the file into the temp dir
    move_uploaded_file($tmpFilePath, $newFilePath);
    $file_sql = "INSERT INTO `gig_pics`(id,gig_id,pic) VALUES ('','$gid','$newFilePath')";
    $file_res = mysqli_query($conn,$file_sql);
  }
}