I've created a file with fopen as so
$string = '<?php
define ("DB_HOST", "'. $_POST["dbhost"]. '");
define ("DB_USER", "'. $_POST["dbuname"]. '");
define ("DB_PASS","'. $_POST["dbpass"]. '");
define ("DB_NAME","'. $_POST["dbname"]. '")
?>';
$confile = "../lib/con.php";
if (!file_exists($confile)) {
$fp = fopen($confile, "x") or die("can't open file ".$confile);
fwrite($fp, $string);
fclose($fp);
} else {
$fp = fopen($confile, "w") or die("can't open file ".$confile);
fwrite($fp, $string);
fclose($fp);
}
However now if I try to download the file using Dreamweaver or Filezilla it won't download the file unless I rename it. Can anyone work out what I may be doing wrong here?
Con in a web server sense is considered a reserve, to some web hosts, and that when writing to a file named con, it sometimes fail, if you rename the the file to conn.php it should work.
Related
public function readPDF($file_id)
{
$file_id = SiteHelpers::encrypt_decrypt($file_id, 'd');
$file_details = $this->model->getFileDetails($file_id);
if($file_details){
$file = Storage::url('app/public/course/'.$file_details->course_id.'/'.$file_details->file_name.'.'.$file_details->file_extension);
print_r($file);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=document.pdf');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// #readfile($file);
return response()->file($file);
}
}
I have this function to display uploaded pdf file. I removed the former #readfile($file); as it does nothing and displays nothing. I did troubleshoot on my file path ($file) and the resulted url displayed pdf. But when I used latter function return response()->file($file); it displayed error that file doesn't exist but my $file string is correct. Could someone help me with that?
I have been going round and round with this. I have uploads working with the follow:
public function store(Tool $tool)
{
If(Input::hasFile('file')){
$file = Input::file('file');
$name = $file->getClientOriginalName();
$path=Storage::put('public',$file); //Storage::disk('local')->put($name,$file,'public');
$file = new File;
$file->tool_id = $tool->id;
$file->file_name = $name;
$file->path_to_file = $path;
$file->name_on_disk = basename($path);
$file->user_name = \Auth::user()->name;
$file->save();
return back();
}
however when I try to download with:
public function show($filename)
{
$url = Storage::disk('public')->url($filename);
///$file = Storage::disk('public')->get($filename);
return response()->download($url);
}
I get the FileNotFound exception from laravel
However, if I use this instead:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
I get
FileNotFoundException in File.php line 37: The file "use calib;
insert into
notes(tool_id,user_id,note,created_at,updated_at)
VALUES(1,1,'windows server 2008 sucks',now(),now());" does not exist
which is the actual content of the file...
It can obviously find the file. but why wont it download?
Try this:
return response()->download(storage_path("app/public/{$filename}"));
Replace:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
With:
return response()->download(storage_path('app/public/' . $filename));
response()->download() takes a path to a file, not a file content. More information here: https://laravel.com/docs/5.4/responses#file-downloads
If any one still could not find their file even though the file clearly exists then try
return response()->file(storage_path('/app/' . $filename, $headers));
It could be due to a missing directory separator or it isn't stored inside the public folder.
I am trying to build a small app that will take a multipart/form-data to send a form with two data items:
username - a text field
file - an image file
The app blocks are attached in the picture to this mail. I do not know how to send multipart/form-data with both files and text-based form data. Need your suggestions on this. Can you please help?
For those who are not acquainted with the Content-Type=multipart/form data, here is a one liner about it:
The content type "application/x-www-form-urlencoded" is inefficient
for sending large quantities of binary data or text containing
non-ASCII characters. The content type "multipart/form-data" should be
used for submitting forms that contain files, non-ASCII data, and
binary data.
More details can be found in:
https://www.w3.org/TR/html401/interact/forms.html#successful-controls
I have found ways to achieve it using get and put methods. I would want to achieve this using the Post method.
The php server code is:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$user = $_POST["username"];
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Upload failed with reason = $uploadFailureReason <br>";
}
else {
echo "Upload Sucessful <br>";
$pyscript = '/home/ubuntu/workdir/src/httppython.py';
$python = '/home/ubuntu/.virtualenvs/cv/bin/python';
$filePath = $target_file;
$cmd = "$python $pyscript --image $filePath --user $user";
//echo "command = $cmd ";
exec("$cmd", $output);
echo "<input type=button onClick=\"location.href='upload_new.html'\" value='New Upload'>"
}
?>
I am using CI version 3 with session driver as files. I just want to get session variables in some sub directory but print_r($_SESSION) not printing my saved session variables.
Can some one help how do i get session in sub-directory.
In addition, I have tried below and getting some variable checking set or not but don't know how to get all variables value from saved session files that I am saving in my tmp folder at root
if(isset($_COOKIE['ci_session'])) {
$file = '../tmp/ci_session'.$_COOKIE['ci_session'];
if(file_exists($file)){
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
//echo $contents;
$pos = strpos($contents, 'logged_in');
if ($pos === false) {
echo "Logged out";
} else {
echo "Logged in";
}
fclose($handle);
}
}
Change your config.php file configuration where you want to get your all session data
$config['sess_save_path'] = APPPATH.'test';
Above config will store all sessions in application/test directory
Here's the problem
I wanted to convert my data into csv format and download it. everything is fine, until the csv file that i had downloaded and there's a little bug on the file which there will be a space at the first line of the file.
for example
Before force download
"name","age",
"brad pit","40",`
After force download
"name","age",
"brad pit","40",
The csv file that i had downloaded and I try to open wit my excel will appear like this
"name" |age
brad pit|40
I believe that is because of the csv file that i had downloaded appeared an external space line in the first line of the data.
Here's the code
//write csv data
$data = $this->dbutil->csv_from_result($query, $delimiter);
//create random file name
$name = rand().'_salesperson_data_'.date('d-m-y').'.csv';
if ( ! write_file('./csv/'.$name, $data))
{
echo 'Unable to write the CSV file';
}
else
{
//perform download
$file = file_get_contents("./csv/".$name); // Read the file's contents
$filename = 'salesperson_data_'.date('d-m-y').'.csv';
force_download($filename, $file);
}
source of force_download()
if ( ! function_exists('force_download'))
{
function force_download($filename = '', $data = '')
{
if ($filename == '' OR $data == '')
{
return FALSE;
}
// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
if (FALSE === strpos($filename, '.'))
{
return FALSE;
}
// Grab the file extension
$x = explode('.', $filename);
$extension = end($x);
// Load the mime types
#include(APPPATH.'config/mimes'.EXT);
// Set a default mime if we can't find it
if ( ! isset($mimes[$extension]))
{
$mime = 'application/octet-stream';
}
else
{
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}
// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".strlen($data));
}
else
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($data));
}
exit($data);
}
}
i thought TRIM will be last solution for me and I try to put any where possible but is stil the same. I couldn't found any solution for this problem. Please help. this stuck me for 2days already.
Thanks in advanced.
I don't know if need to use CSV only, but a good plugin/function that I use is this one: http://codeigniter.com/wiki/Excel_Plugin/
It's works with CodeIgniter Query system for exporting stuff to Excel. I use it a lot and never have problems with it.
try to print on the browser, if you see some extra space then remove.
if the extra is still on the csv file when you download, then this extra space is coming from any of your include file.
when you start writing your code try not to leave some space on the top/bottom of the code.
Use ob_clean(); before writing CSV to remove White spaces.