Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.