Attachment downloaded in "File" format instead of pdf - pdf

I am sending an email with an attachment (pdf) through laravel.But the attachment when downloaded converts to "File" type instead of pdf...
Heres my code
$pdf_file_path = public_path() . '/user/invoices/invoice_' . $data['hp_transaction_id'] . '.pdf';
PDF::loadView('payment.payment-invoice', $data)
->setPaper('a4')
->setOrientation('portrait')
->setWarnings(false)
->save($pdf_file_path);
Mail::send('emails.view-property-transaction-completed', $data, function($message) use ($pdf_file_path, $transaction) {
$message->to(Auth::user()->email)->subject('Transaction Completed');
$message->attach($pdf_file_path, array('as' => 'Invoice-' . $transaction->id, 'mime' => 'application/pdf'));
});
I want the file to get downloaded in pdf format.What should i do for that?

Found the solution. Add .pdf with the file name in as attribute, like this:
$message->attach(
$invoice_path,
array('as' => 'Invoice-' . $transaction->id. ".pdf", 'mime' => 'application/pdf')
);

Related

File type is not validated

I'm trying to validate file type before saving it to database. This is my rule
array('picture', 'file', 'types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'maxSize'=>1024*1024/*1mb*/, 'tooLarge'=>Yii::t('default', 'File is too large'), 'on'=>'create'),
I'm trying to upload multiple images
foreach($_FILES["picture"]["name"] as $key=>$value)
{
$images->picture='file';
$images->IDbuyitnow=1;
if($images->validate())
echo $value." ";
else
echo "doesnt work";
}
HTML code
<?php echo CHtml::fileField('picture[]','',array('class'=>'form-control' ,'multiple'=>true, 'accept'=>'image/*')); ?>
I know I should use CMultiUpload but I wanted to make it like that. But it always validate it even if $images->picture="file" or "25.docx". var_dump($images->validate()) returns true
[SOLVED] Weirdest thing ever, attribute shouldn't be called "picture" but "image" instead.
FULL SOLUTION
HTML
<?php echo CHtml::fileField('image_file[]','',array('class'=>'form-control' ,'multiple'=>true, 'accept'=>'image/*')); ?>
CONTROLLER
$images=new BuyItNowImage;
$file=CUploadedFile::getInstancesByName('image_file');
foreach($fileas $key=>$value)
{
$images->image_file=$value;
$images->IDbuyitnow=1;
if($images->validate())
echo "ok";
else
echo "not ok";
}
MODEL
array('image_file', 'file', 'types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'maxSize'=>1024*1024/*1mb*/, 'tooLarge'=>Yii::t('default', 'File is too large')),

Yii preventing Apple Push Notification script from running

I have a script which does an Apple push notification to an App. The script is:
$payload['aps'] = array('alert' => 'This is the alert text',
'badge' => 1, 'sound' => 'default',
'article_id'=> '110'
);
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = '/var/www/schoolspace/apple_server/mountmercy/MountMercy-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$deviceToken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
I have created the required .pem. When I run this as a standalone script with:
php index.php
everything works and the app receives the notification. There is a warning however:
Warning: socket_close(): supplied resource is not a valid Socket resource
But it still works. However, when I run this in Yii, I get the error:
stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: Unable to set private key file `/var/www/schoolspace/apple_server/mountmercy/MountMercy-dev.pem'
I get this error even when setting YII_DEBUG to false. I run this segment of code in the my Yii "Message" model in afterSave(). It's EXACTLY the same as the standalone script. But somehow Yii is preventing it from running. Any ideas?
Since you are receiving the notification that means your pem file might be okay, but
your code doesn't show where you are providing the passphrase. You can try the below code which worked for me.
$badge = 1;
$sound = 'default';
$development = false;//change it to true if in development
$passphrase='passphrase';//pass phrase of the pem file
$payload = array();
$payload['aps'] = array('alert' => $msg_text, 'badge' => intval($badge), 'sound' => $sound);
$payload = json_encode($payload);
$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;
if($development)
{
$apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
else
{
$apns_url = 'gateway.push.apple.com';
$apns_cert = dirname(Yii::app()->request->scriptFile).'/file.pem';
}
$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);
$device_tokens2= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2 ) . chr(0) . chr(strlen($payload)) . $payload;
$msg=fwrite($apns, $apns_message);
#socket_close($apns);
#fclose($apns);

module overriding in zen cart

I am new to Zen Cart and currently working on a project which has custom template. I am now stuck on override of module file new_products.php !! how to override new_products.php ? I want to change HTML in it. how can I do it ? I have made changes in new_products.php itself. I know it is not right way to do it, but I am confused where do I make changes/override module file ?
Here is the code where I made changes in main new_products.php file
while (!$new_products->EOF) {
$products_price = zen_get_products_display_price($new_products->fields['products_id']);
if (!isset($productsInCategory[$new_products->fields['products_id']])) $productsInCategory[$new_products->fields['products_id']] = zen_get_generated_category_path_rev($new_products->fields['master_categories_id']);
$list_box_contents[$row][$col] = array('params' => 'class="centerBoxContentsNew centeredContent back"' . ' ' . 'style="width:' . $col_width . '%;"',
'text' => (($new_products->fields['products_image'] == '' and PRODUCTS_IMAGE_NO_IMAGE_STATUS == 0) ? '' : '<center><h2>'.$new_products->fields['products_name'].'</h2>' . zen_image(DIR_WS_IMAGES . $new_products->fields['products_image'], $new_products->fields['products_name'], IMAGE_PRODUCT_NEW_WIDTH, IMAGE_PRODUCT_NEW_HEIGHT) . '</center>') . '<h3>' . $products_price.'</h3><p>'.$new_products->fields['products_description'].'</p>
<div class="button black"><a href="' . zen_href_link(zen_get_info_page($new_products->fields['products_id']), 'cPath=' . $productsInCategory[$new_products->fields['products_id']] . '&products_id=' . $new_products->fields['products_id']) . '">Add To Cart' .'</div>');
$col ++;
if ($col > (SHOW_PRODUCT_INFO_COLUMNS_NEW_PRODUCTS - 1)) {
$col = 0;
$row ++;
}
$new_products->MoveNextRandom();}
It's perfectly fine to modify new_products.php, but first copy
includes/modules/new_products.php
to
includes/modules/YOUR_TEMPLATE/new_products.php
(where YOUR_TEMPLATE is the name of your custom template).
Then make changes in the file
includes/modules/YOUR_TEMPLATE/new_products.php
You can determine your template name by going to admin->tools->template selection.
If you don't know how to create a custom template in Zen Cart, read this:
http://www.zen-cart.com/content.php?180

Display images from protected folder

I have a protected folder within Yii and I'm looking to display some of those images within the site. I've tried the following code and it works within the site/index controller in that it returns just the image I wanted.
However when I tried to separate the code it didn't work. Any help is appreciated.
Model
public function getImage() // will take file identifier as #param
{
$imageID = '2562584569'; // will eventually be dynamically assigned
$image = Images::model()->find('tmp_name=:id', array('id' => $imageID));
$dest = Yii::getPathOfAlias('application.uploads');
$file = $dest .'/' . $image->tmp_name . '.' . $image->extension;
if(file_exists($file)){
ob_clean();
header('Content-Type:' . $image->logo_type);
readfile($file);
exit;
}
}
And in the view
CHtml::link('<img src="' . Yii::app()->request->baseUrl .'/images/image" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));
Thanks
Jonnny
"protected" folder are not accessible from the client browser. This prevents people to have access to important files, like your source code.
If you want to store images inside "protected" and want them to be accessible, you need to publish them using CAssetManager.
Usage is something like:
$path = Yii::app()->basePath.'/path-inside-protected';
$yourImageUrl = Yii::app()->assetManager->publish($path);
Yii will then use the file as an asset, coping it to the "assets" folder, sibling to "protected". After that, you can just use the url returned on your HTML.
<img src="<?php echo $yourImageUrl ?>">
I went about it like this
CHtml::link('<img src="' . $this->createUrl('/images/image', array('data'=>$data['data'])) . '" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));
Model
public function actionImage($data)
{
$image = Images::model()->find('tmp_name=:data', array('id' => $data));
$dest = Yii::getPathOfAlias('application.uploads');
$file = $dest .'/' . $image->tmp_name . '.' . $image->extension;
if(file_exists($file)){
ob_clean();
header('Content-Type:' . $image->logo_type);
readfile($file);
exit;
}
}
Thanks for all help

Video upload with Graph API from user's desktop

i am trying to upload a local video to Facebook through the Graph API.
Here is an example from Facebook:
http://developers.facebook.com/blog/post/493/
That works nice, and i get an JSON response after submitting the from.
How can I get the respone ID, for use in, e.g. Javascript or directly as an GET-parameter on my server?
I don't want to upload the video on an public accessable server and then redirect the video upload to Facebook by using curl or something like that.
Has anyone an idea or an example of how i can get this to work?
Edit:
Here is the example i used:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "YOUR_VIDEO_TITLE";
$video_desc = "YOUR_VIDEO_DESCRIPTION";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
?>
Taken from this page: http://developers.facebook.com/blog/post/493/
This is the whole response i get from the form (JSON response):
{
"id": "xxxxx19208xxxxx"
}
The problem is that the JSON response is inline in the page.
I thought to set a target in the form to an inline iframe but then i will not be able to access the JSON code.