当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP实现视频文件上传完整实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-09 17:25:49 浏览: 评论:0 

这篇文章主要介绍了PHP实现视频文件上传的技巧,包含了PHP配置信息的设计及大文件的处理,需要的朋友可以参考下

本文以一个完整实例的形式实现了视频文件上传的功能。虽然是比较基础的应用,仍有一定的参考价值。分享给大家供大家参考之用。具体方法如下:

首先,对PHP来说视频也属于文件,我们利用这个道理,可以通过与上传普通文件类似的方式实现上传PHP视频文件。不同的是视频文件一般较大,上传时间长,而php配置文件也对上传文件的大小有限制。

如果需要更好地用PHP实现文件上传,还是推荐使用FTP上传方式。

一、PHP处理代码如下:

  1. <?php 
  2. //设置最多允许10个文件同时上传 
  3. define('MUILTI_FILE_UPLOAD', '10'); 
  4. //设置文件大小不超过5MB 
  5. define('MAX_SIZE_FILE_UPLOAD', '500000' ); 
  6. //设置上传文件的存储目录 
  7. define('FILE_UPLOAD_DIR', '/fileUploads'); 
  8. //允许上传的文件扩展名 
  9. $array_extention_interdite = array( '.flv' , '.wmv' , '.rmvb' , '.php' , '.php3' , '.php4' , '.exe' , '.msi' , '.htaccess' , '.gz' ); 
  10. //显示信息的公共函数 
  11. function func_message($message='', $ok=''){ 
  12.  echo '<table width="100%" cellspacing="0" cellpadding="0" border="0">'; 
  13.  if($ok == true) 
  14.  echo '<tr><td width="50%"> '.$message.'</td></tr>' ; 
  15.  else 
  16.  echo '<tr><td width="50%"> '.$message.'</td></tr>'; 
  17.  echo '</table>';   
  18. } 
  19. //处理表单提交 
  20. $action = (isset($_POST['action'])) ? $_POST['action'] :'' ; 
  21. $file = (isset($_POST['file'])) ? $_POST['file'] :'' ; 
  22. if($file != '') 
  23.   $file = $file.'/'; 
  24. $message_true = ''; 
  25. $message_false = ''; 
  26. switch($action){ 
  27.  case 'upload' :   
  28.  chmod(FILE_UPLOAD_DIR,0777);   
  29.  for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){    
  30.   if( $_FILES['file_'.$nb]['size'] >= 10 ){  
  31.   if ($_FILES['file_'.$nb]['size'] <= MAX_SIZE_FILE_UPLOAD ){  
  32.    if (!in_array(ereg_replace('^[[:alnum:]]([-_.]?[[:alnum:]])*.' ,'.', $_FILES['file_'.$nb]['name'] ) , $array_extention_interdite) ){  
  33.            if($_POST['file_name_'.$nb] !='') 
  34.              $file_name_final = $_POST['file_name_'.$nb].$extension ; 
  35.            else 
  36.              $file_name_final = $_FILES['file_'.$nb]['name'] ; 
  37.            //修改文件名 
  38.            $file_name_final = strtr($file_name_final, 'aaaaaa', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');  
  39.            $file_name_final = preg_replace('/([^.a-z0-1]+)/i', '_', $file_name_final );  
  40.              
  41.            $_FILES['file_'.$nb]['name'] = $file_name_final;   
  42.            //开始上传 
  43.            move_uploaded_file( $_FILES['file_'.$nb]['tmp_name'] , FILE_UPLOAD_DIR . $file . $file_name_final ); 
  44.          
  45.            $message_true .= '文件上传成功 : '.$_FILES['file_'.$nb]['name'] .'<br>';  
  46.         }else 
  47.            $message_false .= '文件上传失败 : '.$_FILES['file_'.$nb]['name'] .' <br>'; 
  48.       }else //www.phpfensi.com 
  49.         $message_false .= '文件最大尺寸不能超过'.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : "'.$_FILES['file_'.$nb]['tmp_name'].'" <br>'; 
  50.     } 
  51.   }//end for 
  52.  break; 
  53. } 
  54. ?> 

二、HTML代码如下:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  4. </head> 
  5. <title>PHP文件上传</title> 
  6. <style type="text/css" rel="stylesheet" /> 
  7. .border{ 
  8.  background-color:#000000 
  9. } 
  10. .box{ 
  11.  background-color:#f8f8f9; 
  12. } 
  13. .text{  
  14.  color:#000000; 
  15.  font-family: "宋体"; 
  16.  font-size: 12px; 
  17.  font-weight:bold 
  18. } 
  19. input, select{ 
  20.  font-size: 12px; 
  21. } 
  22. body{ 
  23.   margin: 0; 
  24. } 
  25. </style> 
  26. <body> 
  27.  <!-- 文件上传表单,enctype属性是必须的 --> 
  28.  <form name="form" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'] ; ?>"> 
  29.  <input type="hidden" name="action" value="upload"> 
  30.  <table border="0" cellspacing="1" cellpadding="0" align="center" class="border"> 
  31.   <tr>  
  32.   <td> 
  33.    <?php 
  34.    if($message_true != '') 
  35.     func_message($message_true, true); 
  36.    if($message_false != '') 
  37.     func_message($message_false, false); 
  38.    ?> 
  39.    <table width="100%" border="0" cellspacing="5" cellpadding="2" align="center" class="box"> 
  40.    <?php 
  41.     for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){  
  42.    ?> 
  43.    <tr class="text">  
  44.     <td>上传文件: <?php echo $nb; ?></td>  
  45.     <td><input type="file" name="file_<?php echo $nb; ?>"></td> 
  46.     <td>新文件名(包括扩展名):<?php echo $nb; ?> </td> 
  47.     <td><input type="text" name="file_name_<?php echo $nb; ?>"></td> 
  48.    </tr> 
  49.    <?php } ?> 
  50.    <tr>  
  51.     <td colspan="2" align="right" class="text"> 
  52.     上传目的地址:<?php echo FILE_UPLOAD_DIR ;?> 
  53.      <select name="file">     
  54.      <option value=""></option> 
  55.      <?php 
  56.      $repertoire = opendir(FILE_UPLOAD_DIR);  
  57.      while( $file = readdir($repertoire) ) {  
  58.       $file = str_replace('.','',$file); 
  59.       if( is_dir($file)) {  
  60.      ?>  
  61.      <option value="<?php echo $file; ?>"> <?php echo $file; ?>/</option> 
  62.      <?php 
  63.        }  
  64.      }  
  65.      closedir($repertoire);  
  66.      ?> 
  67.     </select> 
  68.     </td> 
  69.     <td colspan="2" align="right"><input type="submit" value="可同时上传<?php echo $nb-1; ?> 个文件 "></td> 
  70.    </tr> 
  71.    </table> 
  72.   </td> 
  73.   </tr> 
  74.  </table> 
  75.  </form>   
  76. </body> 
  77. </html> 

希望本文所述对大家的PHP程序设计有所帮助。

Tags: PHP视频文件上传

分享到: