如何使用会话

时间:2016-04-08 07:42:16

标签: php



<?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>";



}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

  1. $_SESSION['File_Name']应该是数组。
  2. 您需要在两个页面上session_start ();
  3. 要显示第二页上的所有图像,您需要循环遍历数组并以您希望的方式生成html以显示结果...
  4. 示例: 我不知道你的代码如何获得你的图像的链接(从数据库或从第一页用JS报废它们或从文件或通过目录获取它们)...所以我假设你有{{1 }}

    然后我会做这样的事情:

    $_SESSION['fileNames'] = ['src1','src2','src3'];

    当然,请调整html,因为它适合你。

    第二次修改: 检查一下:http://php.net/manual/en/function.scandir.php然后使用图像的文件名填充会话数组。然后更改上面的代码$imgSrcArray = $_SESSION['fileNames']; echo '<ul>'; foreach ($imgSrcArray as $src) { echo '<li><img src="'.$src.'"/></li>'; } echo '</ul>';