I have an issue with my current web project. I need to check if my request contains an input called image, so I just apply the normal method like so:
if($request->has('image'))
{
...
}
But for some reason, this is not working as shown below:
Can someone explain me why ?
I think you need to access uploaded files as,
$request->hasFile('image');
then it should be
if($request->hasFile('image')) { ...
see this DOC
Related
I want to create a file and download it during a Laravel Action.
I use the following code:
public function handle(ActionFields $fields, Collection $models)
{
Storage::disk('local')->put('file.txt', 'example');
$url = Storage::disk('local')->url('file.txt');
return Action::download($url, 'file.txt');
}
This causes the following error :
Failed - no file
What am I doing wrong here?
Not sure where that error message comes from, but it may be the following problem. Calling return Action::download($url, 'file.txt') laravel nova will instruct the browser to download the file at the given url. As mentioned here this url will (depending on your config) probably be the relative url /storage/file.txt. So the browser will try to download http(s)://domain/storage/file.txt which might not be there. The docs mentioned above hold a solution for that.
If you wouldn't even want to create a file which would maybe need cleanup and protection if the data is sensitive, you could as well point the url to a route and do processing in a controller.
I use:
wampserver
laravel 5
bootstrap v3.3.6
I'm new in laravel, I usually use a manual route::get. Now I try to use route:resource to make things faster but somehow those two display different result even though it's should be showing the same page.
My routes code:
Route::get('create', 'PostController#create');
Route::resource('posts','PostController');
as you can see above, it's directing to the same controller
PostController#create code:
public function create()
{
$data['title'] = ' | Create Post';
return view('posts.create',$data);
}
When I go to blog.dev/create (which is from route::get). It's successfully displaying:
BUT, when I go to blog.dev/posts/create (which is from route::resource). It's showing a css-less page:
Why is that happen? any way to fix it? (I prefer not to use route::get)
Note:
I've tried different browsers, clear cookies&cache and restarting wamp
Both have exactly the same code in view page source from browser.
It seems to be issue with css file paths. Your css paths are relative.
make your css file path to be absolute.
Read this:-
https://css-tricks.com/quick-reminder-about-file-paths/
https://www.w3.org/TR/REC-CSS1/#url
http://jeffreybarke.net/2013/06/paths-and-urls-relative-and-absolute/
We're sending a zip file download as a response like this:
$this->response->file( "/export/stuff.zip", array('downlaod'=>true, 'name'=>"stuff.zip") );
return $this->response;
This works fine, BUT the file is always named export.zip. Our name option does not seem to have any effect. We've also tried without the .zip extension. This is confusing because the name options is shown here, in the docs.
What are we doing wrong?
Update:
We figured out that the seemingly arbitrary name "export" is being copied from the name of the controller action. We changed the method name to "admin_exportt" and then we get exportt.zip every time. This isn't documented anywhere that I've seen.
We found where the name is handled in the source code (/lib/Cake/Nework/CakeResponse.php:1254) and it appears that it should either use the original file name, or whatever is specified in the name options:
if (is_null($options['name'])) {
$name = $file->name;
} else {
$name = $options['name'];
}
Ugh! We figured out what was wrong...
Notice the word downlaod in the first line of my code above? That's the culprit. Apparently that bad option was causing the entire array to get ignored. I'm not sure if this will help anyone in the future, but I guess I'll leave it up as a reminder that CakePHP options work that way (at lease in this context).
PS: Whenever you're stuck, go take a walk and come back!
This is how I call the editor:
new nicEditor({
buttonList : ['bold','italic','underline','upload'],
iconsPath:'img/nicedit.png',
uploadURI : 'http://server.com/integracion/files/nicUpload.php'
}).panelInstance(textareaId);
And the .php file exists ( and I the one in the Docs, and I updated the target paths )
/* I want them here http://server.com/integracion/files/uploads/ so... */
define('NICUPLOAD_PATH', './uploads'); // Set the path (relative or absolute) to
// the directory to save image files
define('NICUPLOAD_URI', '/uploads'); // Set the URL (relative or absolute) to
// the directory defined above
But I on response when upload completes (and of corse an alert from nicedit..)
<script>
try {
top.nicUploadButton.statusCb({"error":"Invalid Upload ID"});
} catch(e) { alert(e.message); }
</script>
what am I missing?
-EDIT
I think the problem might be in the php file:
$id = $_POST['APC_UPLOAD_PROGRESS']; /* APC is installed and enabled */
if(empty($id)) {
$id = $_GET['id'];
}
FINAL EDIT:
I have managed to make this work!
Here is an working example:
http://simplestudio.rs/yard/nicedit/
Uploaded images are going to be stored here:
http://simplestudio.rs/yard/nicedit/images/
And here is the whole code, just unpack it and put on your server, mainly I needed to adjust nicEdit.js because it had some issues.
http://simplestudio.rs/yard/nicedit/nicedit.rar
Just make your code with that js file and by looking at my example, it will work :)
Also you need to have php APC installed so that this script can work:
http://php.net/manual/en/apc.installation.php
If you by any mean have some problems I am here to solve it.
I will not delete this example on my server so that everybody who have this issue can freely download it...
The code responsible for image upload is the method uploadFile, it is looking for uploadURI option parameter.
You will need to modify onUploaded event handler to parse your custom response instead of the imgur's one (sample). By default it expects at least {"upload": { "links": {"original": "http://..."}, "image": {"width": "123" } }}.
I'm sorry but I can't help with the FormData() handling server side with PHP.
For more information you can try out the demo page on the nicEdit web site using Firebug or WebInspector to snoop the network requests, and, of course, the source code.
I really like the cforms plugin for wordpress. It's very powerful, yet easy to use.
But today I ran into a dead-end.
Basically I need a form to let users submit their first- and last name, then upload two files, which I need to rename to "A_Firstname.Lastname.ext" and "B_Firstname.Lastname.ext".
By default cforms just leaves the file name as it was by the uploader.
If anyone is familiar with the cforms plugin then any guidelines would be much appreciated!
Thanks in advance.
You can change the filename by creating a my_cforms_logic function in my-functions.php (my_cforms_logic is called from line 270 of lib_validate.php if you want to understand why it works).
There is an example in the sample my-functions.php but even simpler example of adding test_ to the beginning of the given filename would look like this:
function my_cforms_logic($cformsdata,$oldvalue,$setting) {
if ( $setting == "filename" ){
return 'test_' . $oldvalue;
}
return $oldvalue;
}
So if you can figure how to access the values of the first and last name (I suspect something along the lines of $cformsdata['data']['Firstname'] will work) then you should be in business.
Phil