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',
));
Related
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 :')
Hi everyone i need some help i work a program called file upload , but i dont get it what happen to the file i upload in laravel. in public folder there's a name called php4CA1.tmp every time i upload a pdf file please can anybody help me ? what is the meaning of this php4CA1.tmp or my code is wrong?
$request->validate([
'cover' => 'required',
'book' => 'required',
'name' => 'required',
'description' => 'required',
'price' => 'required|numeric'
]);
$image = $request->file('cover');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'),$new_name);
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$book->move(public_path('books'),$book);
$data = array(
'cover' => $new_name,
'book' => $new_book_name,
'name' => $request->name,
'description' => $request->description,
'price' => $request->price
);
Book::create($data);
Hi friend please follow as below you are passing the file instead of file name,
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$response = $book->move(public_path('books'),$new_book_name );
You are passing $book in
$book->move(public_path('books'),$book);
Use this:
$book = $request->file('book');
$new_book_name = rand() . '.' . $book->getClientOriginalExtension();
$book->move(public_path('books'),$new_book_name);
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");
I have following error during creating checboxes in Symfony 1.4 app.
Checkboxes are rendered but with "Array to string conversation" error.
Below is my code.
Form
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => 'Emails',
'expanded' => true,
'multiple' => true,
'choices' => array('test', 'test2'),
]));
Render
<div class="container emails">
<fieldset>
<legend>
<?php echo $form['emails']->renderLabel(); ?>
</legend>
<?php echo $form['emails']->render(); ?>
<?php echo $form['emails']->renderError(); ?>
</fieldset>
</div>
Error
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 103
Notice: Array to string conversion in /var/www/html/symfony/1_4_1/lib/widget/sfWidgetFormSelectCheckbox.class.php on line 10
My Php version is 5.5.8
Symfony version 1.4.19
I know that the best way would be move app to newest symfony version, but this application is too large to rewrite.
Anyone have idea how to solve it?
//Edit
I noticed that if I change code to
$arr = [];
array_push($arr, 'test');
array_push($arr, 'test2');
$this->setWidget('emails', new sfWidgetFormSelectCheckbox([
'label' => __('Adresy email'),
'choices' =>$arr,
]));
it returns any errors, but if I add one more value to array, errors shows again.
Whole class code
class EmailFooterGeneratorForm extends BaseForm
{
/**
* configure...
*
* #param mixed $data
*/
public function configure()
{
sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
$this->setWidget('regards', new sfWidgetFormInputText([
'label' => __('Treść pozdrowień'),
'default' => 'Pozdrowienia/Best regards',
], [
'size' => 45
]));
$this->setWidget('emails', new sfWidgetFormChoice([
'label' => __('Adresy email'),
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('phones', new sfWidgetFormChoice([
'label' => __('Numery telefonu'),
'expanded' => true,
'multiple' => true,
'choices' => $this->getDefault('phones'),
]));
$this->setWidget('employment', new sfWidgetFormSelectRadio([
'label' => __('Zatrudnienie'),
'choices' => $this->buildEmployment($this->getDefault('employment'))
]));
$this->setWidget('certyfications', new sfWidgetFormChoice([
'label' => __('Certyfikaty'),
'multiple' => true,
'expanded' => true,
'choices' => ['AEO', 'TÜV Rheinland', 'FSC']
]));
$templateChoices = [
'' . __('Szablon') . ' 1 ',
'' . __('Szablon') . ' 2 '
];
$this->setWidget('templates', new sfWidgetFormSelectRadio([
'label' => __('Szablony'),
'choices' => $templateChoices
]));
$this->setWidget('www', new sfWidgetFormInputText([
'label' => __('Strona wwww'),
'default' => 'www.fakro.com'
]));
$this->setValidators([
'regards' => new sfValidatorString(
['max_length' => 50, 'min_length' => 12],
['required' => __('Wymagane'),
'min_length' => __('Treść pozdrowień musi mieć przynajmniej %min_length% znaków.'),
'max_length' => __('Treść pozdrowień może mieć maksymalnie %max_length% znaków.')
]
),
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Wymagane')]
),
'employment' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('employment'))],
['required' => __('Wymagane')]
),
'templates' => new sfValidatorChoice(
['choices' => array_keys($templateChoices)],
['required' => __('Wymagane')]
),
'www' => new sfValidatorRegex(
['pattern' => '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/'],
['invalid' => __('Niepoprawny adres strony wwww')]
)
]);
$this->validatorSchema['phones'] = new sfValidatorString(['required' => false]);
$this->validatorSchema['certyfications'] = new sfValidatorString(['required' => false]);
}
private function buildEmployment($employment)
{
$arr = [];
foreach ($employment as $key) {
$str =
"<div style='margin-left: 30px'>" .
__('Firma') . ": " . $key['company_name'] . "<br>" .
__('NIP') . ": " . $key['nip'] . "<br>" .
__('REGON') . ": " . $key['regon'] . "<br>" .
__('Miasto') . ": " . $key['city'] . "<br>" .
__('Ulica') . ": " . $key['street'] . "<br>" .
__('Kod') . ": " . $key['postal'] . "<br>" .
__('Kraj') . ": " . $key['country'] . "<br>" .
__('Wydział') . ": " . $key['depertment_name'] . "<br>" .
__('Stanowisko') . ": " . $key['job_name'] . "<br>
</div>"
;
$arr[] = $str;
}
return $arr;
}
}
It is a bit late, but since I was having this issue and someone else may need it, I found that the issue was that I was using a sfWidgetFormChoice set to be multiple, but I didn't tell it to the validator.
The solution is just to pass the option to it as well.
End up having something like
$this->validatorSchema['choices_name'] = new sfValidatorChoice([
'required' => false,
'multiple' => true,
'choices' => $choices
]);
I'm using it in a Form class, that's why you see $this->validatorSchema, if you are using it anywhere else, like in the OP case, just add that 'multiple' => true as option in the options array.
I refer to http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/ and follow this, I can upload 1 file successfully by using rename filter in ZF2.
However when I use this way to upload 2 files, it goes wrong. I paste my code as following:
$this->add(array(
'name' => 'bigpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Big Pic'
)
));
$this->add(array(
'name' => 'smallpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Small Pic'
)
));
<div class="row"><?php echo $this->formRow($form->get('smallpicture')) ?></div>
<div class="row"><?php echo $this->formRow($form->get('bigpicture')) ?></div>
$data = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($data);
if ($form->isValid()) {
$product->exchangeArray($form->getData());
$picid = $this->getProductTable()->saveProduct($product);
$pathstr = $this->md5path($picid);
$this->folder('public/images/product/'.$pathstr);
//move_uploaded_file($data['smallpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg');
//move_uploaded_file($data['bigpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg');
$fileadaptersmall = new \Zend\File\Transfer\Adapter\Http();
$fileadaptersmall->addFilter('File\Rename',array(
'source' => $data['smallpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg',
'overwrite' => true
));
$fileadaptersmall->receive();
$fileadapterbig = new \Zend\File\Transfer\Adapter\Http();
$fileadapterbig->addFilter('File\Rename',array(
'source' => $data['bigpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg',
'overwrite' => true
));
$fileadapterbig->receive();
}
the above are form,view,action.
using this way, only the small picture uploaed successfully. the big picture goes wrong.
a warning flashed like the following:
Warning:move_uploaded_file(C:\WINDOWS\TMP\small.jpg):failed to open stream:Invalid argument in
E:\myproject\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\http.php
on line 173
Warning:move_uploaded_file():Unable to move 'C:\WINDOWS\TMP\php76.tmp'
to 'C:\WINDOWS\TEMP\big.jpg' in
E:\myproject\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\http.php
on line 173
Who can tell me how to upload more than 1 file in this way. you know, the rename filter way similar to above. thanks.
I ran into the same problem with a site i did. The solution was to do the renaming in the controller itself by getting all the images and then stepping through them.
if ($file->isUploaded()) {
$pInfo = pathinfo($file->getFileName());
$time = uniqid();
$newName = $pName . '-' . $type . '-' . $time . '.' . $pInfo['extension'];
$file->addFilter('Rename', array('target' => $newName));
$file->receive();
}
Hope this helps point you in the right direction.
I encountered the same problem and i managed to make it work using the below code;
$folder = 'YOUR DIR';
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination($folder);
foreach ($adapter->getFileInfo() as $info) {
$originalFileName = $info['name'];
if ($adapter->receive($originalFileName)) {
$newFilePath = $folder . '/' . $newFileName;
$adapter->addFilter('Rename', array('target' => $newFilePath,
'overwrite' => true));
}
}