Codeigniter doesn't upload video files - php

I have a problem with the function for upload video and images. I can upload images but not video I don' know why. In the and more code for controller and form.
Code in user_models php:
function newp($title,$post,$image,$video,$cat){
$id=$this->session->userdata('userid');
if($video!="")
{
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
'videourl'=>$video
);
}
Image:
else if($image!=""){
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
'imageurl'=>$image
);
}else{
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
);}
$this->db->insert('forum',$data);
}
}
Form:
<form method="post" enctype="multipart/form-data" action="<?php echo base_url();?>index.php/forum/newpk">
<div style="float:left;width: 95%;margin-top:5px;margin-left: 15px">
<h3> Add A Post</h3>
<div class="list-group" >
<input type="text" required name="title" id="title" class="form-control" placeholder="Title of the Post">
</div>
<div class="list-group">
<textarea name="post" class="form-control" style="min-height: 150px;" placeholder="Post">
</textarea>
</div>
<div class="list-group">
<span style="float:left;display:inline-block;"><p style="font-size:15px">Video File</p> <input type="file" name="video" accept="video/*" /></span>
<p style="font-size:15px;margin-left:10%;width: 32px;display:inline-block;">OR</p>
Image File
</div>
<div class="listgroup">
To which category it belongs
<select name="cat"><?php foreach($cato as $cat){ ?><option name="cat" value="<?php echo $cat->categoryid;?>"><?php echo $cat->categoryname;?></option><?php }?></select>
</div>
<div class="list-group" style="margin-top: 70px;margin-left: 35%;">
<button type="submit" style="width: 159px;height: 47px;line-height: 2.3;font-size: 17px;" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
Controller:
public function newpk(){
if($this->session->userdata('userid')){
$title=$this->security->xss_clean($this->input->post('title'));
$post=$this->security->xss_clean($this->input->post('post'));
$cat=$this->security->xss_clean($this->input->post('cat'));
if($cat=="" || $post=="" || $title=="")
{
echo "Required field cannot be empty";
die();
}
$themepicid="";
$this->load->model('forum_model');
Video part:
if(isset($_FILES['video']) && $_FILES['video']['size']>0){
$pktid=$this->idGenerator();
$pathh='assets/video/';
$themepicid=$this->session->userdata('userid');
$type= pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
$pic=$themepicid;
$themepicid=$themepicid.$pktid.".".$type;
move_uploaded_file($_FILES['video']['tmp_name'], $pathh.$themepicid);
$image="";
$this->forum_model->newp($title,$post,$image,$themepicid,$cat); // How the forum likes
Image part:
}
else if(isset($_FILES['image']) && $_FILES['image']['size']>0){
$pathh='assets/images/';
$themepicid=$this->session->userdata('userid');
$type= pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$pktid=$this->idGenerator();
$pic=$themepicid;
$themepicid=$themepicid.$pktid.".".$type;
move_uploaded_file($_FILES['image']['tmp_name'], $pathh.$themepicid);
$image="";
$this->forum_model->newp($title,$post,$themepicid,$image,$cat);
}
else{
$image="";
$this->forum_model->newp($title,$post,$image,$image,$cat);
}
redirect('index.php/forum','refresh');
}
else{
redirect('index.php/user','refresh');
}
}
}

Maybe your video size larger than max size upload config in php.ini
You can use upload library from CI, it's easy to use and help you prevent error
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
If you using upload lib of CI, dont forget config allowed types

Related

Photo Gallery: Adding Photos

Ok so I’m working on a photo gallery project and I’m trying to add a photo. I have a page that allows the user to browse for a picture and another input field for adding a title for their picture. The added picture path and title should be added to a xml file where the images are being pulled from.
Also if it helps, I'm using Materialize css. I’m not sure why it’s not working but this is what I currently have:
Form
<form action="../models/addPicture.php" method="POST" enctype="multipart/form-data">
<div class="row center">
<h5>Choose a Photo to Upload</h5>
</div>
<div class="file-field input-field">
<div class="btn-large black">
<span>Browse</span>
<input type="file" />
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Upload file" name="picFile" />
</div>
</div>
<div class="row">
<div class="input-field">
<input type="text" name="picTitle" class="validate" placeholder="Enter a title for your picture.">
</div>
</div>
<div class="row center">
<button class="btn-large black" type="submit">Add Pic!</button>
</div>
Method:
<?php
if (($_FILES['picFile']['name']!="")){
$target_dir = "../images/";
$file = $_FILES['picFile']['name'];
$path = pathinfo($file);
$filename = $path['basename'];
$ext = $path['extension'];
$temp_name = $_FILES['picFile']['temp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
if (file_exists($path_filename_ext))
{
$message = "Added Picture Failed please try again.";
$color = "red-text";
header("Location: ../index.php?Message=".$message."&color=".$color);
}
else
{
move_uploaded_file($temp_name,$path_filename_ext);
$message = "Added Picture Successfully. Please Click Reset to View.";
$color = "green-text";
header("Location: ../index.php?Message=".$message."&color=".$color);
}
}
else
{
$message = "Please enter a picture to submit.";
$color = "red-text";
header("Location: ../index.php?Message=".$message."&color=".$color);
}
?>
I have found a small mistake in your code at line no. 9.
$temp_name = $_FILES['picFile']['temp_name'];
Change the ['temp_name]; to ['tmp_name];
Use this code.
$temp_name = $_FILES['picFile']['tmp_name'];

i tried to upload a file to upload folder but getting failure message

my directory is looks like this
->source files
--css
--upload
add_file.php
upload.php
my code is as follows
upload.php
<form role="form" method="post" enctype="multipart/form-data" action="add_file.php">
<div class="form-group">
<label for="filecaption">
Caption :
</label>
<input type="text" name="f_caption" class="form-control"/>
</div>
<div class="form-group">
<label for="Choose a File">
Caption :
</label>
<input type="file" name="uploaded_file" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" name="upload" value="Upload" class="form-control btn btn-warning"/>
</div>
</form>
add_file.php
<?php
if(isset($_FILES['uploaded_file']))
{
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($f_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is from the add.php.
Try this
<?php
if(isset($_FILES['uploaded_file'])) {
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($temp_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is that you were moving from the f_name instead of the temp_location.

Upload video or picture with php

I am trying to upload video or pictures to my server using php. However I keep getting an error it seems like the file name is empty and I don't understand why, the code keeps executing the script showing an error and nothing is uploaded to my folder
Here is my code:
<div class="container" id="step2-video" style="display:none; margin-top:100px" >
<div class="row">
<div class="col-lg-4 col-md-offset-4">
<form id="video_uploader" action="ad_creator.php" method="post" enctype="multipart/form-data">
<div id="btn-upload-video">
<span class="file-wrapper btn ad-choice">
<input type="file" id="video-file-upload" name="video-file-upload" accept="video/mp4"/>
<span class="button">Choose a video</span>
</span>
</div>
<div id="video-preview" style="display:none" >
<span class="file-wrapper btn ad-choice">
<?php
?>
<input type="submit" name="upload_video" value="Submit" style="margin-left:auto" />
<span class="button">Comfirm</span>
</span>
</div>
</form>
</div>
</div>
</div>
<!--
Step 2 for pictures of the editor
-->
<div class="container" id="step2-picture" style="display:none; margin-top:100px" >
<div class="row">
<div class="col-lg-4 col-md-offset-4">
<form id="picture_uploader" action="ad_creator.php" method="post" enctype="multipart/form-data">
<div id="btn-upload-picture">
<span class="file-wrapper btn ad-choice">
<input type="file" id="picture-file-upload" name="picture-file-upload" accept="image/gif, image/jpeg, image/jpg, image/png"/>
<span class="button">Choose a picture</span>
</span>
</div>
<div id="picture-preview" style="display:none" >
<div id="imagePreview"></div>
<br>
<span class="file-wrapper btn ad-choice">
<input type="submit" name="upload_picture" value="Submit" style="margin-left:auto" />
<span class="button">Comfirm</span>
</span>
</div>
</form>
</div>
</div>
</div>
<?php
if (isset($_POST['upload_picture'])){
$image_name = isset ($_FILES['image']['name']) ? $_FILES['image']['name'] : '' ;
$image_type = isset($_FILES['image']['type']) ? $_FILES['image']['type'] : '';
$image_size = isset($_FILES['image']['size']) ? $_FILES['image']['size'] : '';
$image_tmp_name = isset ($_FILES['image']['tmp_name']) ? $_FILES['image']['tmp_name'] : '';
if($image_name == ''){
echo "<script> alert ('Error occured for picture upload')</script>";
exit();
} else {
move_uploaded_file ($image_tmp_name, "uploads/picture/$image_name");
echo "<script> alert ('sucess for puciture upload')</script>";
}
}
if (isset($_POST['upload_video'])){
$video_name = isset ($_FILES['video']['name']) ? $_FILES['video']['name'] : '' ;
$video_type = isset($_FILES['video']['type']) ? $_FILES['video']['type'] : '';
$video_size = isset($_FILES['video']['size']) ? $_FILES['video']['size'] : '';
$video_tmp_name = isset ($_FILES['video']['tmp_name']) ? $_FILES['video']['tmp_name'] : '';
if($video_name == ''){
echo "<script> alert ('Error occured for video upload')</script>";
exit();
} else {
move_uploaded_file ($video_tmp_name, "uploads/video/$video_name");
echo "<script> alert ('Sucess for video upload')</script>";
}
}
?>
SOLUTION
From Rescaltt, thank you.
I was checking for the wrong file input,
I was checking for $_FILES['image']['name'] instead of $_FILES['picture-file-upload']['name']
Try using a function to do both. Things to note, you should probably do a couple checks and balances:
Sanitize the file name for good measure.
Include your document root for an absolute path or make sure you have the proper relative path.
You may want to check that the upload folder exists and make it if not.
Check success on upload if(move_uploaded_file(...etc.
<?php
function MyUploader($uploadtype = 'picture',$savedir = '/uploads/')
{
$name = (isset($_FILES[$uploadtype.'-file-upload']['name']))? $_FILES[$uploadtype.'-file-upload']['name'] : '' ;
$type = (isset($_FILES[$uploadtype.'-file-upload']['type']))? $_FILES[$uploadtype.'-file-upload']['type'] : '';
$size = (isset($_FILES[$uploadtype.'-file-upload']['size']))? $_FILES[$uploadtype.'-file-upload']['size'] : '';
$tmp_name = (isset($_FILES[$uploadtype.'-file-upload']['tmp_name']))? $_FILES[$uploadtype.'-file-upload']['tmp_name'] : '';
if(empty($name))
return "<script> alert ('Invalid file name.')</script>";
else {
// Make sure you include your ROOT DIRECTORY / correct local path
$upload_dir = $_SERVER['DOCUMENT_ROOT'].$savedir.$uploadtype."/";
// May want to make the directory if not exists
if(!is_dir($upload_dir))
mkdir($upload_dir,0755,true);
// You should check that it worked with if(move_upload_file(...etc
if(move_uploaded_file($tmp_name, $upload_dir.$name))
return "<script> alert ('$uploadtype uploaded successfully')</script>";
else
return "<script> alert ('Could not save file to server.')</script>";
}
}
if(isset($_POST['upload_picture']))
echo MyUploader('picture');
if(isset($_POST['upload_video']))
echo MyUploader('video'); ?>

Upload image in directory

I would try to write a script where users make their own questions. I was able to safely carry the fields such as title, description and some other details. But I want to make users upload images (also for the moment), unfortunately I can not, I ask therefore help to you.
Paste the code:
<?php require_once 'app/init.php';
if (isset($_POST['submit']))
{
$validator = Validator::make(
array(
'title' => $_POST['title'],
'question' => $_POST['question'],
),
array(
'title' => 'required',
'question' => 'required',
)
);
if ($validator->fails())
{
$errors = $validator->messages();
}
else
{
DB::table('question')->insert(
array(
'q_title' => escape($_POST['title']),
'q_desc' => escape($_POST['question']),
'page_title' => escape($_POST['title']),
'h_image' => escape($_POST['filename']),
'user_id' => escape($_POST['userid']),
'user_name' => escape($_POST['username'])
)
);
return redirect_to('question.php');
}
}
?>
<?php echo View::make('header')->render() ?>
<div class="row">
<div class="col-md-8">
<h3 class="page-header">Question</h3>
<!-- Display errors, if are any -->
<?php if (isset($errors)): ?>
<ul>
<?php foreach ($messages->all('<li>:message</li>') as $message) {
echo $message;
} ?>
</ul>
<?php endif ?>
<!-- Form -->
<?php if (Auth::check()): ?>
<form action="" method="POST" enctype="multipart/form-data">
<label for="title">Title</label>
<div class="input-group input-group-lg">
<input type="text" name="title" class="form-control" placeholder="Title" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2">?</span>
</div><br />
<label for="question">Description</label>
<textarea class="form-control" name="question" rows="4" cols="10" placeholder="Description..."></textarea><br />
<label for="question">Image</label>
<input type="file" name="filename" id="image" size="40">
<input type="hidden" name="userid" value="<?php echo Auth::user()->id ?>">
<input type="hidden" name="username" value="<?php echo Auth::user()->display_name ?>"><br />
<button type="submit" name="submit" class="btn btn-md btn-success">Help me!</button>
</form>
<?php else: ?>
<p>
<!-- <?php _e('comments.logged_in', array('attrs' => 'href="login.php"')) ?> -->
<?php _e('comments.logged_in', array('attrs' => 'href="#" class="login-modal" data-target="#loginModal"')) ?>
</p>
<?php endif ?>
</div>
</div>
<?php echo View::make('footer')->render() ?>
I wish the pictures were included in a directory (or if you advise me, even in the database, what is better?).
I hope for your help, thank you.
Simple Example:
HTML
<form action='upload.php' method='post' enctype='multipart/form-data'>
<input name='filename' type='file' />
<input name='btnSubmit' type='submit' />
</form>
PHP
<?php
$filename = $_FILES["filename"]["name"];
$tmpFilename = $_FILES["filename"]["tmp_name"];
$path = "path/to/upload/" . $filename;
if(is_uploaded_file($tmpFilename)){ // check if file is uploaded
if(move_uploaded_file($tmpFilename, $path)){ // now move the uploaded file to path (directory)
echo "File uploaded!";
}
}
?>
When you submit the form,
the $_FILES global variable is created with the infomation on the file!
You can see that's in that variable with this function :
<?php
print_r($_FILES); // this will echo any array
?>
First you should submit the post (normal data & file data)
$data = $_POST['data'];
$files = $_FILES['file'];
var_dump the $files, and you will see a bunch of data, where you can do a lot with in statements.
Then you should check if there is actually a file uploaded:
(file_exists($_FILES['file']['tmp_name'])) || (is_uploaded_file($_FILES['file']['tmp_name']
If a file is uploaded, you could check if there is an existing directory to store the files in:
$directory = 'your path here';
$filename = $directory.'/'. $_FILES['file']['name'];
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
move_uploaded_file($_FILES['file']['tmp_name'], $filename);
I hope this helped you a little...

php and jquery image upload button after load

I'm preparing the script facebook style wall post. Using Php and Jquery. But I have a problem. I wrote a function to upload photos. Upload a picture from your computer picture is selected when the button is clicked. and passage of the picture preview image (image loading) part opens. Photos button after installing upload pictures come back again.
The problem is that (loading image) warning does not disappear. Also (loading image) warning not lost are not coming back button to upload pictures.
Thanks in advance for your help.
jQuery
$('#photoimg').die('click').live('change', function()
{
var values=$("#uploadvalues").val();
$("#previeww").html('<img src="wall_icons/loader.gif"/>');
$("#imageform").ajaxForm({target: '#preview',
beforeSubmit:function(){
$("#imageloadstatus").show();
$("#imageloadbutton").hide();
},
success:function(){
$("#imageloadstatus").hide();
$("#imageloadbutton").show();
},
error:function(){
$("#imageloadstatus").hide();
$("#imageloadbutton").show();
} }).submit();
var X=$('.preview').attr('id');
var Z= X+','+values;
if(Z!='undefined,')
{
$("#uploadvalues").val(Z);
}
});
HTML
<div class="tb-content">
<div class="ct-tab1">
<textarea name="update" id="update" placeholder="Ne düşünüyorsun?" class="contenttextarea" ></textarea>
<div id="button_hide">
<div class="secretdiv">
<div id="webcam_container" class='border'>
<div id="webcam" >
</div>
<div id="webcam_preview">
</div>
<div id='webcam_status'></div>
<div id='webcam_takesnap'>
<input type="button" value=" Resimçek " onclick="return takeSnap();" class="camclick resimcekbutton"/>
<input type="hidden" id="webcam_count" />
</div>
</div>
<div id="imageupload" class="border">
<form id="imageform" method="post" enctype="multipart/form-data" action='message_image_ajax.php'>
<div id='preview'>
</div>
<div id='imageloadstatus'>
<img src='<?php echo $base_url;?>wall_icons/ajaxloader.gif'/> Resim yükleniyor lütfen bekleyin....
</div>
<div id='imageloadbutton'>
<input type="file" name="photoimg" id="photoimg" class="dosyasec" />
</div>
<input type='hidden' id='uploadvalues' />
</form>
</div>
</div>
<div class="videovecamerabutonlari">
<div class="imgbutonu"><img src="wall_icons/videobutonu.png" border="0" /></div>
<div class="videobutonu"><img src="wall_icons/camerabutton.png" border="0" /></div>
</div>
<div class="shr">
<input type="submit" id="update_button" class="update_button" value="Paylaş" />
<input type="submit" id="cancel" value="İptal" />
</div>
</div>
</div>
<div id='flashmessage'>
<div id="flash" align="left" ></div>
</div>
Php
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
$ext = getExtension($name);
if(in_array($ext,$valid_formats))
{
if($size<(10024*10024))
{
$actual_image_name = time().$uid.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$data=$Wall->Image_Upload($uid,$actual_image_name);
$newdata=$Wall->Get_Upload_Image($uid,$actual_image_name);
if($newdata)
{
echo '<img src="'.$path.$actual_image_name.'" class="preview" id="'.$newdata['id'].'"/>';
}
}
else
{
echo "Fail upload folder with read access.";
}
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format.";
}
else
echo "Please select image..!";
exit;
}
?>

Categories