PHP : upload video to youtube using link[direct link] using curl - codeigniter

Video is uploaded to youtube if I use the form below. I am able to fetch the response from youtube after login and adopt the response in the form below.
<form action="<?php echo( $response->url ); ?>?nexturl=<?php echo( urlencode( $nexturl ) ); ?>" method="post" enctype="multipart/form-data">
<p class="block">
<label>Upload Video</label>
<span class="youtube-input">
<input id="file" type="file" name="file" />
</span>
</p>
<input type="hidden" name="token" value="<?php echo( $response->token ); ?>"/>
<input type="submit" value="Upload Video" />
</form>
But If try to do the same with curl as below then it doesnt work .. Any suggestions ??
$file_name_with_full_path = $video_path; //$video_path is like /path/to/abc.mp4
$post = array('file'=>'#'.$file_name_with_full_path, 'token' => $response->token,);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$response->url."?nexturl=".urlencode($nexturl));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
die();
echo result given bool(false). Please tell whats wrong in this . Thanks in advance.

Related

What is the cause of this wrong image upload error message in my Codeigniter 3 application?

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form, which has an image upload field. In the controller, the update() method contains the logic for the image upload action:
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '100';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
$data['uerrors'] = $uerrors;
}
if(!$this->form_validation->run() || !empty($uerrors))
{
print_r($data['uerrors']);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The (surprising) problem I have is that, even though I am uploading an image, print_r($data['uerrors']); returns You did not select a file to upload. in the browser.
In the view, the same error is returned:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo set_value('first_name', $author->first_name); ?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo set_value('last_name', $author->last_name); ?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo set_value('bio', $author->bio); ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<input type="hidden" name="avatar" id="avatar" value="<?php echo $author->avatar; ?>">
<label for="avatar">Upload avatar</label>
<div class="form-group">
<input type="file" name="userfile" id="uavatar" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
<div class="form-group">
<div class="w-50 pull-left pr-1">
<input type="submit" value="Update" class="btn btn-block btn-md btn-success">
</div>
<div class="w-50 pull-right pl-1">
Cancel
</div>
</div>
<?php echo form_close(); ?>
The error message I was expecting, considering that the image I was trying to upload is larger then the specified limit (100KB) is: The file you are attempting to upload is larger than the permitted size.
What am I doing wrong?
Try this
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
<div class="form-group">
<input type="file" name="userfile" id="userfile" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
Try to change your form opening, from :
<?php echo form_open(base_url('dashboard/users/update')); ?>
to
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
To change the form encoding type from text/plain to multipart/form-data to support image data upload. Here is the difference between each encoding type.

recaptcha v2 not getting the "success"

I’m having trouble in the implementation of recaptcha v2 chekbox. I first implemented in my form and it only works in local server. After failing in many attempts I search for some code and I’m using it raw from the tutorial and keep getting error.
Url: https://afrumex.com/google_reCAPTCHA/prueba.html
Hope someone could help. Thanks!!
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ajax Contact Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<!-- ajax contact form -->
<section style="margin-top: 50px;">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<h5 class="card-header">Ajax Contact Form</h5>
<div class="card-body">
<form class="contact__form" method="post" action="mail.php">
<!-- form message -->
<div class="row">
<div class="col-12">
<div class="contact__msg" style="display: none">
<p>Your message was sent successfully.</p>
</div>
</div>
</div>
<!-- end message -->
<!-- form element -->
<div class="row">
<div class="col-md-6 form-group">
<input name="name" type="text" class="form-control" placeholder="Name" required>
</div>
<div class="col-md-6 form-group">
<input name="email" type="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-md-6 form-group">
<input name="phone" type="text" class="form-control" placeholder="Phone" required>
</div>
<div class="col-md-6 form-group">
<input name="subject" type="text" class="form-control" placeholder="Subject" required>
</div>
<div class="col-12 form-group">
<textarea name="message" class="form-control" rows="3" placeholder="Message" required></textarea>
</div>
<div class="col-12 form-group">
<div class="g-recaptcha" data-sitekey="my site key here"></div>
</div>
<div class="col-12">
<input name="submit" type="submit" class="btn btn-success" value="Send Message">
</div>
</div>
<!-- end form element -->
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="main.js"></script>
</body>
</html>
PHP
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// access
$secretKey = 'my secret key here';
$captcha = $_POST['g-recaptcha-response'];
if(!$captcha){
echo '<p class="alert alert-warning">Please check the the captcha form1.</p>';
exit;
}
# FIX: Replace this email with recipient email
$mail_to = "demo#gmail.com";
# Sender Data
$subject = trim($_POST["subject"]);
$name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($phone) OR empty($subject) OR empty($message)) {
# Set a 400 (bad request) response code and exit.
http_response_code(400);
echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
exit;
}
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$captcha&remoteip=$ip");
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<p class="alert alert-warning">Please check the the captcha form2.</p>';
} else {
# Mail Content
$content = "Name: $name\n";
$content .= "Email: $email\n\n";
$content .= "Phone: $phone\n";
$content .= "Message:\n$message\n";
# email headers.
$headers = "From: $name <$email>";
# Send the email.
$success = mail($mail_to, $subject, $content, $headers);
if ($success) {
# Set a 200 (okay) response code.
http_response_code(200);
echo '<p class="alert alert-success">Thank You! Your message has been sent.</p>';
} else {
# Set a 500 (internal server error) response code.
http_response_code(500);
echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
}
}
} else {
# Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo '<p class="alert alert-warning">There was a problem with your submission, please try again.</p>';
}
?>
I had exactly the same problem and I posted my solution under the earlier question.
My tested solution is:
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret_key = 'YOUR_SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$message = 'Google reCaptcha Test';
if(!empty($response))
{
$url = $google_url."?secret=".$secret_key."&response=".$response;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$curlData = curl_exec($curl);
curl_close($curl);
$res = json_decode($curlData, TRUE);
if($res['success'] == 'true')
$message = "Success!";
else
$message = "Enter captcha again!";
}
After struggling for a while trying to implement checkbox recaptcha v2 in my form, I tried to implement invisible recaptcha v2, and this one works perfectly. I don’t know how this can happen! You can see in function here.
I don't know if it has something to do with my ssl certificate (from let's encrypt).
This tutorial help me a lot.
Thanks for the comments ehacinom!!

localhost is redirecting to 127.0.0.1 after submitting form in Codeigniter 3

I have a problem in my code. I have a form in my code that looks like this:
<?php echo form_open('users/auth/login', array('class' => 'form floating-label')); ?>
<div class="form-group">
<input type="text" class="form-control" id="username" name="username" />
<label for="username">Username</label>
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" />
<label for="password">Password</label>
<p class="help-block">Forgotten?</p>
</div>
<br/>
<div class="row">
<div class="col-xs-12 text-right">
<?php echo $this->session->flashdata('note'); ?>
<button class="btn btn-primary btn-raised btn-ink" type="submit">Login Account</button>
</div>
</div>
<?php echo form_close(); ?>
Then after clicking the submit it redirect my localhost to 127.0.0.1
After submitting the form it redirects me to
http://127.0.0.1/teradasys/index.php/user/login
Here's my controller
public function index() {
if($this->aauth->is_loggedin()) {
} else {
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
public function login() {
$identifier = $this->input->post('username');
$password = $this->input->post('password');
if ($this->aauth->login($identifier, $password, true)){
return true;
} else {
$note = $this->aauth->get_errors_array();
$this->session->set_flashdata('note', $note[0]);
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
Please check your base url in application/config/config.php
and change it.
$config['base_url'] = 'http://localhost/test/';
Please check your base url in application/config/config.php
change from
$config['base_url'] = 'http://localhost/test/';
to
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
No need to change anything after placing this .If u r using local or live host.

codeigniter uploading issues

i know this repeated question here , but i did not get meaning i can not understand. before i was uploaded the file using codeigniter format all are working fine. but the following code is give this error Message: Undefined index: file_name
my html code
<?php echo form_open('fileuplod/setflag') ;?>
<input type="text" name="tagname" class="tagname" id="tagname" placeholder="Flag Name"> <br/>
<input type="file" name="file_name" class="flagimg" id="flagimg">
<input type="submit" name="flagsubmit" value="set">
</div>
<?php
echo form_close(); ?>
and my php upload code
if($_FILES['file_name']['name'] != "")
{
$config['upload_path'] ='images/flag';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = false;
$ext =pathinfo($_FILES['file_name']['name'], PATHINFO_EXTENSION);
$config['file_name']= $ext;
$imgname = $config['file_name'];
$this->upload->initialize($config);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file_name'))
{
$error = $this->upload->display_errors();
$data= array('error'=>$error,);
$this->load->view('error',$data);
return false;
}
}
i can not identify why this error arise. please help anyone
Use form_open_multipart() in your form declaration.
Add this or replace your form_open()
<?php echo form_open_multipart('upload/do_upload');?>
or
<form method="post" action="<?=site_url("fileuplod/setflag");?>" enctype="multipart/form-data">
<input type="text" name="tagname" class="tagname" id="tagname" placeholder="Flag Name"> <br/>
<input type="file" name="file_name" class="flagimg" id="flagimg">
<input type="submit" name="flagsubmit" value="set">
</form>

invalid token in joomla

I am using joomla for my website
I have a function in my controller like this
function genericLogis(){
$model = &$this->getModel( JRequest::getCmd( 'view' ) );
$view = &$this->getView( JRequest::getCmd( 'view' ), 'html' );
$view->setModel($model, true);
$view->setLayout('logistical');
$view->logistical();
}
and in my model I have this
function getBasicos(){
JRequest::checkToken() or jexit( 'Invalid Token' );
$plantilla=1;
$db=& JFactory::getDBO();
$query= "SELECT *
FROM ".$db->nameQuote('#__accedo_materiales')."
WHERE ".$db->nameQuote('plantilla')."=".$db->quote($plantilla).";";
$db->setQuery( $query );
$materialesBasicos=$db->loadObjectList();
return $materialesBasicos;
}
and my view is a form like this
<?php defined('_JEXEC') or die('Restricted access'); ?>
<form action="index.php?option=<?php echo $option; ?>&view=<?php echo $view; ?>" method="post" name="adminForm">
.....
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="view" value="materiales" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
why when I click on the link to see this form I see Invalid Token!!

Resources