I'm using dompdf on a drupal 8 site. I need to get the absolute URL for images i want to put inside.
In the method of a controller which generate my pdf, i am able to recover the path:
$path = PublicStream::basePath();
// $path returns : sites/default/files
$getpath = file_create_url($path);
// $getpath returns : http://localhost/drupal/sites/default/files
In fact I don't use default folder and i store everything in the specific site.
How can i retrieve 'sites/mysite/files' instead
Not very elegant but effective :
$host = \Drupal::request()->getSchemeAndHttpHost();
$host .= "/sites/mysite/files";
Related
I need to display a yaml file as an array but i but I can't display it.
I have create a service, and my controller call this service.
In my service i try to call my yaml like this :
$value = Yaml::parseFile('public\assets\organizations.yaml');
return $value;
But that return me that error :
File "public\assets\organizations.yaml" does not exist.
You are specifying the file with a relative path. This will not be resolved relative to the file it is in but relative to the current working directory you are in when executing the script. Since this depends on various factors it will always be troublesome.
Thus you should always use absolute paths. In symfony you can get the base path of your project via the kernel.project_dir configuration parameter.
Your code does not provide enough context to understand how or where you are using it. But if it is inside a controller extending AbstractController you can use getParameter():
$projectDir = $this->getParameter('kernel.project_dir');
$absolutePath = $projectDir . '/public/assets/organizations.yml';
$value = Yaml::parseFile($absolutePath);
return $value;
Also note that using \ as the directory separator won't work under Linux/Unix-like systems where / is used as directory separator!
Since / will work as directory separator under Windows, too, it is easiest to use it for cross-OS compatibility. Alternatively use the DIRECTORY_SEPARATOR constant.
Having a working WordPress installation, is it possible to have an image at an arbitrary URL on the WordPress site?
E.g. http://mysite.wordpress.com/this/is/an/arbitrary/path/image.png
you can use wp_upload_bits() function with some parameters.
here is an Example for that.
<?php
if(isset($_FILES['your-file-input']['name']) && sizeof($_FILES['your-file-input']['name'])){
$file = wp_upload_bits(sanitize_file_name($_FILES['your-file-input']['name']),null,file_get_contents($_FILES['your-input-file']['tmp_name']),'path/to');
print_r($file);// it returns an array of file information [url/size/type...]
}
I have Wordpress Plugin in which I try To display an uploaded image, but I get url like this:
http://localhost/opt/lampp/htdocs/wordpress2/wp-content/plugins/fileuploadplugin/uploads/website-development-banner.jpg
instead of this:
http://localhost/wordpress2/wp-content/plugins/fileuploadplugin/uploads/website-development-banner.jpg
My code is:
<?php
// Configuration - Your Options
$allowed_filetypes = array('.csv','.jpg','.gif','.png','.jpeg'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/wordpress2/wp-content/plugins' );
//$targetpath=WP_PLUGIN_DIR.'/testplugin/documents/'.basename($_FILES["photo"]["name"]);
$upload_path = WP_PLUGIN_DIR.'/fileuploadplugin/uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
//echo $upload_path;exit;
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
//$targetpath='/uploads'.basename($filename);
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file here'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.
?>
My Problem is how to remove /opt/lampp/htdocs from $uploadpath to get uploaded image to display correctly?
The problem is that you are using the same path for both uploading and displaying the image.
Path to Upload the image
In your upload code above, you need to use the file system path, as you are doing. The path in your code is:
$upload_path = WP_PLUGIN_DIR.'/fileuploadplugin/uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
This will have a value something like
/home/user/var/www/wp-content/plugins/fileuploadplugin/uploads/
URL to Display the image
The file system path obviously won't work to display the image, so you need to use a URL or a path relative to your website, e.g.
http://www.example.com/wp-content/plugins/fileuploadplugin/uploads/uploadedimage.jpg
To get the URL for your plugins folder, you need to use:
$upload_url = WP_PLUGIN_URL.'/fileuploadplugin/uploads/';
And then you can display the image using that path, e.g.
<img src="/<?php echo $upload_url.$filename; ?>" />
Note:
I have kept WP_PLUGIN_DIR and used WP_PLUGIN_URL in the code above to make it easier for you to see the changes required. However these should not be used directly. Instead you should use the functions provided by WP to get these values: ref Wordpress Codex: Determining Plugin and Content Directories.
I am not sure if this is an issue with my current setup, or what I want to do.
I have a module that is programatically creating nodes in my Drupal 6 site, and within each I have to provide links in between various nodes.
I basically have a few foreach loops, and within each I have the current path.
For instance:
foreach ($page->category as $category) {
$category_link = "category/" . $category['id'];
// generate category pages
...
$content = "<a href='$category_link'>".$category['name']."</a>";
_create_node($content);
foreach ($category->article as $article) {
$article_link = $category_link . "/article/" . $article['id'];
// generate article page
$content = "<a href='$category_link'>".$category['name']."</a>";
$content .= "<a href='$article_link'>".$article['name']."</a>";
_create_node($content);
}
}
The issue that I'm seeing is that the link seems to be continually built up.
For instance, in the main category pages it is fine (I'll see category/1234), and the article link will be fine, but the category link will seem to be longer than it should. Basically, I'll end up seeing:
category/1234/article/5678/category/1234
My first thought was to make use of $base_url and just create absolute paths, however whenever I try printing that variable from my module it is completely empty. This is on a local server, however when I move it to production Drupal isn't installed at the root, so I can't simply add a slash to the front of the link.
Try using $GLOBALS['base_path'] to get the base path.
$GLOBALS['base_path'] will work, but you are accessing a global variable that ALSO contains some things like your database connection info and some other important stuff. So with a slip of the finger you could muck up other things. I prefer base_path() which does the same thing but is a modicum safer.
Use
global $base_url;
For path to themes folder use
path_to_theme()
You can use base_path() but that will not provide you with the domain name.
Base url will provide you the complete url like : www.example.com
base_path() will give you : /
path_to_theme() will give you : sites/all/themes/yourthemename
My problem is almost like that of http://elrte.org/redmine/boards/2/topics/734?r=3250 which didn't get any solution.
I am using elfinder on my wordpress site in which on every user register I create a folder under the files directory on username and when that user comes to the interface where elfinder shows I want elfinder to show that user his user's folder as the root directory instead of the actual root set.
I found out through code that the root directory is set in connectors/php/connector.php file that passes it to elFinder.class.php file
I even found out a way to do that hardcoded... which is if you go into elFinder.class.php file and append the username with root in __contruct function everything works fine
public function __construct($options=array()) {
foreach ($this->_options as $k=>$v) {
if (isset($options[$k])) {
$this->_options[$k] = is_array($this->_options[$k])
? array_merge($this->_options[$k], $options[$k])
: $options[$k];
}
}
if (substr($this->_options['root'], 1) == DIRECTORY_SEPARATOR) {
$this>_options['root'] = substr($this->_options['root'], 0, 1);
} $this>_options['root'] .= 'username';
But the problem is that I am unable to find a way through which I can access username in elFinder way... It feels like there will be some simple way that my mind just ain't clicking to.. any help in this regard will be greatly appreciate.
Even if I can't access username in this function and somehow pass the username to this function or connector.php which calls its construct???
Modifying elFinder.class.php is a wrong way, what you need is to get your WordPress instance inside connector.php and generate correct root option for current user, the username/path you should get from WordPress.