Jump to content

Save file by click


fluidsharp

Recommended Posts

Hello.

I'm not  understand how to save file from page. I'd like make link to file with save ability.

<?php

chdir('upload/');

foreach (glob("*.*") as $filename)

$uploadfile = $_GET['file'];

  readfile ('upload/'.$uploadfile);

  echo "<a href='upload.php? file=$uploadfile'> $filename </a> </p>";

}

?>

and msg appear:  Warning: readfile(upload/) [function.readfile]: failed to open stream: No such file or directory in ..

 

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/186293-save-file-by-click/
Share on other sites

i've changed:

$dir='upload';

echo "<a href= $dir/$filename > $filename </a><br>"; 

so when I focus cursor on link I see way: http://localhost:8080/upload/Credit.xls, but can't download files from directory (upload).

--------------------------

In php man I've found info about force the browser to display the save dialog.

There is string:  header('Content-Disposition: attachment; filename="downloaded.pdf"');

What about it ? Could you give me some suggestions\way for its task?

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/186293-save-file-by-click/#findComment-983842
Share on other sites

so... 8)

index.php:

<?php
error_reporting( E_ALL & E_STRICT );
$dir= "upload/";
foreach (glob(/*$dir.*/"*.*") as $file) {
   
echo "<a href='download.php? file=$file'> $file </a> </p>";  
}

download.php

<?php
error_reporting( E_ALL & E_STRICT );
$uploadfile = $_GET['file']; 
if (file_exists($uploadfile)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($uploadfile));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($uploadfile));


readfile($uploadfile);    
}
  ob_clean();
    flush();
       exit;
header('Location: index.php');   
?>

?>

 

Link to comment
https://forums.phpfreaks.com/topic/186293-save-file-by-click/#findComment-984321
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.