Convert any audio files to mp3 format codeigniter ffmpeg - php

I want to convert uploaded audio files to mp3 with ffmpeg in codeigniter.I've put the ffmpeg files in my codeigniter root folder.And here is my code controller,
$this->upload->initialize(
array(
"upload_path" => './uploads/',
"overwrite" => FALSE,
"max_filename" => 300,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "mp3|amr",
"max_size" => $this->settings->info->file_size,
)
);
if ( ! $this->upload->do_upload('audio_file')) {
$error = array('error' => $this->upload->display_errors());
$this->template->jsonError(lang("error_97") . "<br /><br />" .
$this->upload->display_errors() . "<br />" . mime_content_type($_FILES['audio_file']['tmp_name']));
}
// $data = $this->upload->data();
$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'];
exec("ffmpeg -i " . $directory_path_full . " " . $directory_path . $file_name . ".mp3");
$file_name = $file_name . '.' . 'mp3';
$audioid = $this->feed_model->add_audio(
array(
"file_name" => $file_name,
"file_type" => $data['file_type'],
"extension" => $data['file_ext'],
"file_size" => $data['file_size'],
"userid" => $this->user->info->ID,
"timestamp" => time()
)
);
Next what is code to convert the audio file to mp3 using ffmpeg.Or any other way to do this?Can anybody suggest me?

You cannot convert files using PHP but you can use ffmpeg
$ ffmpeg -i input.mp4 output.mp3
in PHP you can use exec() to execute above command
exec("ffmpeg -i /path/to/input.mp4 /path/to/output.mp3");

Related

Cannot upload file: (file name), Ckeditor 5 Laravel

Help :(, I'm a newbie trying out CKeditor 5 for my post form
I'm using ckeditor 5, and i'm trying to upload images in it. But, when i'm trying to load image i have a massage: Cannot upload file filename.
Where's the problem?
Init function :
ClassicEditor
.create( document.querySelector( '#body' ), {
ckfinder:{
uploadUrl: "{{ route('ckeditor.upload') .'?token=' . csrf_token()}}"
}
} )
.catch( error => {
console.error( error );
} );
PHP config :
$config['backends']['default'] = array(
'name' => 'default',
'adapter' => 'local',
'baseUrl' => config('app.url').'/userfiles/',
'root' => public_path('/userfiles/'),
'chmodFiles' => 0777,
'chmodFolders' => 0755,
'filesystemEncoding' => 'UTF-8'
);
Controller :
public function uploadImage(Request $request){
if($request -> hasFile('upload')){
$originame = $request->file('upload')->getClientOriginalName();
$fileName = pathinfo($originame, PATHINFO_FILENAME);
$extension = $request->file('upload')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$request->file('upload')->move(public_path('media'), $fileName);
$url = asset('media/' . $fileName);
return response()->json(['fileName' => $fileName, 'uploaded'=> 1, 'url' => $url]);
}
}
Thank you, help me :')

Unable to open using mode r: fopen() while uploading file to aws s3 from elastic beanstalk with php

I have uploaded my codeigniter project to aws elastic beanstalk and i am trying to upload an imagefile and an audio file after the image is done uploading.
Now the image upload is a successful one and below is the code
$path = $_FILES['thumbnail']['name'];
$file_temp = $_FILES['thumbnail']['tmp_name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$new_name = time() . "." . $ext;
$file_type = $_FILES['thumbnail']['type'];
$upload_path = 'uploads/thumbnails/';
$s3->putObject(
array(
'Bucket' => 'bucket-name',
'Key' => $upload_path . $new_name,
'SourceFile' => $file_temp,
'ContentType' => $file_type,
'ACL' => 'public-read',
'StorageClass' => 'STANDARD'
)
);
and for the audio file upload the code sample is as below
$path = $_FILES['audio']['name'];
$file_name = "audio_" . time() . "." . pathinfo($path, PATHINFO_EXTENSION);
$file_temp = $_FILES['audio']['tmp_name'];
$file_type = $_FILES['audio']['type'];
$upload_path = 'uploads/audios/';
$s3->putObject(
array(
'Bucket' => 'bucket-name',
'Key' => $upload_path . $file_name,
'SourceFile' => $file_temp,
'ContentType' => $file_type,
'ACL' => 'public-read',
'StorageClass' => 'STANDARD'
)
);
And here is the snippet that executes the 2 upload methods
//upload image file
$thumb_upload = $this->upload_thumbnail();
//upload video file
$audio_upload = $this->upload_audio();
But i am trying to find out what the error is because if the image upload is a success, why is the audio upload giving an error as
Error:Unable to open using mode r: fopen(): Filename cannot be empty
Thanks in advance

Codeigniter Image Upload PathInfo Extension 'The filetype you are attempting to upload is not allowed'

I am uploading multiple images and I need to get their different file format.
But the problem is i only got the first file image format and there's an error when I do resize images because of the incorrect file format.
I am having an error The filetype you are attempting to upload is not allowed. which occur when I put the PATHINFO_EXTENSION when getting file.
Here's my code
for($i = 0; $i < $count_upload; $i++) {
$_FILES['userfile']['name'] = pathinfo($_FILES['product_image']['name'][$i], PATHINFO_EXTENSION);
$_FILES['userfile']['type'] = $_FILES['product_image']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['product_image']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['product_image']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['product_image']['size'][$i];
$file_name = $file_name;
$config = array(
'file_name' => $file_name,
'allowed_types' => 'jpg|jpeg|png',
'max_size' => 3000,
'overwrite' => TRUE,
'encrypt_name' => FALSE,
'remove_spaces' => TRUE,
'upload_path'
=> $directory
// => './assets/img/products'
);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$response = array(
'status' => false,
'environment' => ENVIRONMENT,
'message' => $error['error'],
'csrf_name' => $this->security->get_csrf_token_name(),
'csrf_hash' => $this->security->get_csrf_hash()
);
echo json_encode($response);
die();
}else {
$file_name = $this->upload->data()['file_name'];
echo $file_name;
echo '<br>';

Image not resize and upload in Codeigniter PHP

I am trying to create thumbnail(resize) of uploaded image using codeigniter,
But image is not uploaded in server/folder,Here is my code, Where I went wrong ?
if (isset($_FILES['image'])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$filename = time() . uniqid(rand()) . $_FILES['image']['name'];
move_uploaded_file($_FILES["image"]["tmp_name"], "vendorProfile/" . $filename);
$saveArr['image'] = $filename;
$this->load->library('image_lib');
$source_paths = base_url() . 'vendorProfile/' . $filename;
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/thumb';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
} else {
echo "image is uploaded";
}
$this->image_lib->clear();
}
}
You just look at the file path which you pass on $config_manip array where you need to pass file path without Http or https PHP don't accept Http. So if in your case your file struct like something this
-
application
system
image_folder <----
Then you just pass like something this
$config_manip = array(
'image_library' => 'gd2',
'source_image' => './image_folder/'.$file_name,
'new_image' => './thumb_folder/'.$target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
If you are using the ubuntu server you need to give permission for that folder to upload the file/image.
if (!file_exists($target_path)) {
if (!mkdir($target_path, 0777, true)) {
chmod($target_path, 0777);
}
}

symfony 1.4 file uploads and templates

I successfully saved the path of photo into mysql database.My question is,how to render correctly in templates?I tried
<td width="129" rowspan="5"><img src= "/uploads/photos<?php echo $applicant->photo?>"width="129" height="129"/></td>
Not working.
In my action, i tried
$photo = $this->form->getValue('photo');
$filename = 'photo'.sha1($photo->getOriginalName());
$extension = $photo->getExtension($photo->getOriginalExtension());
//$photo->save(sfConfig::get('sf_upload_dir').'/' . $filename . $extension);
$photo->save(sfConfig::get('sf_upload_dir') . '/'.$photo) . date('Ym') . '/' . $filename . $extension);
and in my form i tried
$this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Photo',
'file_src' => '/uploads/photos/'.$this->getObject()->getPhoto(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div class="sublabel">%file%<br />%input%<br />%delete% %delete_label%</div>',
));
$this->validatorSchema['photo'] = new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir').'/photos',
'mime_types' => 'web_images',
));

Categories