Miles Johnson's file uploader generates empty files - php

I'm using CakePHP 2.3.6 and just added Miles Johnson's image uploader.
Everything seems to work when I use my View to upload a photo I get a success return.
A file gets created with the right name, so formatName function seams to work.
But the size of the created file is always only 56bytes. No matter how big the originally uploaded file was.
I have the strong feeling that there is something wrong with user rights or with configuration of apache server that it maybe does't alow upload.
Does anybody know what could let me create a file but not put content into this file?
On the Uploader Howto there is nothing said about any changes I would have to do on apache or php configuration.
View:
<?php
echo $this->Form->create('Site');
echo $this->Form->input('startpageImage', array('type' => 'file'));
echo $this->Form->end('Submit');
?>
Model:
public $actsAs = array(
'Uploader.Attachment' => array(
'startpageImage' => array(
'tempDir' => TMP,
'finalPath' => '/img/uploads/',
'nameCallback' => 'formatName',
)
)
);
public function formatName($name, $file) {
return sprintf('%s-%s', $this->field('id'), $name);
}

I wrote a email to Miles Jones and he found the mistake immediately:
View:
<?php
echo $this->Form->create('Site', array('type' => 'file'));
echo $this->Form->input('startpageImage', array('type' => 'file'));
echo $this->Form->end('Submit');
?>
I forgot the
, array('type' => 'file')
in the first line of Form.
No black magic, no rocket science... :)

Related

URL with no corresponding view

I'm trying to get a fileupload working in the same way the existing one does, so I tried to look at how that one works. The original upload functionality just calls a URL (site.com/model/upload/model_id) and then the file that was selected gets saved on the server. Other than that, it renders exactly the same as the model view would (site.com/model/model_id)
Seeing that, I started looking through all server files. There is not a single file called upload, not any other mention of that URL, not even anything in the site controller. The only thing I found was in the modelController:
public function actionUpload($id) {
$model = new Model();
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file) {
$path = 'uploads/documents/'. $id .'/';
FileHelper::createDirectory($path);
$model->file->saveAs($path . $model->file->baseName . '.' . $model->file->extension);
}
return $this->render('view', ['model' => $this->findModel($id)]);
}
How does this work? I tried to replicate it with a copy of this function with a different name, but that then just gives me a page not found error.
The code for the documentupload in the view is this:
<?php $form = ActiveForm::begin(['action' => \yii\helpers\Url::to(['upload', 'id' => $model->id]), 'options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file')->fileInput(['style' => ''])->label(false) ?>
<?= Html::submitButton('Upload', ['class' => 'btn-success']) ?>
<?php ActiveForm::end() ?>
Edit: Solved by adding a boolean parameter to the actionUpload function.

Yii2 uploaded image doesn't show up

I use this yii2 module for an image uploading purposes.
My upload folder is:
C:\XAMPP\HTDOCS\MYAPPNAME\WEB\UPLOAD
├───cache
├───global
└───store
└───Products
└───Product15
Where store is the folder for original pictures and the cache is for the resized ones. yii2-images is configured like this:
'yii2images' => [
'class' => 'rico\yii2images\Module',
//be sure, that permissions ok
//if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
'imagesStorePath' => 'upload/store', //path to origin images
'imagesCachePath' => 'upload/cache', //path to resized copies
'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
'placeHolderPath' => '#webroot/upload/store/no-image.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
],
and my Product model upload method is:
public function upload()
{
if ($this->validate()) {
$path = 'upload/store/' . $this->image->baseName . '.' . $this->image->extension;
$this->image->saveAs($path);
$this->attachImage($path);
#unlink($path);
return true;
} else {
return false;
}
}
Uploading images work. I mean, they fisically appear where they should. I load image
<?php $img = $model->getImage(); ?>
and in my DetailView widget instead of 'img':
[
'attribute' => 'image',
'value' => "<img src='{$img->getUrl()}'>",
'format' => 'html',
],
And what I get instead of a rendered image is:
image-by-item-and-alias?item=Product15&dirtyAlias=71273544a6-1.jpg
I have no idea where this "image-by-item-and-aliasitem=Product15&dirtyAlias?" part even comes from when it actually must be something like:
Products\Product15\71273544a6-1.jpg
and given format html parameter it must therefore render an image.
Can you please pinpoint me to the source of this error?

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...

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