custom image uploading from frontend in magento code does not work - magento

i've problem in uploading images in my custom module in magento..
i used core php ccoding for this purpose but magento is not uploading image..
i used same code to upload file in my local xampp.
but that code is not working on my server..
i also use this code but it also not working....
$path = Mage::getBaseDir().DS.'customer_documents'.DS; //desitnation directory
$fname = $_FILES['docname']['name']; //file name
$uploader = new Varien_File_Uploader('docname'); //load class
$uploader->setAllowedExtensions(array('doc','pdf','txt','docx')); //Allowed extension for file
$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
$uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
$uploader->setFilesDispersion(false);
$uploader->save($path,$fname); //save the file on the specified path

I had added this line in my code and it's working..
<?php require_once ("lib/Varien/File/Uploader.php");?>
after this write this code
<?php
$path = Mage::getBaseDir().DS.'customer_documents'.DS; //desitnation directory
$fname = $_FILES['docname']['name']; //file name
$uploader = new Varien_File_Uploader('docname'); //load class
$uploader->setAllowedExtensions(array('doc','pdf','txt','docx')); //Allowed extension for file
$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
$uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
$uploader->setFilesDispersion(false);
$uploader->save($path,$fname); //save the file on the specified path
?>

Related

How to keep original image name in Laravel image file upload

I am trying to upload or update an image file.but this code is changing the name of original image into a hexa number.How can i keep the file name same in DB and my project images folder?
$image = $request->file('cus_image');
if(($image)){
$data=array();
$image_name=hexdec(uniqid());
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='images/';
$image_url=$upload_path.$image_full_name;
$success=$image->move($upload_path,$image_full_name);
$data['pic']=$image_full_name;
DB::table('customer')
->where('Cus_id',$Cus_id)
->update($data);
}
Change $image_name=hexdec(uniqid()); to $image_name = $image->getClientOriginalName();

Download zip folder based on form input - Codeigniter

I want to create search file form, where user search files based on year.I already made folder file in a years (ex. folder 2019, folder 2018, etc), so when user input value the results will show based on user input. I get the result that i want, but i can't download file as zip because the value og path folder is null. I already tried use input-> get and session-> set_flashdata, but the result still null. My question is how do I get the year value, so can direct to the path folder?
Note : tahun is years in english
Controller
public function download_zip() {
// Read files from directory
$tahun = $this->input->get('tahun');
if($this->input-post('but_createzip2') != NULL){
// File name
$filename = $tahun . _laporan.zip;
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}}
View
<form role="form" action="<?php echo base_url().'laporan'?>">
<input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
</form>
// Download
<?php echo form_open('laporan/download_zip'); ?>
You are posting dat using GET method then why are you made condition in POST method
public function download_zip() {
// Read files from directory
$tahun = $this->input->get('tahun');
if($this->input->get('tahun') != NULL){ // MODIFIED HERE YOU ARE PASSING VALUES USING GET METHOD
// File name
$filename = $tahun ."_laporan.zip";
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}
Try this code
You have a wrong form target, try to modify the codes be like below.
View :
<?php echo form_open('laporan/download_zip', ['method' => 'post', 'role' => 'form']); ?>
<?php echo form_hidden('but_createzip2','1');?>
<input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
<?php echo form_close(); ?>
Controller :
public function download_zip() {
// Read files from directory
$tahun = $this->input->post('tahun');
if($this->input-post('but_createzip2') != NULL){
// File name
$filename = $tahun . _laporan.zip;
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';
// Add directory to zip
$this->zip->read_dir($path, FALSE);
// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);
// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}

Remove unused Images/files from upload folder laravel

I have laravel5.4 application.I want to remove unused images/files from my upload folder which is not available in my database.
For example :
I have 50 images in my upload folder for user profile but some of the image not use for any user.i think he removed or update his image from frontend.
Yes i know we need to code to remove file when user update or remove profile picture at a time also delete from upload folder.but my app run from many time and i want to remove unused file using script not manually beacause i have lot's of files so it's hard to check and remove file manually.anyone can you please help me for create any function for remove file from folder.
Sorry for my bad English.
I use something like this in my AdminController to remove images by clicking on a button.
Maybe you need to change the path or extensions
public function deleteUnusedImages()
{
$file_types = [
'gif',
'jpg',
'jpeg',
'png'
];
$directory = public_path();
$files = File::allFiles($directory);
foreach ($files as $file)
{
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, $file_types)) {
if(DB::table('users')->where('votes', '=', $file)->count())
continue; // continue if the picture is in use
echo 'removed' . basename($file)."<br />";
unlink($file); // delete if picture isn't in use
}
}
}

Laravel display image from path which is in database

So use is uploading a logo and it's path is stored in a database like this:
C:\xampp\htdocs\laravel\public\logo\1496912432.jpg
I am displaying the image like this:
<img class="images" id="image" src="{{$business->image}}" />
However I get this error:
Not allowed to load local resource: file:///C:/xampp/htdocs/laravel/public/logo/1496912432.jpg
How can this problem be solved?
//edit
Controller:
public function image(Request $request) {
if($request->hasFile('img'))
{
$image = Input::file('img');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('logo/' . $filename);
Image::make($image->getRealPath())->fit(303, 200)->save($path);
$file = $request->file('img');
$session = session()->get('key');
$update_image = Business::find($session);
$update_image->image = $path;
$update_image->save();
return ['url' => url('logo/' . $filename)];
}
Use Laravel file() to store files https://laravel.com/docs/5.4/requests#files
Store the $path to your db
$path = $request->photo->store('logo');
the $request->photo is depending on your input file attribute name. In your case, it should be $request->img.
the above code will create a folder (if not exist), namely "logo" and store to that folder with random string file name.
Also check your configuration for file, located at /config/filesystem.php. Default is set to public
Use asset function to get the full path from public folder
<img class="images" id="image" src="{{ asset($business->image }}" />
You can do in two ways
Best way is update url path when image saving save url path to db
$path = $request->photo->store('logo'); // in 5.4
The other way if you can't changes db url you can do some hack like this
$file = explode('/public/', $business->image);
echo asset($file[1]);
You want to store all files inside the web root. Because of cross-domain security, you cannot access the file:// domain/protocol from a http protcol. By using Laravel to store and retrieve, it will come from the same host.

File upload in joomla module

I have been searching this for quite a while but couldn't find a solution to match my need. I am developing a module for Joomla 2.5 . I need functionality to allow users to upload images/any file type from the backend in module configuration.
Question : How can I add field for file upload in joomla module.
Thanks!
Just a sample from the joomla docs:
<?php
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', null, 'files', 'array');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
//Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
//First check if the file has the right extension, we need jpg only
if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
if ( JFile::upload($src, $dest) ) {
header("Location: http://".$_SERVER["HTTP_HOST"]."/administrator"); Redirect to a page of your choice
} else {
echo "error !"; //Redirect and throw an error message
}
} else {
echo "Wrong extension !"; //Redirect and notify user file is not right extension
}
?>
For more detailed information and full example(s): http://docs.joomla.org/How_to_use_the_filesystem_package
Keep in mind that your HTML form must include
enctype="multipart/form-data"
otherwise you will not get any result with the joomla function!

Resources