The contact form I'm working on uses an option to select to upload an attachment through file upload or relative file path located on the server. The first option has no issues, but the second won't work and sends without attachment.
I tried multiple combination but nothing seems working. How would I do this?
$mail->AddAttachment("cvs/test-docx_3094.docx", $name = 'test-docx_3094.docx', $encoding = 'base64', $type = 'application/octet-stream' );
$mail->AddAttachment("cvs/test-docx_3094.docx");
$mail->AddAttachment("http://mydomain.com/cvs/test-docx_3094.docx");
any input? Ive been working on this for so long, just cant get it done.
PLMK.
You need to use the absolute filepath.
To show what i mean i assume you have the following directory structure:
app/classes/mailer.php
app/data/test-docx_3094.docx
app/data/test-docx_3095.docx
You could use the following php to get the absolute path in mailer.php:
$relativePath = "cvs/test-docx_3094.docx";
$absolutePath = realpath(dirname(__FILE__) . '/../data/') . $relativePath;
EDIT
Considering your comment you have the following file structure:
website/
contents/
myplugins/
cvs/
test-docx_3094.docx
phpmailer.php
So with this the absolute path would be:
$relativePath = "cvs/test-docx_3094.docx";
$absolutePath = realpath(dirname(__FILE__) . '/cvs/') . $relativePath;
if your php script is located in the myplugins-folder.
Related
I have created a simple script to upload file in my WordPress plugin using
wp_handle_upload
In database only link to this image is stored. I would like to delete this uploaded file when i delete the post which it is linked to, however using
unlink()
does not work due to link structure which looks like this:
http://localhost/wp-content/uploads/2016/10/image.jpg
Does Anyone know the way to remove "http://[ip]/" from path or any WordPress method to remove uploaded file
I would be grateful for help.
You can use get_home_path() to get the root directory. Then your code would be:
$url = 'http://localhost/wp-content/uploads/2016/10/image.jpg';
$path = parse_url($url, PHP_URL_PATH); // Remove "http://localhost"
$fullPath = get_home_path() . $path;
unlink($fullPath);
I have an XML file that I use to create a PHP object. I then want to simply print out the image however I am getting the error 'Not allowed to load local resource'.
To get the image URL I have set a variable
$root = ( __DIR__ );
And then append that to the path in my XML file.
$parseXML = simplexml_load_file('chicken.xml');
$img_url = $parseXML->picture;
$imgg = $root . '/' . $img_url;
This now gives me the current path which is
C:\wamp\www\recipesUpdated/images/MarinoWebsite.jpg
If I copy and paste this into my browser it displays the image but won't work when I echo it in an img src.
Is there a way of getting the path just to
\recipesUpdated/images/MarinoWebsite.jpg
Without the C:\wamp etc ?
Thank you!
You get path to image on your local computer, and browser not allowed to load this path.
Url must contain web server address or be relative
Example:
http://localhost/recipesUpdated/images/MarinoWebsite.jpg
or
/recipesUpdated/images/MarinoWebsite.jpg
For your question, try to set variable $root to empty string or your web server address.
Today I am adding a custom code to wordpress page.php to manage form data including file upload. I just want to check the real page so I can give the path to upload the file in code.
I have this code to upload the file:
$target = "uploads/";
$target = $target . basename($_FILES['pass_doc']['name']);
//Here we check that $ok was not set to 0 by an error
move_uploaded_file($_FILES['pass_doc']['tmp_name'], $target);
$pass_doc = $_FILES['pass_doc']['name'];
But I don't know target path should I give I tried:
$target = "example.org/uploads/";
But its also not working for me. Please give any suggestions.
Thanks
The problem is, when you says uploads/, that will be always a relative path to your current directory. Use the wordpress wp_upload_dir() function. Check the documentation here
$upload_dir = wp_upload_dir();
$target = $upload_dir['baseurl'] . "/" . basename($_FILES['pass_doc']['name']);
I'm trying to use mime->addAttachment() to add an attachmen to an email i'm about to send
relative path: ../../clientdata/client1/attachments/file.txt
direct path: /home/hosting/site.eu/html/ssl/clientdata/client1/attachments/file.txt
script location: /home/hosting/site.eu/html/ssl/work/php_scripts/send_email.php
send_email.php is executed using:
exec("php /home/hosting/site.eu/html/ssl/work/php_scripts/send_email.php");
Is there something I'm missing here?
The code i'm using to add the attachment:
$f = 'ssl.site.eu/clientdata/client1/attachments/file1.txt';
$arr_file = explode('/', $f, 2);
$file_path = '/home/hosting/site.eu/html/ssl/'.$arr_file[1]; //this works
// $file_path = '../../'.$arr_file[1]; // this doesn't work;
$mime->addAttachment($file_path,'application/octet-stream');
Beware the working directory of your process.
Now does the following code give the currect path?
echo getcwd().$relative_path;
PHP getcwd()
if you include some PHP files that relative the file that codes execute,you must be sure that you include these files relative the process's current directory,but not the file contains current executing codes.
I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.