PHP download file code [customizable]
- Use the get method to pass in the http: // domain/download. php? parameter? File = 'savepath on server' & name = 'File name you want'
-
- // Input the file path and file name, which are used to read and rename the file respectively.
- If (empty ($ _ GET ['file']) | empty ($ _ GET ['name']) {
- Echo 'script alert ("no file found .....! "); Location. replace (" index. php ") script '; exit ();
- }
- // File path
- $ File = $ _ GET ['file'];
- // File name [that is, the file title, not the name saved on the server]
- $ Name = $ _ GET ['name'];
- // Obtain the file suffix
- $ Suffix = substr (strrchr ($ file, '.'), 1 );
-
- If (is_file ('./'. $ file )){
- Header ("Content-Type: application/force-download ");
- Header ("Content-Disposition: attachment; filename =". $ name. '.'. $ suffix );
- Readfile ('./'. $ file );
- Exit;
- } Else {
- Echo "the file does not exist! ";
- Exit;
- }
- ?>
|
PHP