I have a button that runs the below code. The csv file writes fine, but for some reason it is not downloading. Nothing happens, no errors. I have used the force download before and it has worked fine.
Any ideas?
$csvContent = $this->dbutil->csv_from_result($query);
$date = date('U');
$filename = date('Y-m-d-Gis', $date);
if (!write_file("./reports/$filename.csv", $csvContent)) {
echo 'Unable to write the file';
} else {
echo 'File written!';
$data = file_get_contents("reports/$filename.csv");
force_download($filename, $data);
}
You should try $data = file_get_contents("./reports/$filename.csv"); instead ;)
Related
I'm trying to download the most recent file with the extension add.zip via ftp
I'll preface this by saying I am not well versed in php and have created this frankenstein piece of code from a scattering of other codes.
here is my code
<?php
// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');
// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');
if (!$login_result) {
// PHP will already have raised an E_WARNING level message in this case
die("can't login");
}
// get list of files on given path
$files = ftp_nlist($ftp, '/1668161919_DATA/*add.zip') or die("Could not find file");
$mostRecent = array(
'time' => 0,
'file' => null
);
foreach ($files as $file) {
// get the last modified time for the file
$time = ftp_mdtm($ftp, $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
ftp_get($ftp, "/home/mysite/public_html/wp-content/uploads/data-zipped/target.zip", $mostRecent['file'], FTP_BINARY);
ftp_delete($ftp, $mostRecent['file']);
ftp_close($ftp);
?>
After some testing
<?php
// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');
// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');
if ($login_result === TRUE) {
echo 'woot!';
} else {
echo 'doh!';
}
Returns a woot
but
// set up basic ssl connection
$ftp = ftp_ssl_connect('myremotesite.com');
// login with username and password
$login_result = ftp_login($ftp, 'username', 'password');
// get list of files on given path
$files = ftp_nlist($ftp, '/1668161919_DATA/*add.zip');
if ($files === TRUE) {
echo 'files found!';
} else {
echo 'doh! files not found';
}
?>
Cause a 504 error with no further information.
What am I doing wrong?
When I copy the URL to clipboard in FileZilla the data is sftp://username#myremotesite.com/1668161919_DATA
Am I even close to getting this right?
When I have tried using similar FTP file downloads in the past, it was with
ftp_connect
This is the first time I have been forced to use
ftp_ssl_connect
Ok, so I set up a testing environment and ran the script using ftp_connect instead of ftp_ssl_connect and the code worked fine.
I think I need to define my port as 22. But
$ftp = ftp_ssl_connect('myremotesite.com','22')
doesn't work. Can anyone assist?
i'm trying to store image to my DB using laravel with flutter
this is my flutter code:
and this is what i get in file.path:
here my laravel code:
public function store(Request $request){
dd(request->all);
$input =$request->json()->all();
$doctor = new AcceptDoctor();
echo "before if";
if ($request->hasFile('file')) {
echo "inside if";
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
$doctor->save();
return $doctor;
}
this is the output:
dd output:
the if statement not working and i try to echo the signature_path is null
i try the same code using html it's work
what should i do?
if ($request->hasFile('file')) {
$path = $request->file('signature_path')->store('images');
$doctor-> signature_path = $path;
}
This piece of code seems conflicting, first you check if file is available, and then you try to access signature_path. My guess, according to your error message, it should be:
if ($request->hasFile('file')) {
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
if this doesn't work, paste the code:
dd($request->all());
on the very first line of store method and let us know
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.
I have code written to allow someone to enter a date. if the submit date equals the file name then codeigniter should download the file to the desktop. The code shown makes sense to me but when I run it I get 'file was not found'. I can pull the file up on the server though and the name matches what is returned in the mysql database. My end goal is to be able to download the file with the same filename as my submission_date field
if($this->input->post('submit')) {
$trlr_num = $this->input->post('trlr_num');
} elseif($this->uri->segment(4)) {
$trlr_num = $this->uri->segment(4);
}
if( !empty($trlr_num)) {
$query = $this->trailer_model->select_trailer_draw($trlr_num);
$result =& $query->row_array();
if( !empty($result)) {
// Get filepath to bill
$directory = 'http://intranet.gmwdc.com/inc/img/inspection/upload';
$file = $directory.'/'.$result['submission_date'];
$this->load->helper('download');
if(file_exists($file)) {
$data = file_get_contents($file);
force_download($result['submission_date'],$data);
} else {
$this->session->set_status_data('error','File was not found.');
}
return;
} else {
$this->session->set_userdata('error','No Inspection Draw Found');
}
}
You might be referring wrong directory or file name. Also, it seems that $file is having no extension.
$file = $directory . '/' . $result['submission_date'] . $extension;
Edited
Instead of $directory try Codeigniter FCPATH which points to your Codeigniter installation directory.
$file = FCPATH . 'dir1/dir2/' . $result['submission_date'];
i am trying to check if a file exists, however it is in codeigniter behind /application/views folder.
Is there a way to check for this file OR do I need to move it outside the /application/ folder?
any help would be appreciated?
I am calling the filename from the controller like so:
public function event() {
$data['main_content'] = 'event';
$this->load->view('template', $data);
}
then in the file 'template.php' is where I am making the IF statement for IF file_exists then display that file otherwise do nothing:
<?php
$filename = $main_content;
$exists = file_exists('application/views/scripts' . $main_content);
var_dump($exists); // just for testing
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
if ($exists){
$this->load->view('scripts/'.$main_content.'');
} else{ }
?>
Try the following:
$filename = 'whatever';
$exists = file_exists('application/views/' . $filename);
var_dump($exists); // just for testing
Alternatively, try:
file_exists(realpath(APPPATH . '/views/' . $filename)));
I just tested both ways, both in a controller and a view, and they are working properly.