I try to send Mail in a Magento CE 1.8.0.0,
I can send pictures et text files, but when i try to send pdf, it's always fail, my attachment size is 0 Ko and i can't open it ...
This is how i work :
if($filename != '')
{
$mailTemplate
->getMail()
->createAttachment(
file_get_contents(Mage::getBaseDir('tmp').'/pjcontact/'.$filename),
Zend_Mime::TYPE_OCTETSTREAM,
Zend_Mime::DISPOSITION_ATTACHMENT,
Zend_Mime::ENCODING_BASE64,
basename($filename)
);
}
On server, the pdf file is correct.
With this code, only PDF fails ... I'm on it since this morning and i find nothing, someone had an idea ?
My suspicion is that file_get_contents returns false. Try debugging it with:
if ( $filename != '' )
{
$path = Mage::getBaseDir( 'tmp' ) . '/pjcontact/' . $filename;
Mage::log("File found in {$path}? ".(file_exists($path)?"Yes":"No").". Is it readable? ".(is_readable($path)?"Yes":"No"));
$mailTemplate
->getMail()
->createAttachment(
file_get_contents( $path ),
Zend_Mime::TYPE_OCTETSTREAM,
Zend_Mime::DISPOSITION_ATTACHMENT,
Zend_Mime::ENCODING_BASE64,
basename( $filename )
);
}
Related
I have a method which is responsible for downloading a file.
$attachment = KnowledgeDatabaseAttachments::where('id', $id)->first();
if ($attachment) {
$filesPath = storage_path('app/knowledge_database_attachments');
return response()->download($filesPath . '/' . $attachment->physical_name);
}
After download, when I try to open it (this is an error message from my OS):
Could not load image '88ebb9c0-11af-11e8-b056-b1568dc848cb.jpg'.
Error interpreting JPEG image file (Not a JPEG file: starts with 0x0a 0xff)
File is saved like so:
$filesPath = storage_path('app/knowledge_database_attachments');
$physicalName = Uuid::generate() . '.' . $file->getClientOriginalExtension();
$file->move($filesPath, $physicalName);
KnowledgeDatabaseAttachments::create([
'knowledge_database_id' => $page->id,
'name' => $file->getClientOriginalName(),
'physical_name' => $physicalName
]);
File exist in that directory, and the downloaded file has correct size and name.
Funny part is that I can also create a newsletter which will include this file. When I create newsletter file is copied:
$extension = explode('.', $attachment->physical_name)[1];
$newPhysicalName = Uuid::generate() . '.' . $extension;
File::copy($attachment->getPathAttribute(), $storagePath . DIRECTORY_SEPARATOR . $newPhysicalName);
SendMailAttachments::create([
'mail_id' => $mail->id,
'filename' => $attachment->name,
'physical_name' => $newPhysicalName,
]);
And then, in the newsletter edit view I can as well download this file, with this (identical as above) method:
$attachment = SendMailAttachments::where('mail_id', $mailId)->where('filename', $attachmentName)->first();
if ($attachment) {
$filesPath = storage_path('app/sendmail_attachments');
return response()->download($filesPath . '/' . $attachment->physical_name);
}
And it works - file is correctly downloaded and I can open it.
Why I cant open file downloaded with first method?
I use Laravel 5.1 and Ubuntu 16.04 (if that matters).
EDIT
When I run file command on a downloaded file the result is data. When I run it on file in storage, the result is correct JPEG image data.
Try to add headers with response
View docs
$headers = array('Content-Type' => ' image/jpeg');
$filesPath = storage_path('app/knowledge_database_attachments');
return response()->download($filesPath,$attachment->physical_name,$headers);
Note: Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII file name.
The problem is that I have something being output before the image stream.
Temporary solution:
$response = response()->download($filesPath . '/' . $attachment->physical_name);
ob_end_clean();
return $response;
Permanent solution:
Find whats being output and remove it.
Found this here: https://laracasts.com/discuss/channels/laravel/image-is-being-thrown-as-a-white-blank-image?page=1
I am trying to move one image to new folder move_uploaded_file is returning 1 but the file is missing, I am working on localhost with XAMPP
$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],"\Images");
$name = basename($_FILES['arr']['name'][0]);
move_uploaded_file($_FILES['arr']['tmp_name'][0],'/images/' . $filename);
You have to add a destination for the file to go, including the filename that you wish to assign to it.
Refer to: http://php.net/manual/en/function.move-uploaded-file.php
You should specify the destination file name
if ( move_uploaded_file($_FILES['arr']['tmp_name'],dirname(__FILE__) . '/images/' . $_FILES['arr']['name'] ) ) {
echo "file uploaded";
} else {
echo "error in uploading";
}
I do not know why I am not able to delete a file in Laravel with the code:
$path = storage_path('app/identification_cards') . '/' . $filename;
Storage::delete($path)
The command is executed without errors and it returns true.
What I checked:
- the path is correct. If I use the same exact path in a bash terminal (with the "rm" command) the file is deleted;
- the file does have 777 permissions.
I don't know how to solve it.
Thanks.
Storage::delete will point to storage\app\ path so no need to add app folder name once again
Storage::delete('identification_cards/'.$filename);
use Illuminate\Support\Facades\File instead of Storage. The chunk of code working on my localhost right now:
$separatorLcl=DIRECTORY_SEPARATOR;// a '\' on win os, '/' on linux or whatever
$image = $request->file('userprofile_picture');
$filename = time() . '.' . $image->getClientOriginalExtension();
try {
Image::make($image)->resize(300, 300)->save( storage_path('app'
.$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename ) );
}catch ( \Exception $e){
l::k('fayli soxta nashud');
}
if($request->hasFile('userprofile_picture')){
l::k('$user2='.$user->name);//logging
$photo=$user->photo;
if(is_null($photo)){
$user->photo()->create([
'path'=>storage_path('app'
.$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename )
]);
}
else{
l::k($photo->path);//logging
try {
File::delete($photo->path);
}catch ( \Exception $e){
l::k('fayli photo nest');
}
$photo->path=storage_path('app'
.$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename );
$photo->save();
}
I am trying to write a script in Yii for downloading files. view file isecho CHtml::link('Major Oilseeds: World Supply and Distribution.','downloadpdf', array('class'=>'btn btn-darkorange')); and the controller code is $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
echo $path;
if(file_exists($path)){
Yii::app()->getRequest()->sendFile( 'Major_Oilseeds.csv' , file_get_contents($path) );
}
else{
echo '<br>File not found';
} the code echo $path dispalys the location as http://localhost/projectv2/download/Major_Oilseeds.csv and the download folder contains the file named "Major_Oilseeds.csv" but its always showing "File Not Found" Error. plz somebody help me to solve this. I have also tried the code $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
// $filename = 'Major_Oilseeds.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$path.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return; but its also not working :-(
file_exists can only work with local files. In your case you are trying to use file_exists with a URL.
You can find a workaround here: http://uk1.php.net/file_exists (please search for "http" in this page)
I'm stuck and I was hoping somebody here could help me.
I want to create a link to a file in a php script (the file is actually an image, a .tif image). I have Google d this relentlessly, to no avail.
The firewall is blocking external connections but I am running this query on a machine that mimics an internal connection. I currently use this machine to allow external user to run queries that hit the internal database so I know this works…
For instance this part works just fine:
$result = pg_query($conn, $query);
while($row=pg_fetch_assoc($result))
{
echo "<p>image details:<br />image: <u>" . $row['field'] . "</u> image date: <u>" . $row['field'] . "</u> image path: <u>" . $row['filename'] . "</u></p>";
The part of the query that I want to turn into a link is the $row[‘filename’], which is returned as
//host\path\path\path\path\path\path\path.TIF
… but now I want access to certain files associated with those queries. I want to turn this filename into a url that, when put into a link, goes to this file and opens it:
$imageURL = $row['filename'];
echo "<p>Using imageURL: <a href='$imageURL' border='0' target='_blank'>Click Here</a></p>";
Maybe this isn't possible.
I just can't seem to figure out how to fetch the image.
I do not have imagemagick or imagick and I don't want to go that route. Thanks in advance for any help.
EDIT
The machine I am running the query on is http://webapps.example.com
The machine I am querying (where the image resides) is not - the image path I am trying to return (as a link) is //hostipaddress\path\path\path\path\path\path\filename.TIF
You can use a proxy page which you can link to normally which does the job of rendering the image.
Let's call the proxy page readimage.php and it takes the filename or path as an argument.
Your link would look something like this:
<?php
$imageURL = $row['filename'];
echo "<p>Using imageURL: <a href='readimage.php?image=$imageURL' border='0' target='_blank'>Click Here</a></p>";
?>
readimage.php
<?php
$img = isset( $_GET['image'] ) ? $_GET['image'] : null;
if ( !$img )
return;
// Build internal file path
//$filepath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'myimages' . DIRECTORY_SEPARATOR . $img;
$filepath = $img;
//==========================
// VERY IMPORTANT TO WHITELIST THE PATH OR RESULT to make sure you're not showing unintended data
//==========================
if ( file_exists( $filepath ) ) {
// Find mime type
$mime = '';
// Try using the fileinfo functions. Requires PHP >= 5.3 and PECL 1.0
if ( function_exists( 'finfo' ) ) {
$finfo = new finfo( FILEINFO_MIME ); // return mime type ala mimetype extension
/* get mime-type for a specific file */
$mime = $finfo->file( $filepath );
}
// No mime yet? Try to use the deprecated mime_content_type() function
if ( !$mime && function_exists( 'mime_content_type' ) ) {
$mime = mime_content_type( $filepath );
}
// Not yet? Fallback to extensions :(
if ( !$mime ) {
$ext = pathinfo( $filepath, PATHINFO_EXTENSION );
switch ( $ext ) {
case "jpg" :
case "jpeg" :
case "jpe" :
$mime = "image/jpg";
break;
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
$mime = "image/" . strtolower( $ext );
break;
}
}
if ( $mime ) {
header( 'Content-type: ' . $mime );
readfile( $filepath );
}
}
?>
If you can have a proxy address that would allow you to send request outside, you could do it using curl.
** update 2
Image from curl
Save image from url with curl PHP
Curl behind a proxy:
curl_setopt($ch, CURLOPT_PROXY, "http://to.the.proxy.address");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "your password");