jQuery FileUpload syntax error - php

While implementing jQuery FileUpload in CodeIgniter everything seems to work, no javascript errors and the files get created in the default folders (chmod'ed 0777), but there are two issues.
1. I can't override the default options on the PHP handler provided by the package. Here is the Controller action I'm using to do so, none of the parameters passed are actually applied, if I dump the options within the UploadHandler I get the default options.
public function fileupload()
{
if ($this->input->is_ajax_request())
{
# load library
$this->load->library('UploadHandler');
# upload file
$upload_handler = new UploadHandler(array(
'upload_dir' => FCPATH.'upload/realty/'.$this->_user->id.'/',
'mkdir_mode' => 0777,
'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
));
}
else
{
redirect('error/page_not_found');
}
}
2. The script is throwing a error when a file finishes uploading :
SyntaxError: Unexpected token {
Here is the response of an example request :
{"files":[{"name":"nebula-ngc-281.jpg","size":590295,"type":"image\/jpeg","url":"http:\/\/test.filipematias.info\/mercadoimobiliario\/admin\/files\/nebula-ngc-281.jpg","thumbnailUrl":"http:\/\/test.filipematias.info\/mercadoimobiliario\/admin\/files\/thumbnail\/nebula-ngc-281.jpg","deleteUrl":"http:\/\/test.filipematias.info\/mercadoimobiliario\/admin\/?file=nebula-ngc-281.jpg","deleteType":"DELETE"}]}{"files":[{"name":"nebula-ngc-281 (1).jpg","size":0,"type":"image\/jpeg","error":"File upload aborted","deleteUrl":"http:\/\/test.filipematias.info\/mercadoimobiliario\/admin\/?file=nebula-ngc-281%20%281%29.jpg","deleteType":"DELETE"}]}

According to this document Creating Libraries check how we can pass the param in library
so it would be:-
$options= array(
'upload_dir' => FCPATH.'upload/realty/'.$this->_user->id.'/',
'mkdir_mode' => 0777,
'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
));
$this->load->library('UploadHandler',$options);

Related

Uploading a file via PHP + Symfony 4 throws an "File not found error"

I am trying to upload a file using Symfony 4 documentation (https://symfony.com/doc/4.0/controller/upload_file.html) - yes, I know that is an obsolete version, but at the moment I can't update it.
I have done everything as in documentation (except for creating a new entity, because I only need to upload files, and send link to that file in email), files are uploaded correctly to my directory, but it throws HTTP 500 errors, and in log files there are something like this:
[2020-04-20 15:39:40] request.CRITICAL: Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "/tmp/phpr2tM6D" does not exist" at [...]/vendor/symfony/http-foundation/File/File.php line 37 {"exception":"[object] (Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException(code: 0): The file \"/tmp/phpr2tM6D\" does not exist at [...]/vendor/symfony/http-foundation/File/File.php:37)"} []
any ideas?
ok, so form is basically just
{{ form_start(warranty_form) }}
{{ form_end(warranty_form)}}
and fragments of controller
$form = $this->createForm(WarrantyType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
/** #var UploadedFile $documentFile */
$documentFile = $form->get('documentFile')->getData();
if($documentFile) {
$originalDocumentFilename = pathinfo($documentFile->getClientOriginalName(), PATHINFO_FILENAME);
$newDocumentFilename = uniqid().'.'.$documentFile->guessExtension();
try {
$documentFile->move('%kernel.project_dir%/public/uploads/pdf',$newDocumentFilename);
} catch(FileException $e) {
}
$message = (new \Swift_Message('Test email '))
->setFrom('error#example.org')
->setTo('error#example.org')
->setBody("a","text/plain");
}
and form is just a standard form, with possibility to upload PDF files
->add('documentFile', FileType::class, ['label' => 'Dokument', 'mapped' => false, 'required' => true, 'constraints' => [new File(['maxSize' => '1024k', 'mimeTypes' => ['application/pdf', 'application/x-pdf'], 'mimeTypesMessage' => 'Załaduj prawidłowy dokument'])]])
First i would use your try+catch block to output your error details with
} catch(FileException $e) {
dd($e);
}
There could be more information you need to debug.
From your provided error message "The file "/tmp/phpr2tM6D" does not exist" i would suggest to check that after an upload that this file or a similiar is created for "/tmp" AND way more important is that it can be accessed by your PHP Process. I would say the file is there, but it cannot be read by php. This could happen if your webserver and php use different owner/groups.
Can you show whole controller method? Do you use $documentFile variable after move file?
It looks like the catch(FileException $e) should suppress your exception, and as it still appear I think maybe you somewhere try to access $documentFile variable again or maybe tried to call $form->get('documentFile')->getData()?
You should know that $documentFile->move('', ''); is use $renamed = rename($this->getPathname(), $target);
more see

Ckeditor 5 file uploading adapter error

I'm using ckeditor classic 5, and i'm trying to upload images in it. I downloaded php lib Ckfinder3 php connector, set config.
When i'm trying to load image i have a massage: Cannot upload file *filename*.
Interesting moment, that they are fisically loaded on server: i can view it in directory, that set in my config file and this files are correct:
/var/www/projectfiles/downloadfiles/
The next step i'm check permission and groups: it's www-data 0777.
What can be a reason? Can you give some advice or solution?
Thanks!
UPD: init function
ClassicEditor
.create( document.querySelector( '#material_preview' ), {
ckfinder: {
uploadUrl: '/ckfinder/connector/?command=QuickUpload&type=Images'
}
} )
.catch( error => {
console.error( error );
} );
php config:
$config['authentication'] = function () {
return true;
};
$config['backends']['default'] = array(
'name' => 'default',
'adapter' => 'local',
'baseUrl' => 'http://files.project.com/downloadfiles/',
'root' => '/var/www/projectfiles/downloadfiles/',
'chmodFiles' => 0777,
'chmodFolders' => 0777,
'filesystemEncoding' => 'UTF-8'
);
The connector path looks invalid. For PHP it should be something like:
'/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files'
Please have a look at CKEditor integration examples in CKFinder demo: https://ckeditor.com/docs/ckfinder/demo/ckfinder3/samples/ckeditor.html
Finally i got it!
The connector path is valid. The problem was in cache folder's permisson in laravel. Be glad if it helps you.

load selected pre-existing files in blueimp

I am able to load all the files in a directory using blueimp file uploading plugin.
I just want to select only few of the files using filenames in that.
I know that get method of uploadhandler returns all the files in given directory but is there any way to select only few files?
This is what I have done to get all files..
public function getFile() {
extract($this -> reqData);
require_once ('path/to/UploadHandler.php');
$options = array(
'script_url' => "path to uploadhandler",
'upload_dir' => "xxxxxxx",
'upload_url' => "xxxxxxx",
'type' => 'project_files'
);
$upload_handler = new UploadHandler($options);
exit;
}
Please don't give negative remarks just because my editing is poor.
Thanks everybody...

CakePHP Error: Missing view for Media viewclass

Here is my function to download the uploaded files:
public function download() {
$this->viewClass = 'Media';
$params = array(
'id' => 'article.pdf',
'name' => 'article',
'download' => true,
'extension' => 'pdf',
'path' => WWW_ROOT . DS . 'files' . DS
);
$this->set($params);
}
However calling this function results in the following error message:
Error: Media could not be found.
Error: Create the class Media below in file: src\View\Media.php
Any suggestions what is wrong with my code? Thanks in advance!
Any suggestions what is wrong with my code?
Yes:
Wrong CakePHP Version (3.x) for what you try to use
And even in 2.x Media view is deprecated since 2.3 as the documentation clearly states on top of the page: Deprecated since version 2.3: Use Sending files instead.
Read the documentation!
Taken from the link above:
public function sendFile($id)
{
$file = $this->Attachments->getFile($id);
$this->response->file($file['path']);
// Return response object to prevent controller from trying to render
// a view.
return $this->response;
}
And always put the exact CakePHP version you're using in your question.
Error: Create the class Media below in file: src\View\Media.php
Create file in
Path - src/View/
File Name - media.php

Yii: How to make Dropzone Extension work?

I'm trying to add Dropzone Extension to my application in Yii, which allows asynchronous file uploading. http://www.yiiframework.com/extension/yii-dropzone/
The first thing i did was putting the downloaded folder called "dropzone" into my extensions folder "C:\xampp\htdocs\site\protected\extensions".
And here is my code for the action in the controller (MainController.php)
public function actionUpload()
{
$test = rand(100000, 999999); //TEST
var_dump($test);
$model = new UploadFile;
if(isset($_FILES['images'])){
$model->images = CUploadedFile::getInstancesByName('images');
$path = Yii::getPathOfAlias('webroot').'/uploads/';
//Save the images
foreach($model->images as $image)
{
$image->saveAs($path);
}
}
$this->render('upload', array('model' => $model));
}
the view (upload.php)
<?php
$this->widget('ext.dropzone.EDropzone', array(
'model' => $model,
'attribute' => 'images',
'url' => $this->createUrl('file/upload'),
'mimeTypes' => array('image/jpeg', 'image/png'),
'options' => array(),
));
?>
and the model (UploadFile.php)
<?php
class UploadFile extends CFormModel
{
public $images;
public function rules(){
return array
(
array(
"images",
'file',
'types' => 'jpg,gif,png',
),
);
}
}
When I run it I can see the Dropzone interface and I can add images dragging them or selection them from the file explorer.
It appears their respective progress bar and a success mark, but nothing appear in the directory of uploads, and any error is shown neither in the IDE (Netbeans) nor in the Chrome console.
I did some print tests and I realize that the code inside the 'actionUpload' is being executed only the first time (when it draws the view), but when its called from the dropzone widget it do nothing.
I'd really appreciate if you have a solution for this. I'd love if someone could give me a simple working example of this extension. Thanks.
As I understand, dropzone uploads files one by one, not all together. So $model->images holds only one image object. And foreach cycle fails.

Categories