Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Is it possible to download a python file from a website?

I'm building a website where users can download various program data and scripts used on that data. I'm wondering if it's possible to download a python script directly? Or do I need to zip it or something?

Right now I list out each python script associated with a program as a link to download. However, the browser says "Failed - Server problem" when I click on the link. I used a test csv file in place of one of the python scripts (in the same folder on the server) and that downloaded successfully, so I have access to the folder where the scripts are.

Just looking to confirm- it's not possible to download a python script directly?

Here is the code in case it's helpful:

var site_url = "<?php echo site_url();?>";
var timestamp="";
var scriptsrendered='<table class="table table-striped">';
i=0;
   while (i<v.scripts.length) {
     if (v.scripts[i].fldTimestamp===null) {
         timestamp ="unknown date";
     }
     else {
         timestamp=new Date(parseInt(v.scripts[i].fldTimestamp)*1000);
     }
     scriptsrendered+='<tr><td style="width:150px;">'+timestamp+'</td>';
     scriptsrendered+='<td><a href="'+site_url+"nefin-temp/nefin_scripts/"+v.scripts[i].fldFileName+'" download="'+v.scripts[i].fldFileName+'">'+v.scripts[i].fldFileName+'</a></td></tr>';
     i++;
 }
 scriptsrendered+='</table>';
 $('#programScripts').html(scriptsrendered);

Edit:

I'm not seeing anything in the browser console as far as errors. Php logs are clean also. Apache error log has:

[Wed Jun 08 13:26:37.729794 2022] [fcgid:warn] [pid 65316:tid (104)Connection reset by peer: [client 132.198.100.190:38728] [request de6efb774b83e68d900a90abb805c0d9] mod_fcgid: error reading data from FastCGI server, referer https://dev.vmc.w3.uvm.edu/nefin-xana/extractor
[Wed Jun 08 13:26:37.729823 2022] [core:error] [pid 65316:tid [client 132.198.100.190:38728] [request de6efb774b83e68d900a90abb805c0d9] End of script output before headers: FEMCFHM_munger.py, referer https://dev.vmc.w3.uvm.edu/nefin-xana/extractor

The file is small, so shouldn't be a timeout issue. I'll see if I can figure out if the file type is blocked for download somewhere...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using the code from this answer (How to force file download with PHP) combined with a simple htaccess tweak you don't need to refresh the page.

Create a php file called downloader.php with the below code. You will want to do some file verification to make sure they are loading the correct files but for this example, this should work.

Modified the description from the original answer:

In the below example, we are getting the file name via the querystring. php://output is a write-only stream (generally used by echo or print). after that, you just need to set the required headers and call stream_copy_to_stream(source, destination). stream_copy_to_stream() method acts as a pipe which takes the input from the source stream (read stream) and pipes it to the destination stream (write stream) and it also avoids the issue of allowed memory exhausted so you can actually download files that are bigger than your PHP memory_limit.

$file = $_GET["file"];

$readableStream = fopen('nefin-temp/nefin_scripts/$page', 'rb');
$writableStream = fopen('php://output', 'wb');

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="$file"');
stream_copy_to_stream($readableStream, $writableStream);
ob_flush();
flush();

Then in your htaccess add this following line.

RewriteRule ^nefin-temp/nefin_scripts/(.*)$ downloader.php?file=$2 [L]

This will force the files from your scripts folder to be loaded into the downloader.php file.

Instead of htaccess, you can also just update your links like as follows:

change nefin-temp/nefin_scripts/ in the URls to downloader.php?file= and still use downloader.php

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!