Downloading a file with a different name to the stored name
Sure, use a Content-disposition header
header('Content-Disposition: attachment; filename="filetodownload.jpg"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');
Downloading a file with a different name to the stored name with ajax
You can't make the client download the file through an ajax request.
What you are doing here is reading the file server-side with PHP and returning his content to the caller script ( in fact you see the content in the "data" variable )
Without ajax, you can call the script like this:
<a href='path/to/your/phpscript.php' target='_blank' ><img src='../tools/telecharger.gif' /></a>
And it will send the file to the browser
EDIT: if you add the attribute target="_blank" to the anchor it will open the download in a new tab, without reloading the current page
How to download file with its original name instead of unique name?
I would just prefix the filename with the hash instead of replacing it. This way original filename is preserved. So your code would look like:
$file_name1 = md5(uniqid()) . '-' . $_FILES['file1']['name'];
// Example: b4b34d95b44ea3ebaa125508fd51dd44-image.jpg
On download, you could just remove this hash from the filename:
$file_name1 = substr($file_name1, 33);
// Example: image.jpg
And then set the filename like:
header('Content-Disposition: attachment; filename="' . $file_name1 . '"');
How can I rename a downloaded file in a for loop, when files have the same name?
Since you know the name of the download file, you can rename as you go. It can be tricky to know when a download completes, so I used a polling method.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
import shutil
download_file = os.path.expanduser("~/Downloads/Data.csv")
save_to_template = os.path.expanduser("~/Documents/Data-{}.csv")
# remove stale files
if os.path.isfile(download_file):
os.remove(download_file)
ChromeOptions=webdriver.ChromeOptions()
driver =webdriver.Chrome('Users/yu/Downloads/chromedriver')
countries = ['China', 'Malaysia', 'Brazil']
for country in countries:
inputcountry.send_keys(country)
inputcountry.send_keys(Keys.RETURN)
# one option is to poll for file showing up.... assuming file
# is renamed when done
for s in range(60): # give it a minute
if os.path.exists(download_file):
shutil.move(download_file, save_to_template.format(country))
break
else:
raise TimeoutError("could not download {}".format(country))
How to rename or keep the file name only while downloading the file entering a required key?
There are multiple ways to solve your problem. The most simple solution is to set the download
attribute without value
link.download = '';
This will use the final segment of the URL as filename.
download
Causes the browser to treat the linked URL as a download. Can be used with or without a value:
Without a value, the browser will suggest a filename/extension, generated from various sources:
- The Content-Disposition HTTP header
- The final segment in the URL path
- The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)
Defining a value suggests it as the filename. / and \ characters are converted to underscores (_). Filesystems may forbid
other characters in filenames, so browsers will adjust the suggested
name if necessary.
MDN
wget command to download a file and save as a different filename
Use the -O file
option.
E.g.
wget google.com
...
16:07:52 (538.47 MB/s) - `index.html' saved [10728]
vs.
wget -O foo.html google.com
...
16:08:00 (1.57 MB/s) - `foo.html' saved [10728]
Is there a way to tell a browser to download a file as a different name than as it exists on disk?
I'm stupid. Right in the Flask API docs it says you can include the parameter attachment_filename
in send_from_directory
if it differs from the filename in the filesystem.
How to set name of file downloaded from browser?
Can't find a way in HTML. I think you'll need a server-side script which will output a content-disposition header. In php this is done like this:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: inline; filename="filetodownload.jpg"');
In fact, it is the server that is directly serving your files, so you have no way to interact with it from HTML, as HTML is not involved at all.
Related Topics
Running a Zend Framework Action from Command Line
Avoid Public Folder of Laravel and Open Directly the Root in Web Server
Multiple Excerpt Lengths in Wordpress
Variable Type Hinting in Netbeans (Php)
Authorization Header Missing in PHP Post Request
How to Get a One-Dimensional Scalar Array as a Doctrine Dql Query Result
PHP Sessions in a Load Balancing Cluster - How
How to Handle User Input of Invalid Utf-8 Characters
Phpmyadmin Automatic Logout Time
How to Access a Property of an Object (Stdclass Object) Member/Element of an Array
Reverse Geocoding with Google Map API and PHP to Get Nearest Location Using Lat,Long Coordinates
Codeigniter Send Email with Attach File
Show/Hide Fields Depending on Select Value
How to Manage a Single PHP5 Session on Multiple Apache Servers