Hey guys,

I'm having the most toughest time of my life to figure out a way to upload video files using PHP.
I am able to upload images but not video files or zip files. I haven't put any file extension check right now.
Here is my code, will you be able to find where i went wrong?

I am using CodeIgniter. Is there anything i need to do in CI to make this happen?

<form action="<?php echo current_url()?>" method="POST" enctype="multipart/form-data">
    <input name="files[]" type="file" multiple required>
    <input type="submit" name="form_submit" value="Upload Selected Items">
</form>



    public function add(){

        $data=$this->common();
        $data['title']="Upload New Media";
        $data['subtitle']="Upload Media & Add Info";
        if(isset($_POST['form_submit'])){
            var_dump($_FILES);
            foreach($_FILES as $file){
                for($i=0;$i<count($file)-1;$i++){
                    if(isset($file['name'][$i])){
                    /* for debugging */
                        echo $file['name'][$i]."<br>";
                        echo $file['type'][$i]."<br>";
                        echo $file['tmp_name'][$i]."<br>";
                        echo $file['error'][$i]."<br>";
                        echo $file['size'][$i]."<br><br>";

                        if($file['error'][$i]==0)
                        {

                        $tempFile = $file['tmp_name'][$i];
                        $fileName = $file['name'][$i];
                        $ext=explode(".",$fileName);
                        $targetPath = getcwd().'/uploads/'.$data['userid'].'/';
                        if(!file_exists($targetPath)) {mkdir($targetPath);}
                        $targetFile = $targetPath.substr(uniqid(rand()),0,90).'.'.end($ext);
                        if(move_uploaded_file($tempFile, $targetFile)==true){
                            echo "done!";
                        }

                        }
                    }
                }
            }
        }

        $this->load->view('admin/upload',$data);
    }

Recommended Answers

All 10 Replies

YOur code shall be like this are you using windows you need to download FFMpeg.exe because this code will work in Linux.If you are using Linux server, Use print phpinfo() function to check whether your hosting server has FFmpeg installed or you need it to be installed.

$file    = 'video_file';
$config['upload_path']  = './video_folder/';
$config['allowed_types']  = 'mov|mpeg|mp3|avi';
$config['max_size']  = '50000';
$config['max_width']    = '';
$config['max_height']    = '';

$this->upload->initialize($config);
$this->load->library('upload', $config);

if(!$this->upload->do_upload($file))
{
 // If there is any error
 $err_msgs .= 'Error in Uploading video '.$this->upload->display_errors().'<br />';
}
else
{
 $data=array('upload_data' => $this->upload->data());
 $video_path = $data['upload_data']['file_name'];
  $directory_path   = $data['upload_data']['file_path'];
 $directory_path_full      = $data['upload_data']['full_path'];
 $file_name   = $data['upload_data']['raw_name'];

 // ffmpeg command to convert video

 exec("ffmpeg -i ".$directory_path_full." ".$directory_path.$file_name.".flv"); 

// $file_name is same file name that is being uploaded but you can give your custom video name after converting So use something like myfile.flv.

 /// In the end update video name in DB 
 $array = array(
  'video' => $file_name.'.'.'flv',
  );
 $this->db->set($array);
 $this->db->where('id',$id); // Table where you put video name
 $query = $this->db->update('user_videos');  
}

Hey i forgot to mention. I am using windows 64-bit and running the scripts on WAMP with PHP 5.5.12!

First tell me what is your purpouse .Is the purpouse learning to write a code or you have any project to accompalish.Or you just want to see video on your site.

Just to learn writing code on Codeigniter! I was able to upload images but was wondering why am not able to upload videos just like i upload images. I was able to upload videos on wamp long time back! Thought i had gone wrong somewhere with my code!

Have you looked upon the above code ?

<?php
mysql_connect("localhost","root","");
mysql_select_db("db_daxa1");
if(isset($_POST['upload']))
{
    //echo "hi..";
    $file=$_FILES['video_file']['name'];
    $dest="video/$file";
    $src=$_FILES['video_file']['tmp_name'];
    move_uploaded_file($src,$dest);
    $insert="insert into tbl_video values('','$dest','1')";
    mysql_query($insert);
}

?>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
  $(document).ready(function(){

    $("#upfile1").click(function () {

    $("#file1").trigger('click');
});
});
function msg()
{
    //alert("Thanks..");
}
</script>
</head>
<body onmouseleave="msg();" onmouseover="msg1()">
<center>
<form method="post" enctype="multipart/form-data">
<!--<input type="file" name="video_file"/>-->
<img src="images.jpeg" id="upfile1" style="cursor:pointer" height="30" width="120"/>
<input type="file" id="file1"  name="video_file" style="display:none" />
<input type="submit" name="upload" value="Submit"/>


</form></center>
<?php
$select_video=mysql_query("select * from tbl_video");
while($fetch_video=mysql_fetch_array($select_video))
{
?>
    <embed src="<?php echo $fetch_video['video'];?>" autostart="false" volume="true"/>

<?php //echo $fetch_video['video'];?>
<?php } ?>
</body>
</html>

use this code that definately help you

@Hiren2927 mysql is deprecated.It will give an error we shall use mysqli or PDO

u can make changes in above code code is perfect in xampp u just have make some changes like mysqli...etc than u,ll able to upload video try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.