如何使用会话在另一个页面中显示多个图像


how do i show multiple image in another page using session

<?php
lets have a look in my code i am uploading multiple images using form action
here i am uploading multiple images
Session_Start();
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 
 
   $upload_path = "/var/www/html/addtocart/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
                           $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
                           $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
                           $file_extension = end($ext); //store extensions in the variable
                           $target_path = $upload_path . basename($_FILES["file"]["name"][$i]);//set the target path with a new name of image
         
 
                      $j = $j + 1;//increment the number of uploaded images according to the files in array       
if (file_exists($target_path)) { 
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}      
else { 
if(($_FILES["file"]["size"][$i] <50000000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions))
 {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j.') .<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
               
$File_Name=  basename( $_FILES["file"]["name"][$i]);
     echo $File_Name. "</br>"; 
       
    
$var_holding_img = "<img src='$File_Name' alt='picture' width='200' height='256'><br/>"; 
$string =   $var_holding_img ; //here i want to store multiple images and show all the images in another page
$_SESSION['File_Name'] = $string;
echo $_SESSION['File_Name'] ; 
  

            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>'; 
        }
}
 
    }
echo '<script type="text/javascript">        
      
function Link1()      
{      
if(document.Form1.checkbox1.checked == true)       
{      
alert("Want to Add Some More Medicines.");  
 location.href="products.php"; 
}      
}      
function Link2()      
{      
if(document.Form1.checkbox2.checked == true)       
{      
alert("You have clicked on Make Delivery At Home.");  
location.href="billing.php";      
}      
} 
       
</script>';
  
echo "<form name='Form1' style='color: green ; font-size: 150%' action = '#'> 
 <input type='radio' name='checkbox1' value='' onclick = 'Link1()'  />Want to Add Some More Medicines<br>
 <input type='radio' name='checkbox2' value='' onclick = 'Link2()'  />Make Delivery At Home<br>
</form>";
}

  1. $_SESSION['File_Name']应该是数组
  2. 你需要在两页上都session_start ();
  3. 要在第二页上显示所有图像,您需要通过循环中的数组进行迭代,并按照您希望的结果显示方式生成html

示例:我不知道从你的代码中你是如何获得图像的链接的(从DB或用JS从第一页删除它们,或从文件或通过目录获得它们…),所以我假设你有$_SESSION['fileNames'] = ['src1','src2','src3'];

然后我会做这样的事情:

$imgSrcArray = $_SESSION['fileNames'];
echo '<ul>';
foreach ($imgSrcArray as $src) {
    echo '<li><img src="'.$src.'"/></li>';
}
echo '</ul>';

当然,调整html以适应您。

第二次编辑:选中此项:http://php.net/manual/en/function.scandir.php然后用图像的文件名填充会话数组。然后像这样更改上面的代码echo '<li><img src=HERE_GOES_PATH_TO_YOUR_FOLDER/"'.$src.'"/></li>';