error with upload images in Attachments using Croogo and cakephp - image

Hope someone can help me!
I'm trying to upload an image in Attachments and insert image in a page using croogo.
And when I upload i keep getting the same error message:
Security Error
The requested address was not found on this server.
Request blackholed due to "auth" violation.
The images are saved in the uploads folder but i cant get them to display on the page.

Just add some line of code in your controller
public function afterFilter() {
if (($this->request->isPost() || $this->request->isPut()) && empty($_POST) && empty($_FILES)) {
$this->Security->csrfCheck = false;
}
}
OR
May be you forgot to define multipart in your form.
echo $this->Form->create('Something', array( 'type' => 'file'));
Hope this help you.

Related

How to display PDF Documents on the browser using a View in Laravel 5.8

I'm working on a web application using Laravel 5.8, I'm new to Laravel framework. I would like to display PDF documents on the browser when users click on some buttons. I will allow authenticated users to "View" and "Download" the PDF documents.
I have created a Controller and a Route to allow displaying of the documents. I'm however stuck because I have a lot of documents and I don't know how to use a Laravel VIEW to display and download each document individually.
/* PDFController*/
public function view($id)
{
$file = storage_path('app/pdfs/') . $id . '.pdf';
if (file_exists($file)) {
$headers = [
'Content-Type' => 'application/pdf'
];
return response()->download($file, 'Test File', $headers, 'inline');
} else {
abort(404, 'File not found!');
}
}
}
/The Route/
Route::get('/preview-pdf/{id}', 'PDFController#view');
Mateus' answer does a good job describing how to setup your controller function to return the PDF file. I would do something like this in your /routes/web.php file:
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
The other part of your question is how to embed the PDF in your *.blade.php view template. For this, I recommend using PDFObject. This is a dead simple PDF viewer JavaScript package that makes embedding PDFs easy.
If you are using npm, you can run npm install pdfobject -S to install this package. Otherwise, you can serve it from a CDN, or host the script yourself. After including the script, you set it up like this:
HTML:
<div id="pdf-viewer"></div>
JS:
<script>
PDFObject.embed("{{ route('show-pdf', ['id' => 1]) }}", "#pdf-viewer");
</script>
And that's it — super simple! And, in my opinion, it provides a nicer UX for your users than navigating to a page that shows the PDF all by itself. I hope you find this helpful!
UPDATE:
After reading your comments on the other answer, I thought you might find this example particularly useful for what you are trying to do.
According to laravel docs:
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
All you need to do is pass the file path to the method:
return response()->file($pathToFile);
If you need custom headers:
return response()->file($pathToFile, $headers);
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
Or if file is in public folder
Route::get('/show-pdf', function($id='') {
return response()->file(public_path().'pathtofile.pdf');
})->name('show-pdf');
then show in page using
<embed src="{{ route('show-pdf') }}" type="text/pdf" >

How to send blade as attachment in pdf format in laravel?

I want to send blade file as attachment in mail with laravel. I am
writing below code for this but its not working.
Below is my controller where i am getting data from form and then send
this data to another controller where my attachment function is
called.
$data = array('shareholders'=>$request->com_shareholder_count,'contract_send'=>$request->contract_send);
$to = $mail_log->to_email_id = $request->email_id;
$mail = Mail::to($to)->send(new SendMailable($data));
This is my SendMailable controller :
$director_info_pdf = view('directors_info',compact('data'))->render();
On return this variable it shows me error :
message: "Invalid view.", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\xampp\htdocs\caps_admin\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php"
line: 285
message: "Invalid view."
After this line i am writing code to attach my files. where i am sending some files other 3 files are directly send from folder. And last one is attached from blade file.
->attachdata($director_info_pdf, 'dynamic_data.pdf')
->attach( $public_path.'/'.'contract.pdf', [
'as' => 'contract.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'HMRC.pdf',[
'as' => 'HMRC.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'clientR3.pdf',[
'as' => 'contract1.pdf',
'mime' => 'application/pdf',
]);
I am able to send mail with all 4 files as attachment. But when i am trying to open my files in mail rest 3 files are working as pdf. but ->attachdata($director_info_pdf, 'dynamic_data.pdf') this file get corrupted.
I dont know how to first change this file into pdf and then send as attachment.
I am using snappy for pdf.
I think you want to send pdf as an attachment in your email without saving it in your system .
your can refer the following code to do this .
public function sendmail()
{
$data["email"]='useremail#gmail.com';
$data["client_name"]='user_name';
$data["subject"]='sending pdf as attachment';
//Generating PDF with all the post details
$pdf = PDF::loadView('userdata'); // this view file will be converted to PDF
//Sending Mail to the corresponding user
Mail::send([], $data, function($message)use($data,$pdf) {
$message->to($data["email"], $data["client_name"])
->subject($data["subject"])
->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
'mime' => 'application/pdf',
])
->setBody('Hi, welcome user! this is the body of the mail');
});
}
hope it helped you .
for better understanding you can follow this article
how to send pdf as an attachment in email in laravel
I dont know how to first change this file into pdf and then send as attachment. I am using snappy for pdf.
So that's the problem right there. You are attaching an HTML file, and simply naming it as if it's a PDF. That won't work. You do indeed need to generate this PDF from the HTML view that you render.
Here is the documentation for snappy:
https://github.com/barryvdh/laravel-snappy#usage
I suggest you to read up on it, as the examples given there are pretty straight forward and easy to understand.
This is untested, but if you've set up Snappy correctly, based on your code, something like this should work:
$snappy = App::make('snappy.pdf');
$director_info_pdf = $snappy->getOutputFromHtml(view('directors_info',compact('data'))->render());

php codeigniter uploading photos

So I was watching a video on how to upload photos using codeigniter. The link I used is here and the code is also at this site http://code.tutsplus.com/tutorials/codeigniter-from-scratch-file-uploading-and-image-manipulation--net-9452. I got everything to work however, when I tried to use the code on my own website I ran into a problem.
Basically all I changed was that I created a template for the view to be loaded in instead of just loading a single view. There is a controller Gallery.php file that looks like the follows.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery_view',$data);
}
}
I simply changed this code to. Really only changing the last line and replacing it with those new three bottom lines.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$var = $this->load->view('gallery_view',$data,true);
$data['center_content'] = $var;
$this->load->view('includes_main/template',$data);
}
}
Now I get an strange error that I do not understand and no error shows up in the console log. Here is a picture of the error. http://i.imgur.com/tsKNj9W.png. The error says "unable to load the requested file" then doesn't have any file name after it. then there is a .php at the bottom of the page. I just don't have a clue what is giving me this error. I checked my template over again and again, but don't see anything wrong.
My template is as follows. I commented out everything just to make sure nothing else was giving me this error. So I am just left with one line.
<?php $this->load->view($center_content); ?>
Thanks for reading. Sorry I just have been stuck on this for a while and still haven't been able to fix it.
In this case there there are many mistakes. you did not load upload library and you also did not set config array. you can find on codeigniter user guide how to upload a file. I recommend to you read this link to upload a file in codeigniter
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Wordpress submit post content from front-end with Ajax

I am generating my own Wordpress template and would like to give users a front-end form (so they don't have to log in to the wordpress dashboard) to submit some post content. I've got everything working, thanks to this tutorial - but I'd like to make one enhancement. Currently when the user presses submit the data is stored in wordpress and they are then redirected to a fixed URL of my choosing. Rather than this I would like to simply clear the form data and display a confirmation message - I guess via AJAX? I know there are built in AJAX capabilities in wordpress, but I've never really used it. Can anyone help me out?
The part of the code that submits the data to Wordpress and provides the URL to be redirected is copied below. I assume I wil need to make a change here?
$post_id = wp_insert_post($post_information);
if($post_id)
{
wp_redirect( '/thanks' );
exit;
}
You could simply redirect to the current page with get_permalink():
if ( $post_id )
{
wp_safe_redirect( add_query_arg( array( 'thanks' => '1' ), get_permalink() ) );
exit;
}
To display a message in the template, check the query arg:
if( isset( $_GET['thanks'] ) && '1' == $_GET['thanks'] )
echo '<p class="success">Thanks!</p>';
if you need more featuredone means, try this,
http://kvcodes.com/2014/02/front-end-post-submission-wordpress/

The url from an image via custom field (wordpress)

Am not even sure if this can be done but...
Ive added a feed from my forums to wordpress it works great but I need it to auto add the url of the image in a custom field from the images in the post (feed) first image would be fine as its only ahve a slider
Is there any way to do this?
Details
Ok I think I did not explain this very well so made a few screen shots
This is my slider at the minute with my
This is an imported post one other feed I was using
On this image you can see the custom field (which I have to fill in after every import)
Adding the image url into the custom field
and finaly a view of the slider working
This is what am trying to do (auto) so my feed from my booru / forums / 2 other of my sites and (2 other peoples) sites make my home page on a new site
Hope this explain it alot more
This uses the external Simple Pie library built into WordPress to fetch the feed, get the image url and create a new post for each item and save the image url as a custom field.
To activate the process we have to hook into wp_cron. The code below does it daily but it would probably be better to do it weekly to prevent overlap. Some overlap will probably occur so this still needs a way to check if we have already imported the image
First we need a function to save the custom field after the post has been created. This section comes from another answer I found on WordPress Answers.
Edit:
This needs to be wrapped in a plugin to schedule the cron event and the cron event was missing the action to make it fire.
Edit:
Final version below tested and it works but the feed the OP is getting is using relative url's so the domain name needs to be added somewhere in the output code.
<?php
/*
Plugin Name: Fetch The Feed Image
Version: 0.1
Plugin URI: http://c3mdigital.com
Description: Sample plugin code to fetch feed image from rss and save it in a post
Author: Chris Olbekson
Author URI: http://c3mdigital.com
License: Unlicense For more information, please refer to <http://unlicense.org/>
*/
//Register the cron event on plugin activation and remove it on deactivation
register_activation_hook(__FILE__, 'c3m_activation_hook');
register_deactivation_hook(__FILE__, 'c3m_deactivation_hook');
add_action( 'c3m_scheduled_event', 'create_rss_feed_image_post');
function c3m_activation_hook() {
wp_schedule_event(time(), 'weekly', 'c3m_scheduled_event');
}
function c3m_deactivation_hook() {
wp_clear_scheduled_hook('c3m_scheduled_event');
}
function create_rss_feed_image_post() {
if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://animelon.com/booru/rss/images'); // specify the source feed
}
foreach ($feed->get_items() as $item) :
// global $user_ID;
$new_post = array(
'post_title' => $item->get_title(),
'post_status' => 'published',
'post_date' => date('Y-m-d H:i:s'),
//'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
if ($enclosure = $item->get_enclosure() )
update_post_meta( $post_id, 'feed_image_url', $enclosure->get_link() );
endforeach;
}

Resources