Uploading files with CodeIgniter - codeigniter

I am using CodeIgniter to download a file created by using the following code:
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=Pricemaster.csv');
echo "Category,Supplier,Org Name,Org Modelno,Capacity,Our Modelno,China,OEM + Freight,Cost Price,USD %,USD price,GBP price,INR %,INR Conversion rate,INR price" . PHP_EOL;
foreach ($data as $val) {
$imp = implode(',', $val);
echo $imp . PHP_EOL;
}
I want to save this file to my upload folder. How can I do this?

You can write files to the server by using the File Helper
Firstly load the file helper:
$this->load->helper('file');
Then try this:
$file_path = './uploads/file.php'
$output = "Category,Supplier,Org Name,Org Modelno,Capacity,Our Modelno,China,OEM + Freight,Cost Price,USD %,USD price,GBP price,INR %,INR Conversion rate,INR price" . PHP_EOL;
foreach ($data as $val) {
$imp = implode(',', $val);
$output .= $imp . PHP_EOL;
}
if ( ! write_file($file_path, $output)) {
echo 'Unable to write the file';
} else {
echo 'File written!';
}
You'll want to change the file name / path accordingly. The data will obviously be your CSV output.
If the file writes successfully you can use this path to attach to your email.

Related

LARAVEL - MULTIPLE FILE UPLOAD DOESNT WORK AND RETRIEVING

When I click on submit, it's only storing last selected file where it suppose to store all the images and how to retrieve those files? Any suggestion or example? Do I need to have different attributes for multiple files?
Controller
if (is_array($request->carEvidence)) {
foreach ($request->carEvidence as $key => $file) {
$destinationPath = public_path('image/');
$profileImage = $key . "-" . date('YmdHis') . "." . $file->getClientOriginalExtension();
$file->move($destinationPath, $profileImage);
$post['carEvidence'] = "$profileImage";
}
}
Views
<input type="file" name="carEvidence[]" multiple>
MYSQL
https://ibb.co/9sPLw8C
File Input
https://ibb.co/VHkxRwc
you are passing multiple files in array, so you have to use loop to retrieve all the files in controller.. something like
if (is_array($request->carEvidence))
{
foreach ($request->carEvidence as $key => $file) {
$destinationPath = public_path('image/');
$profileImage = $key."-".date('YmdHis') . "." . $file->getClientOriginalExtension();
$file->move($destinationPath, $profileImage);
// code to save in your db table
}
}

Change download filename codeigniter

Convert file name before force download.
I am trying to be able to convert the file name of the stored file name to the orignal file name before download
So when user clicks on a file to download instead of showing
post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
Like in image below
It will just rename the downloaded file something like
config.php
Question how to only change the downloaded filename when click on image.
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download($path . $stored_file_name, NULL);
}
How about that
(imho your foreach loop doesn't make any sense)
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download("your_filename.txt",file_get_contents($path . $stored_file_name));
}
Simple like that!
force_download(
$filenameWithFileExtension,
file_get_contents($fileToDownload),
mime_content_type($fileToDownload)
);
For this situation use force_download() something like this -
$file_name = "using md5 to convert it on normal text and stroe variable";
$file_path = "It's your file path, where have file in your drive"
$file_path = 'uploads/'.$path;
force_download($file_name, file_get_contents($file_path));
It's simple and working fine. Just need to store your file path and file name store in 2 different variables and pass them in force_download().
Hope it's work.
Good Luck.

Sitemap-XML in Processwire 3

how can i generate a sitemap for processwire 3 for huebert-webentwicklung.de/sitemap.xml it doesen't work with the Plugin MarkupSitemapXML. Any idea how to get it work?
Thanks.
Create a new page template (sitemap.xml) then set the page output to be XML the the PW backend. Create a page and link it (set it to hidden).
function renderSitemapPage(Page $page) {
return
"\n<url>" .
"\n\t<loc>" . $page->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" .
"\n</url>";
}
function renderSitemapChildren(Page $page) {
$out = '';
$newParents = new PageArray();
$children = $page->children;
foreach($children as $child) {
$out .= renderSitemapPage($child);
if($child->numChildren) $newParents->add($child);
else wire('pages')->uncache($child);
}
foreach($newParents as $newParent) {
$out .= renderSitemapChildren($newParent);
wire('pages')->uncache($newParent);
}
return $out;
}
function renderSitemapXML(array $paths = array()) {
$out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
array_unshift($paths, '/'); // prepend homepage
foreach($paths as $path) {
$page = wire('pages')->get($path);
if(!$page->id) continue;
$out .= renderSitemapPage($page);
if($page->numChildren) $out .= renderSitemapChildren($page);
}
$out .= "\n</urlset>";
return $out;
}
header("Content-Type: text/xml");
echo renderSitemapXML();

Listing files,subfolders and their files in a file in codeigniter

my Uploads folder structure in codeigniter is like
Uploads
-File1.txt
-File2.txt
-Subfolder 1
-- File1.txt
--File2.txt
-Subfilder2
--File1.txt
I want to display each file name and if that file is in subfilder it should display as Subfilder/File.txt
Thats what my code looks like
" <?php
foreach ($direcotry as $key => $value ) {
if(!is_numeric($key)){
echo $key . '/' ;
foreach ($key as $subKey => $value) {
echo $value . '<br>';
}
}
else{
echo $value . '</br>';
}
}
?>"
![enter image description here][1]
and it gives an error as invalid argument to foreach
CodeIgniter contains directory helper which provides this for you
$this->load->helper('directory');
$map = directory_map('path/to/uploads');
print_r($map)

Fineuploader: PHP randomized image name variable value

I am using Fineuploader for an ajax upload of changing a users profile picture, I have part of my upload.php file below:
function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
if (!is_writable($uploadDirectory)){
return array('error' => "Server error. Upload directory isn't writable.");
}
if (!$this->file){
return array('error' => 'No files were uploaded.');
}
$size = $this->file->getSize();
if ($size == 0) {
return array('error' => 'File is empty');
}
if ($size > $this->sizeLimit) {
return array('error' => 'File is too large');
}
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = #$pathinfo['extension']; // hide notices if extension is empty
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
}
$ext = ($ext == '') ? $ext : '.' . $ext;
if(!$replaceOldFile){
/// don't overwrite previous files that were uploaded
while (file_exists($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)) {
$filename .= rand(10, 99);
}
}
$this->uploadName = $filename . $ext;
if ($this->file->save($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)){
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
}
so if the image is uploaded with the same name, it creates a duplicate with a number so if asd.jpg is uploaded twice its asd44.jpg .. but I can not find how to make a variable to get the image path with random number created.
Only the initial name of the file that was uploaded. Does anyone know how to go about doing this and then making the update statement to send to database?

Resources