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);
    }

Dani AI

Generated

Quick diagnosis for : the upload loop is the immediate bug, and on WAMP larger videos often fail because of PHP configuration (php.ini) rather than CodeIgniter itself.

The loop problem: count($file) returns the number of keys in the file-array (name/type/tmp_name/error/size), not the number of uploaded files. That, plus the -1, will skip files or iterate wrong number of times. Use the file-name array for the count or iterate by index. Example pattern (safe for PHP 5.x):

$files = $_FILES['files'];
foreach ($files['name'] as $i => $name) {
    if ($files['error'][$i] !== UPLOAD_ERR_OK) { continue; }
    $tmp = $files['tmp_name'][$i];
    if (!is_uploaded_file($tmp)) { continue; }
    $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
    $dir = __DIR__ . '/uploads/' . $data['userid'] . '/';
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    $dest = $dir . time() . '_' . bin2hex(openssl_random_pseudo_bytes(6)) . '.' . $ext;
    if (!move_uploaded_file($tmp, $dest)) {
        // log the failure
    }
}

Checklist for WAMP / Windows (common causes):

  • Check php.ini (use phpinfo()) — adjust upload_max_filesize and post_max_size (both must exceed expected video size), increase max_execution_time, max_input_time, and memory_limit as needed, and confirm file_uploads = On. Restart Apache after edits.
  • Ensure upload_tmp_dir exists and is writable; inspect $_FILES and the error codes (0 = OK, 1 = UPLOAD_ERR_INI_SIZE, 2 = UPLOAD_ERR_FORM_SIZE, 3 = PARTIAL, 4 = NO_FILE, 6 = NO_TMP_DIR, 7 = CANT_WRITE, 8 = EXTENSION) to pinpoint failures.
  • When creating upload folders from PHP, use mkdir(..., 0777, true) to create parent folders and check permissions.

Notes on other replies: is right that ffmpeg is only needed for transcoding, not uploading; ’s example shows the basic idea but avoid deprecated mysql_* in new code—use mysqli or PDO and validate MIME/type with Fileinfo rather than trusting the extension.

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 ?

hi

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

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.