2014-07-09 132 views
0
<?php 
    error_reporting(0); 
    //$SITE_URL="http://shreebalajiinfotech.com/Android/Download_videos/"; 
    $SITE_URL="http://localhost/"; 
    $DIR="Videos"; 
    if($_REQUEST['category']=="") 
    { 
     echo 'Please pass folder name'; 
    } 
    $dir = $_REQUEST['category']; 
    $result = array(); 
    $SUB=$DIR ."/" .$dir; 
    //var_dump(is_dir($DIR ."/" .$dir)); 
    //$image="http://shreebalajiinfotech.com/Android/Download_videos/Videos/test1/119064635524.jpg"; 
    $cdir = scandir($SUB); 
    //$files2 = scandir($dir, 1); 
    //print_r($cdir);  
    ?>  
     <div id="video_container"> 
     <?php   
     foreach ($cdir as $value) 
     //for ($i=0; $i<count($cdir); $i++) 
     { 
     // if ($cdir[$i] != '.' && $cdir[$i] != '..') 
     //{ 
     //{    
     if (!in_array($value,array(".",".."))) 
     { 
      /*echo "<pre>"; 
      var_dump(each($value)); 
      echo "</pre>"; 
      echo $value; 
      */    
      //echo $key[$value];     
      //print_r($cdir[$i]); 
      $values=explode('.',$value);     
      if($values[1]=="mp4") 
      {     
       //$result[]=$value; 
       //echo $cdir[$i]; 
       //echo "<br/>"; 
     ?> 
     <script src="jquery.min.js"></script> 
     <script type='text/javascript'> 
     window.onload = function(){ 
     // $(document).ready(function(e) { 
     //$('#submit<?=$value;?>').click({ 
     var video = document.getElementById('my_video_<?=$value;?>'); 
     var thecanvas = document.getElementById('thecanvas'); 
     var img = document.getElementById('thumbnail_img'); 
     var div = document.getElementById('Imagecontainer'); 
     var sources = document.getElementById('video<?=$value;?>'); 
     alert(sources.src); 
     var videoname=sources.src.substring(sources.src.lastIndexOf('/')+1); 
      setTimeout(video.pause(draw(video, thecanvas, img,videoname)),6000); 
     if(video.paused==true) 
     { 
       setTimeout(video.play(),2000); 
     } 
     function draw(video, thecanvas, img,videoname) 
     { 
     alert(video); 
     // get the canvas context for drawing 
     var context = thecanvas.getContext('2d'); 
     // draw the video contents into the canvas x, y, width, height 
     context.drawImage(video, 0, 0, thecanvas.width, thecanvas.height); 
     // get the image data from the canvas object 
     var dataURL = thecanvas.toDataURL(); 
     alert(dataURL); 
     // set the source of the img tag 
     var img1 = document.createElement('img'); 
     img1.setAttribute('src', dataURL); 
     document.getElementById('Imagecontainer').appendChild(img1); 
     img1.setAttribute('src', dataURL); 
     $.ajax({ 
     type: "POST", 
     url: "upload.php", 
     data: {image: dataURL,folder:'<?=$_REQUEST['category'];?>',videoname:videoname}, 
     success: function(response) { 
       alert(response); 
       } 
       }); 
     } 
     }; 
     </script> 
     <video id="my_video_<?=$value;?>" class="<?=$value;?>" controls autoplay> 
     <source id="video<?=$value;?>" src="<?=$SITE_URL.$SUB ."/".$value;?>" type="video/mp4" /> 
     </video> 
      <canvas id="thecanvas"> 
     </canvas> 
     <div id="Imagecontainer"></div> 
     <img id="thumbnail_img" alt="Right click to save"/> 
     <?php 
     } 
     ?> 
     <br/> 
     <?php 
     } 
     } 
    ?> </div>    

上面的代碼只對foreach循環中的第一個元素進行迭代。對於其他元素,它有時只取第一個元素的值,但有時會被擠壓。每個循環只能在windows load事件中執行,如何讓這個腳本執行另一個事件。ajax只執行第一次迭代

+5

這是很多需要閱讀的代碼,其中很多隻是html。如果你刪除了所有與邏輯無關的東西(註釋,大部分與你的問題無關的html,php),並且只包含最低限度的問題,那麼你將更有可能獲得響應。 – Hecksa

+0

我已編輯此代碼..請檢查此。 –

+0

如果不仔細看,我很確定問題出在'''''''''''''''''''''''window.onload = function(){'這一行。生成的html代碼有多個腳本塊全部覆蓋'window.onload',所以只有最後一個塊會_win_。 –

回答

0

你想在你的foreach循環中做太多事情。它正在吐出大量的window.onload函數,這對那些試圖理解你的意圖的窮人瀏覽器來說毫無意義。在這種情況下,我認爲你應該把你在php中的視頻文件數組傳遞給一個javascript數組。類似:

<script type='text/javascript'>  
    window.onload = function(){ 
     var videos = <?php echo json_encode($cdir); ?> 

     for(var i = 0; i < videos.length; i++) 
     { 
      //Setup each video 
     } 
    }; 
</script> 

這是通過編碼被稱爲「JavaScript對象表示法」,這JavaScript可以輕而易舉地使用創建一個數組的格式PHP的陣列。一旦你在javascript中的數組而不是php,你可以遍歷每個客戶端,裏面的 window.onload函數,它應該可以解決你遇到的問題。

+0

嗨..我試過用json_encode,但它不能正常工作。我的腳本必須有

+0

我在哪裏「//設置每個視頻」,這就是我的意思。創建任何必需的元素,填充它們等等。如何選擇實現它完全取決於您 - 您沒有真正解釋您想要的頁面,因此沒有其他人可以幫助您。 – Hecksa

+0

我想從視頻中獲取圖像並將其存儲到正在掃描的文件夾中。 –