Symfony 2.8: how to upload an image from URL soruce? - file

How are you? First, sorry about my english :(
I'm triying to upload a file from a URL source like https://www.somedomain/somefolder/someimage.jpg but I'm getting some troubles...
I get this error when I'm triying to set $myFile->setFile('url'):
Catchable Fatal Error: Argument 1 passed to
MyBundle\Entity\File::setFile() must be an instance of
Symfony\Component\HttpFoundation\File\UploadedFile, string given
I've also tried through $content = get_file_contents('url') and then $myFile->setFile($content), but this returns the same error.
On the other hand, it's not a problem if I upload the file with an inputfile box in a form. But I cannot do it by indicating only one URL source (for a CronJob, by the way).
I've read the documentation about Files on Symfony, but I can't get success :(
Can anyone help me with this issue, please?
Thanks a lot! Greetings from Barcelona!! :)

To get rid of this issue you can do this:
<?php
use Symfony\Component\HttpFoundation\File\UploadedFile;
$file = new UploadedFile('url', 'name of the file');
$myFile->setFile($file);
Take a look at UploadedFile
Be sure to provide the correct path to the path. Not sure it will work like this because you want to set the UploadedFile without uploading the file through an input file and without submitting the form. Have a try.

Related

CakePHP routing not redirecting correctly

I have a problem and i have no idea what's wrong.
I have build a basic authentication system, simple one. but what i noticed is that the URL- from the side bar is different from the one that is generatet from cakephp for example:
http://localhost/sitename/users
is the url that displays on toolbar.
When i do:
echo Router::url($this->here, true );
the result is:
http://localhost/sitename/sitename/users
The site still works, but time after time generates an error such as:
http://localhost/sitename/sitename/users/
Missing Controller
Error: SitenameController could not be found.
Error: Create the class SitenameController below in file: app\Controller\SitenameController.php
<?php class SitenameController extends AppController {
}
So i dont know what is causing the problem...
If someone, anyone could help me i would very appruciate it...
Thank you very much.
Your app is in a subdirectory so you should use
Router::url(null, true);
If the $url param is null the method will find the address to the actual controller/action. Read more here.
From the book:
$this->request->here contains the full address to the current request.
The full address includes the subdirectory as well. So if you use Router::url() with the $full param set to true it "duplicates" the subdirectory.

How to get the latest modified file in a directory with Cakephp

Is there any method in Cakephp that is able to return the latest modified file in a directory? I have tried with $this->System->getLatestModifiedFile() in Cakephp Model but got Call to a member function getLatestModifiedFile() on a non-object error.
Thanks for advice there!
Please go through http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html and look for File::lastChange() in File API section. This might help.
With reference to cakephp file and folder API, i managed to use File::lastChange() method to solve my issue. Thanks for the help there. Here's an example:
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
$file = new File('/tmp/test.txt');
//the following will return an epoch time, can be formatted by date method
echo $file->lastChange();

Correct way in CakePHP 1.3 to create zip file and download without leaving view

I just upgraded from cakephp 1.1 to 1.3. I have everything on the site updated and working great, except for creating and downloading zip files.
Here is the code in my accounts_controller.php:
function zip() {
$this->checkSession();
$this->checkUpgradedAccount();
$files = array();
$this->layout="zip";
/*
code where I locate the files to zip, combine them, etc
*/
$tmp_file = "/home/[userdirectory]/tmp/".md5(mktime()).".zip"; //directory name edited
$command = "/usr/bin/zip -j $tmp_file ".implode(" ",$zip_files);
exec($command);
foreach($zip_files as $zf) {
unlink($zf);
}
$file_path = $tmp_file;
$this->set("path",$file_path);
$this->render();
}
When I call this action, however, I get an error:
Error: The requested address '/accounts/zip' was not found on this
server.
It worked just like this in version 1.1. I'm assuming something has changed, but I'm not sure what, and was unable to find anything pertinent in the documentation.
The zip.ctp view file does exists, but it has nothing in it other than: <?php ?>
I suspect something is different with layouts. There is NO "zip.ctp" in the /layouts directory. However, I have changed that line to $this->layout('default'); and it renders a blank page with NO ERROR, but also with no download.
Please direct me on the proper way to download my zip file in cake 1.3. Thanks in advance.
You have two different problems here. That error you're getting is because you don't have a zip layout file. As for your problem with getting the zip file, you should be using the media view class - http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Views.html#media-views

Javascript edit external file

Okay so I'm trying to make a script that can edit an external .txt file. I want to be able to do something like /name John Doe and it saves that name in the file that the command is supposed to edit.
Another example would be I have a file called List.txt associated with the command /todo, whenever I do /todo * it adds whatever came after the command to the List.txt file.
Is there any way I can do this in javascript?
You're in luck, it appears that HTML5 actually supports this. Of course you'll have to run it through a browser, I don't know if you can hack it somehow to work from bash.
Yes, its possible to do this by creating an ajax http request to an server server side script that edits the file based on the http request's content.
Heres an example PHP serverside script the handle the ajax request:
Note: This example has a lot of security issues and is untested
<?php
$command = $_POST['command'];
$argument = $_POST['argument'];
if ($command == "name") {
$file = fopen("names.txt", "a");
fwrite($file, $argument."\n");
fclose($file);
} else if ($command == "todo") {
$file = fopen("todo.txt", "a");
fwrite($file, $argument."\n");
fclose($file);
}
?>
There is also a great tutorial on AJAX requests here
They also have a php tutorial on here
ps. sorry it took so long.

CakePHP Cakemenu plugin fails after global error due to incorrect string encoding

I am using CakePHP 2.1.2 with PHP 5.3.5 and a plugin called 'Cakemenu' which normally works fine. The plugin stores menus in a db table with the menu link stored as text like
array('plugin'=>null,'controller'=>'assets','action'=>'index');
The helper in the plugin gets those values, then executes this code to convert that text to an array:
//Try to evaluate the link (if starts with array)
if (eregi('^array', $value['Menu']['link'])) {
$code = "\$parse = " . $value['Menu']['link'] . ";";
$result = eval($code);
if (is_array($parse)) {
$value['Menu']['link'] = $parse;
}
}
Everything works fine unless CakePHP is handling an error. For example if I mistype the name of a controller in the browser I should get a menu and then the missing controller message. Instead I get a page full "Parse error: syntax error, unexpected $end in..." messages pointing to the line with the eval statement. If I printout the variable that is getting eval'ed I see that it has been (incorrectly) encoded with Html entities when it normally does not.
Good string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'assets','action'=>'index');
Bad string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'Parts','action'=>'add');
To temporarily fix the problem I added two statements to just replace the offending characters
$value['Menu']['link'] = str_replace( ''','\'',$value['Menu']['link']);
$value['Menu']['link'] = str_replace( '>','>',$value['Menu']['link']);
and everything works great again. Some other pieces of information that might be helpful is that the array of data used to generate the menu is read during the beforeFilter of the app and saved in a view variable and then the menu is generated as an element in the view.
I'm thinking that the error causes CakePHP (or PHP) to skip some loading or configuration process and that causes the string to be mishandled. Any help would be appreciated, thanks
Your beforeFilter() method won't be executed on error pages. You'll have to handle your errors yourself and manually call beforeFilter(). I wrote a blog post on how to use custom error pages - pay close attention to the Controller Callbacks section.

Resources