How To Display Uploaded Image Correctly? - wordpress

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.

Related

Get original url of uploaded file by form element " managed_file" in Drupal

When I upload a file in Drupal via the Drupal\file\Plugin\Field\FieldWidget\FileWidget I'd like to get access to the information of the original file, but not the copy.
For example I have a file at the location /home/rnsrk/foo.txt and I upload it via a form element "managed_file".
If I dump the fileUri in the FileWidget::process function with
$files = $element['#files'];
$file = array_shift($files);
dpm($file->getFileUri());
I get only the file info of the file already uploaded and copied to the temp dir: public://2022-11/foo_1.txt.
How do I get the information of the original file from /home/rnsrk/foo.txt?

WordPress upload file

I would like to upload a file like as (.apk) but I can't. I receive this message.
Sorry, this file type is not allowed for security reasons.
I show solutions as to settings on multisite, but I don't have this choice on my version. In which way I could enable the settings of multisite?
Is there any way to upload files like apk,( may a plugin)?
Thank you in advance!
Open your functions.php file and add the following code inside it.
add_filter('upload_mimes', 'allow_custom_mimes');
function allow_custom_mimes ( $existing_mimes = array() ) {
// with mime type ‘application/vnd.android.package-archive
$existing_mimes['apk'] = 'application/vnd.android.package-archive';
return $existing_mimes;
}
Restart your server after adding code and check.

wordpress media upload to rename files appropriately if file exists

I'm creating a plugin in wordpress that uses the wp media uploader to upload files to the site. Problem is that if a file exists with the same name, the name of the file being currently uploaded is appended with a number at the end.
This is a problem if I upload file001.pdf and then the next file is renamed to file0012.pdf instead of file001-2.pdf
It's a problem because then the user may think that is file 12 and not version 2 of file 1.
How can i change that so if there's already a file in the system with the same name, the file being uploaded gets the right rename?
EDIT
So I found out there's a function in wp-includes/functions.php called wp_unique_filename which will check for unique file names and increment until the name is unique. I just need to find a way now to customize that function on the plugin directory.
WordPress provides one hook wp_handle_upload_prefilter as below
function handle_uploadedimage($arr) {
$random_number = md5(rand(10000,99999));
$ext = pathinfo($arr['name'], PATHINFO_EXTENSION);
$arr['name'] = $random_number .'.'.$ext;
return $arr;
}
add_filter('wp_handle_upload_prefilter', 'handle_uploadedimage', 1, 1);

Responsive filemanager in tinymce, directory settings

I'm using this file manager for file upload in tinymce.
File Manager
Having some problem with the directory. I'm using tinymce 4.
My filemanager folder is in "localhost/BAD/" directory,
I have a file named about.php which is in "localhost/BAD/admin/" directory. Now I can upload image and see from that about.php file. My settings of tinymce is
**external_filemanager_path:"/BAD/filemanager/",
filemanager_title:"Filemanager" ,
external_plugins: { "filemanager" : "filemanager/plugin.min.js"}**
And my cofig.php file settings are
$base_url="http://localhost";
$upload_dir = '/BAD/source/';
$current_path = '../source/';
$thumbs_base_path = '../thumbs/';
After uploading image and selecting,it shows a path in the image source box as "../source/eng.jpg" and saved that into database.
But I want to access this image from "localhost/BAD/" directory NOT "localhost/BAD/admin/". It starts to find the source file on 'localhost', Not in 'localhost/BAD'
I tried some changes but not working. How can I save the source link as "source/eng.jpg" instead of "../source/eng.jpg" or is there any way to save the image from one directory and access it from previous level directory?
Solution:
In tinymce init:
relative_urls:false,
external_filemanager_path:"/BAD/filemanager/",
filemanager_title:"Filemanager" ,
external_plugins: { "filemanager" : "/BAD/filemanager/plugin.min.js"}
and in config.php
$base_url="http://localhost";
$upload_dir = '/BAD/source/';
$current_path = '../source/';
$thumbs_base_path = '../thumbs/';

File api :: Recieveing file name instead of browse,drag,droop

I am using Html file Api to read a file.Using i read it.it will show a browse button and select the file and read ..in this manner.
But instead of the above i want to manually set one default file to read every time.With out changing the code how can it possible.
In file Api it read the file from browser using ----
function resetFileUploader() {
getElem("uploader").innerHTML= '<input id="filechooser" type="file" />';
getElem("filechooser").addEventListener("change", getFile, false);
}

Resources