26468

Downloading a file from a PHP server via a website [closed]

<h3>Question</h3>
<b>Closed</b>. This question needs details or clarity. It is not currently accepting answers. <hr class="my12 outline-none baw0 bb bc-powder-2" /> <svg aria-hidden="true" class="svg-icon iconLightbulb" width="18" height="18" viewbox="0 0 18 18"><path d="M15 6.38A6.48 6.48 0 007.78.04h-.02A6.49 6.49 0 002.05 5.6a6.31 6.31 0 002.39 5.75c.49.39.76.93.76 1.5v.24c0 1.07.89 1.9 1.92 1.9h2.75c1.04 0 1.92-.83 1.92-1.9v-.2c0-.6.26-1.15.7-1.48A6.32 6.32 0 0015 6.37zM4.03 5.85A4.49 4.49 0 018 2.02a4.48 4.48 0 015 4.36 4.3 4.3 0 01-1.72 3.44c-.98.74-1.5 1.9-1.5 3.08v.1H7.2v-.14c0-1.23-.6-2.34-1.53-3.07a4.32 4.32 0 01-1.64-3.94zM10 18a1 1 0 000-2H7a1 1 0 100 2h3z"></path></svg> <p class="mb0"> <b>Want to improve this question?</b> Add details and clarify the problem by editing this post.

<p class="mb0 mt6">Closed 5 years ago.

</aside>

I have a physical files which I want users to download on my website. The files are located at:

C:/xampp/htdocs/myfile/uploads/*

I need a PHP script which can download files dynamically. Assuming I have the following link in my website which when clicked, will execute a script

<a href="magic.php"> Download file </a>

What PHP code do I need in the magic.php to download a file from the above path?


<h3>Answer1:</h3>

Download link

<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>

magic.php page

<?php $file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']); if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); exit; } ?>
<h3>Answer2:</h3>

Maybe you could do something like this: (Correct me if i am wrong)

<a href="uploads/<?php echo $row['name']; ?>" download="download">

来源:https://stackoverflow.com/questions/27668971/downloading-a-file-from-a-php-server-via-a-website

Recommend

  • GridLayout and number of rows and columns
  • get 2 letter state/province abbreviation
  • ReactJS component PropTypes - specify a function type with a set number parameters
  • How to read an SQL query generated by Dapper?
  • How do I search for a particular file in a directory using Perl?
  • mysqli_fetch_array starting the query on a certain row
  • Check if input is positive integer [duplicate]
  • Django: How to create form with ForeignKey field (input as CharField)
  • How to determine if a coordinate is inside SVG Close Path?
  • Embedding most recent YouTube video with Channel ID
  • JPA EntityManager and JavaFx [duplicate]
  • Thick border for rounded button in blackberry
  • How to install PyGObject through PyCharm
  • Cannot select Infragistics controls in WinForms designer
  • How to add a progress ring to the splash screen in Windows 8?
  • pass sessionid through jquery ajax call to php
  • Use Perl to Add GIF Image Other Than 8-bit to PDF
  • Application backgrounding in Xamarin.Mac
  • Can't perform CORS request using Angularjs
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Silverlight MVVM, stop SelectionChanged triggering in response to ItemsSource reset
  • Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php
  • Android NDK refer to external libraries in JNI
  • E: Malformed entry 1 in list file /etc/apt/sources.list.d/sbt.list (Suite)
  • Questions related to Garbage Collector and finalize() method
  • read.table returning character matrix, would like numeric
  • Remove stopwords and tolower function slow on a Corpus in R
  • Run git bash script in Windows 7/64bit
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • Use PHP to Replace HTML with HTML
  • Disabling swipe gesture in Windows Phone 8.1 pivot control
  • How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
  • Create .java file and compile it to a .class file at runtime
  • 'Edit' function for forum posts and such
  • Generate and export point cloud from Project Tango
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • concise way of flattening multiindex columns
  • JQuery: Infinite input select
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Apple Mach-O Linker error (“duplicate symbol”)