A complete example of uploading video files by PHP

  • 2021-07-16 02:04:19
  • OfStack

In this paper, the function of uploading video files is realized in the form of a complete example. Although it is a relatively basic application, it still has a certain reference value. Share it with you for your reference. The specific methods are as follows:

First of all, video is also a file for PHP. Using this reason, we can upload PHP video files in a similar way to upload ordinary files. The difference is that the video file is as large as 1 and takes a long time to upload, while the php configuration file also has a limit on the size of the uploaded file.
If you need to better use PHP to upload files, it is recommended to use FTP upload method.

1. The PHP processing code is as follows:


<?php
// Set the maximum allowed 10 Upload files at the same time 
define('MUILTI_FILE_UPLOAD', '10');
// Set the file size to no more than 5MB
define('MAX_SIZE_FILE_UPLOAD', '500000' );
// Set the storage directory for uploaded files 
define('FILE_UPLOAD_DIR', '/fileUploads');
// File extensions allowed for uploading 
$array_extention_interdite = array( '.flv' , '.wmv' , '.rmvb' , '.php' , '.php3' , '.php4' , '.exe' , '.msi' , '.htaccess' , '.gz' );
// Common function for displaying information 
function func_message($message='', $ok=''){
 echo '<table width="100%" cellspacing="0" cellpadding="0" border="0">';
 if($ok == true)
 echo '<tr><td width="50%"> '.$message.'</td></tr>' ;
 else
 echo '<tr><td width="50%"> '.$message.'</td></tr>';
 echo '</table>';  
}
// Processing Form Submissions 
$action = (isset($_POST['action'])) ? $_POST['action'] :'' ;
$file = (isset($_POST['file'])) ? $_POST['file'] :'' ;
if($file != '')
  $file = $file.'/';
$message_true = '';
$message_false = '';
switch($action){
 case 'upload' :  
 chmod(FILE_UPLOAD_DIR,0777);  
 for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){   
  if( $_FILES['file_'.$nb]['size'] >= 10 ){ 
  if ($_FILES['file_'.$nb]['size'] <= MAX_SIZE_FILE_UPLOAD ){ 
   if (!in_array(ereg_replace('^[[:alnum:]]([-_.]?[[:alnum:]])*.' ,'.', $_FILES['file_'.$nb]['name'] ) , $array_extention_interdite) ){ 
           if($_POST['file_name_'.$nb] !='')
             $file_name_final = $_POST['file_name_'.$nb].$extension ;
           else
             $file_name_final = $_FILES['file_'.$nb]['name'] ;
           // Modify file name 
           $file_name_final = strtr($file_name_final, 'aaaaaa', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy'); 
           $file_name_final = preg_replace('/([^.a-z0-1]+)/i', '_', $file_name_final ); 
           
           $_FILES['file_'.$nb]['name'] = $file_name_final;  
           // Start uploading 
           move_uploaded_file( $_FILES['file_'.$nb]['tmp_name'] , FILE_UPLOAD_DIR . $file . $file_name_final );
       
           $message_true .= ' File uploaded successfully  : '.$_FILES['file_'.$nb]['name'] .'<br>'; 
        }else
           $message_false .= ' File upload failed  : '.$_FILES['file_'.$nb]['name'] .' <br>';
      }else
        $message_false .= ' The maximum file size cannot exceed '.MAX_SIZE_FILE_UPLOAD/1000 . 'KB : "'.$_FILES['file_'.$nb]['tmp_name'].'" <br>';
    }
  }//end for
 break;
}
?>

2. The HTML code is as follows:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<title>PHP File upload </title>
<style type="text/css" rel="stylesheet" />
.border{
 background-color:#000000
}
.box{
 background-color:#f8f8f9;
}
.text{ 
 color:#000000;
 font-family: " Song Style ";
 font-size: 12px;
 font-weight:bold
}
input, select{
 font-size: 12px;
}
body{
  margin: 0;
}
</style>
<body>
 <!--  File upload form, enctype Attribute is required  -->
 <form name="form" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'] ; ?>">
 <input type="hidden" name="action" value="upload">
 <table border="0" cellspacing="1" cellpadding="0" align="center" class="border">
  <tr> 
  <td>
   <?php
   if($message_true != '')
    func_message($message_true, true);
   if($message_false != '')
    func_message($message_false, false);
   ?>
   <table width="100%" border="0" cellspacing="5" cellpadding="2" align="center" class="box">
   <?php 
    for($nb = 1 ; $nb <= MUILTI_FILE_UPLOAD ; $nb ++ ){ 
   ?>
   <tr class="text"> 
    <td> Upload file:  <?php echo $nb; ?></td> 
    <td><input type="file" name="file_<?php echo $nb; ?>"></td>
    <td> New file name (including extension): <?php echo $nb; ?> </td>
    <td><input type="text" name="file_name_<?php echo $nb; ?>"></td>
   </tr>
   <?php } ?>
   <tr> 
    <td colspan="2" align="right" class="text">
     Upload destination address: <?php echo FILE_UPLOAD_DIR ;?>
     <select name="file">    
     <option value=""></option>
     <?php 
     $repertoire = opendir(FILE_UPLOAD_DIR); 
     while( $file = readdir($repertoire) ) { 
      $file = str_replace('.','',$file);
      if( is_dir($file)) { 
     ?> 
     <option value="<?php echo $file; ?>"> <?php echo $file; ?>/</option>
     <?php 
       } 
     } 
     closedir($repertoire); 
     ?>
    </select>
    </td>
    <td colspan="2" align="right"><input type="submit" value=" Can be uploaded at the same time <?php echo $nb-1; ?>  Files  "></td>
   </tr>
   </table>
  </td>
  </tr>
 </table>
 </form>  
</body>
</html>

I hope this article is helpful to everyone's PHP programming.


Related articles: