I'm with a problem I would like to share to see if I can provide help.
PHP Excel use Ubuntu and want guaradar the file generated in a specific folder of my project directory. I understand that looking information is made as follows, but it is not working properly.
My code is as follows:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Content-Disposition: attachment;filename="ReporteSuscriptores.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save('nameoffile.xls');
$filename = (__DIR__);
$objWriter->save($filename . '/report.xlsx');
Try other alternatives without success as the next block
$objWriter->save('nameoffile.xls');
And I can not save the file generated excel in a specific directory
Related
I have a web.py server hosted on pythonanywhere.com doing some handy things with python.
Now I'd like to just serve a straightforward html file from the same server i.e. just return the contents of a static html file to the client
The comments/answers below state that it should be possible, out of the box, to serve static files in the static directory, located in the same directory as the main python file which contains the following :
import web
urls = (
'/', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, Joe'
if __name__ == "__main__":
app.run()
The server above works fine, when I go to http://myhost/ it displays "Hello , Joe".
The directory static exists and contains a file small.jpg but when I try the url http://myhost/static/small.jpg it gives me "not found"
Previous text of question up to Nov 9th 2022 is below :
original question title : Trying to return a html file in web.py but getting "No template named ....." error message
So I've looked at the web.py documentation on serving static files and templating and I think the following code should work :
import web
render = web.template.render('static/')
# have also tried render = web.template.render('/full/path/to/static/')
urls = (
'/getlatlongEIRCODE', 'getlatlongEIRCODE', #other stuff
'/getlatlongGOOGLE', 'getlatlongGOOGLE', #other stuff
'/getmonthlyPV', 'getmonthlyPV', #other stuff
'/Tomas', 'Tomas',
)
class Tomas:
def GET(self):
return render.Tomas()
I have created a folder static at the same level as my file above (which works fine for the other scripts) and i have created a file Tomas.html in the static folder containing
<h1>Help me</h1>
However I get an error message when I go to https://example.com/Tomas
<class 'AttributeError'> at /Tomas
No template named Tomas
P.S. From the static files page it seems to say I should just be able to put the Tomas.html file in a folder called "static" and then access is via https://example.com/static/Tomas.html but that is not working (it returns "not found")
You're using a relative path to your template directory without paying attention to the working directory. See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/
You're working too hard. 'static' is built in.
As the documentation says, http://localhost/static/logo.png will return the file logo.png from the existing directory static, which is relative to your webserver root.
Do not use render() for this (not needed). Also, do not list your desired file ('/Tomas') in the urls list (not needed).
Anything under the static directory can be accessed with the url https://localhost/static/...
"static" is hardcoded in the web.py server, so you cannot (easily) change this to some other folder. The suggestion in the web.py documents is to have nginx or apache host your application and use an Alias there to go to web.py static. (I think you can also add StaticMiddleware to your web.py application, but you'd need to investigate that yourself -- look at web.application.run()
The case of the disappearing /static/ directory was related to the fact that I'm hosting on pythonanywhere.com
Even though the web.py documentation says that the /static/ folder is plugged in by default, that's not the case in pythonanywhere and you need to expressly make the link between the url http://yourhost/static/ and /path/to/static in the Web part of the dashboard.
Hello i have one more problem with deploying my app by Streamlit. It works localy but when I want to upload it on git hub it doesnt work..Have no idea whats wrong. It seems that there is problem with path to the file:
"File "/app/streamlit/bobrza.py", line 14, in <module>
bobrza_locations = pd.read_csv(location)"
Here is link to my github repo. Will be very very grateful for help. Thank in advance.
https://github.com/Bordonous/streamlit
The problem is you are hard coding the path of the bobrza1.csv and route.csv to the path on your computer so when running the code on a different environment the path in not legal.
The solution is to make location independent from running environment, for this we will use the following:
__file__ variable - the path to the current python module (the .py file).
os.path.dirname() - a function to get directory name from path.
os.path.abspath() - a function to get a normalized absolutized version of path.
os.path.join() - a function to join one or more path components.
Now you need to change your location and location2 variables in the code to the following:
# get the absolute path to the directory contain the .csv file
dir_name = os.path.abspath(os.path.dirname(__file__))
# join the bobrza1.csv to directory to get file path
location = os.path.join(dir_name, 'bobrza1.csv')
# join the route.csv to directory to get file path
location2 = os.path.join(dir_name, 'route.csv')
Resulting in an independent path of the bobrza1.csv and route.csv.
I want use fpdf in my project CI4..
how i can do this step by step?
How include it as thirdparty?!
<?php namespace App\Controllers\Purchase;
namespace App\ThirdParty\fpdf;
use CodeIgniter\Controller;
use FPDF;
?>
and how build the controller to call fpdf methods?!
$pdf = new PDF();
$pdf->AddPage();
thanks
I found a solution to this today.
Assuming your fpdf.php file is in the directory app\ThirdParty
1- make the first line of the file fpdf.php look like this
<?php namespace App\ThirdParty;
2- In your controller add the line
use App\ThirdParty\FPDF;
3- Now you can create your pdf file
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(1,13);
$pdf->AddPage('P','A4');
$this->response->setHeader('Content-Type', 'application/pdf');
$pdf->Output('test.pdf','F');
The file will be stored in the public directory of ci4.
I hope I've helped you.
I resolved this by using the old way (i.e adding fpdf directly in app/Controllers/);
Firstly, make sure you have the latest version of fpdf installed. Mine is 1.82 then unzip in app/Controllers.
require('fpdf.php');//to be done in your controller
$pdf = new \FPDF();
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(1,13);
$pdf->AddPage('P','A4');
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$this->response->setHeader('Content-Type', 'application/pdf');
$pdf->Output('test.pdf','F');
In case you are using XAMPP for linux kindly update fpdf in /opt/lampp/lib/php.
First include font folder, fpdf.php file and fpdf.css file in the ThirdParty folder.
Open fpdf.php file and include following 2 lines after <?php
namespace App\ThirdParty;
use Exception;
Then rename the fpdf.php file in ThirdParty folder to FPDF.php
Now you can import fpdf using:
use App\ThirdParty\FPDF;
I am importing products using the built in CSV import (System - import - products)
All my data is imported fine, but I get an error
Imported resource (image) could not be downloaded from external resource due to timeout or access permissions in rows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
In my css I have written the base_image path as ://mywebsite.ca/pub/media/import/img/hose/jpg/Ach7938.jpg
as a test I removed my folder structure, edited the csv and tried this and got same problem, same error http://mywebsite.ca/pub/media/import/Ach7938.jpg
I tried writing file path as /pub/media/import/img/hose/jpg/Ach7938.jpg
but it won't let me past the "check data" validator.
I confirmed the permissions on the folders and files is 775
I am using PHP 5.6, I tried 7 but it broke the whole site in many different ways, so that isn't an option
As a side note, if I run this 20 times in a row, the entire site hangs and import won't work again until I do a full system restore (whats up with that?)
Can I put the images on a different server and link to them that way?
Image path can be relative path or simple name. I've imported with both and it worked. In my case issue was the images given in path wasn't available in folder. I tried it with importing available and not-available image and was getting error in case when that image wasn't available.
Path can be any one ,it isn't must to use pub/media/import only.
For the import images issue i have fixed the issue from Uploader.php file. change below file path code and success working import images.
File path:
magento/vendor/magento/module-catalog-import-export/Model/Import/Uploader.php
Find the below line from line number 201:
$filePath = $this->_directory->getRelativePath($filePath . $fileName);
Replace with:
$filePath = strpos($filePath, $fileName) !== false ?
$filePath:$this->_directory->getRelativePath($filePath . $fileName);
For More information refer to this GitHub link:
https://github.com/magento/magento2/pull/20761/commits/8612789375b0c173f0ba852c587882af6ff8bc7f
The Images File Directory needs to be set relative to the Magento doc root.
For example if your product images are in pub/media/catalog/product
Set that as your Images File Directory.
How to create new file in a user directory on NetBeans Platform application? I used:
System.getProperty("netbeans.user", "user.home") + "/myfile");
But the NB IDE 7.1.1 told me that it is depreceated and I should use InstalledFile Locator instead. Ok, I tried this:
File file = InstalledFileLocator.getDefault().locate("myfile", null, false);
It works fine, if the file already exists. I cannot see any way, how to create new with the InstalledFileLocator. But the javadoc say, this method allows to get folder. So I tried this:
File file = InstalledFileLocator.getDefault().locate("myfile", null, false);
if (file == null) {
file = new File(InstalledFileLocator.getDefault().locate("", null, false), "myfile");
}
Again without success, the method locate now fails that it can't find anything (the "/" is forbidden and does not work too).
So my question is, how to corectly load in my NetBeans Platform application an existing file in the user directory (it is for writing also, so it should not be in the program directory) and if it does not exist, create it?
You could use Places.getUserDirectory().
File file = InstalledFileLocator.getDefault().locate("myfile", null, false);
if (file == null)
{
file = new File(Places.getUserDirectory() + File. separator + "myfile");
}
From the netbeans platform docs InstalledFileLocator should not be used to find resources on the system filesystem. To find data in the system filesystem, use the Filesystems API. Ex:
FileObject fo = FileUtil.getConfigFile(myfile);
if (fo == null) {
fo = FileUtil.getConfigRoot().createData(myFile,ext);
}
Probably the easiest thing you can do is to include a simple empty file (say "here.txt") in your module that will be installed in the user directory automatically. You can see an example of this here (see the section "Lessons learned: bundling files with your NetBeans modules").
Basically you include the file in the "release/modules/ext/here.txt" directory of your module.
When the module is installed the platform will install the 'here.txt' file included in your module in the user directory automatically for you, so you don't have to worry about this.
Once your module is installed an running you want to locate the file like this:
File hereTXT = InstalledFileLocator.getDefault()
.locate("modules/ext/here.txt",
"a.b.c",
false);
(Where "a.b.c" is your module identifier.)
And then from that 'hereTXT' file you can get the directory with 'hereTXT.getParent()', and you're all set.