PHPmailer file attachement not working [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Here is my email send php file , email sent but attachment is not working. File is there on that path . Issue is I need to get file name from a linux command and store in a variable . When I called that variable attachment is not working.
<?php
require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$file_name1 = shell_exec('sed "1q;d" /tmp/file.txt');
$file = "/home/user/.$file_name1";
echo $file;
//set Address data
$mail->Subject = "Acknowledgement Files ".date("m-d-Y:h:i:s")." ";
$mail->MsgHTML(" Acknowledgement Files are here attached");
$mail->AddAttachment($file)
if($mail->Send()) {
echo "Message sent!";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
If I am going to use full path like
$file = "/home/user/filetxt"
this works but I want to call variable and echo is display correct name.
any thoughts?

Put your variable outside the quotation, like this:
$file = "/home/user/".$file_name1;

So I found issue and solution as well. So when we run any command on linux likels /path/ output is result\n. It adds new line charactor as well. So I changed this line
$file_name1 = shell_exec('sed "1q;d" /tmp/file.txt');
to
$file_name1 = trim(shell_exec('sed "1q;d" /tmp/file.txt'));
to remove new line . AND now it works

Related

Problem with adding image files to my server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Basically, I am trying to put an image file into a folder in my server and add the dir in MYSQL BD. It successfully puts the file into the folder and adds the path link to the row in the table.
Now, the problem: when I try to download the file and show it, it downloads but nothing shows, so I check the file in the FTP and it turns out the file size is 0.1kb. What am I doing wrong and how do I fix it?
I've done some research on the issue and I might be crazy but I think I'm the only one having this issue because I couldn't get help anywhere.
here's my code;
$ImageData = $_POST['image_data'];
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageData));
// file_put_contents($UploadPath,$ImageData);
echo "Your Image Has Been Uploaded.";
}
N/B: I use base64_decode because I encoded it from android.
I expect the file to save to the server dir with the actual size.
Files are stored in $_FILES not $_POST.
Assuming that the field in your form is called image_data
$ImageTmpPath = file_get_contents($_FILES['image_data']['tmp_name']);
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageTmpPath));
echo "Your Image Has Been Uploaded.";
}
So thanks to #RiggsFolly and #Rust, files are actually stored with $_FILES not $_POST
but what actually solved my issue was the
file_put_contents($UploadPath,base64_decode($ImageData));
it is supposed to be
move_uploaded_file(base64_decode($ImageTmpPath),$UploadPath);

PHP: put_file_content not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a php-script which saves a pdf from a eclipse birt report to pdf.
I'm using get file content as imput.
The birt report pdf takes some time to create.
I think this is the problem.
In the following, the script:
<?php
$rname = 'reportname';
$wname = $rname . '_' . date('d.m.Y') . '.pdf';
$pdf = file_get_contents("http://xxx.xxx.xxx.x:8080/Birt/run?__report=" . $rname . ".rptdesign&sample=my+parameter&__format=pdf");
file_put_contents('/tmp/report' . $wname, $pdf);
?>
What is the problem?
Thanks for your help :)
Try to set a request timeout for file_get_contents
file_get_contents('http://www.example.com/', false, stream_context_create(Array("http" => Array("method" => "GET",
"timeout" => 600,
))));
Also check the default timeout
echo ini_get("default_socket_timeout");
file_put_contents() will return a numeric value (representing bytes written) if it succeeded with writing, and will return false when the write was not successful. If the write is not successful, the web server might not have permission to write to the directory. Make sure the directory is writable.
$result = file_put_contents('/tmp/report' . $wname, $pdf);
if( is_numeric($result) && $result > 0 ) {
// write was successful
} else {
// write was NOT successful
}
Relevant PHP doc: http://php.net/manual/en/function.file-put-contents.php

Copying files via system call fails on PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to copy some files from one directory on server to another, but it does not work.
Here is my code:
system('cp /var/www/site1/images/' . $row['imageUrl']. ' /var/www/site2/content/upload/content/item/mid/' . $row['imageUrl']);
$file = '/var/www/site1/images/'.$row['imageUpl'];
$newfile = '/var/www/site2/content/upload/content/item/mid/'.$row['imageUpl'];
if (copy($file, $newfile)) {
echo "success";
}
else
{
echo "failed";
}
Look at the copy() function here: http://php.net/manual/ru/function.copy.php

I need to create a file in my server and write some contents on it using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am using a cross domain request.A request has been made to php file in a server via ajax from another server.From php side Here, I need to create a file and write some contents in that file.My request is reaching perfectly.But I am not able to create a file.Please help
NOTE : ITS A CROSS DOMAIN REQUEST
<?php
$filename = "lin.txt";
$data2 = "lin IS HERE";
$newFile= fopen($filename, 'w');
chmod($newFile, 777);
fwrite($newFile, $data2);
fclose($newFile);
?>
Thanks in advance
In am not sure what are you going to do, because you haven't told us your code. I think this will help you:
<?php
$file = 'demo.txt';
$content = "Example\n";
file_put_contents($file, $content);
?>
The file is demo.txt (it is at the same location with the PHP document.
$content is the text which will be put on your txt file.
Hope it helps!

check if file exist in cPanel directory php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I got a problem with my code. It's returning false. I have searched and tried some similar questions here but none of them helped. anyway, im using cPanel here. and i'm sure that the file really exists and so with the folder names. Hope you can help me with this. thanks in advance.
<?php
$filename = 'event-01.jpg';
if ( file_exists( $_SERVER{'/home2/user/public_html'} . "/MyProject/events/event-01.jpg")) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
i have also tried
$filename = 'event-01';
if (!file_exists('http://mysite.com/MyProject/events/event-01.jpg')) {
echo "The file $filename doesn't exist";
}
Problem
To put it bluntly; this...
file_exists( $_SERVER{'/home2/user/public_html'} . "/MyProject/events/event-01.jpg")
is not php.
Solution
Check a file exists
Assuming that the file path is actually:
/home2/user/public_html/MyProject/events/event-01.jpg
Then you should just be using that in file_exists:
file_exists("/home2/user/public_html/MyProject/events/event-01.jpg")
Server root
I assume that what you actually meant to do was:
file_exists($_SERVER['DOCUMENT_ROOT']."/MyProject/events/event-01.jpg")
You might also like to try to var_dump($_SERVER) to see all of the information that it stores.
References
file_exists: http://php.net/file_exists
$_SERVER: http://php.net/manual/en/reserved.variables.server.php
In PHP, $_SERVER is a superglobal containing various server related information and unrelated to what you're trying to achieve.
Simply try this instead:
if (file_exists("/home2/user/public_html/MyProject/events/event-01.jpg")) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

Categories