Generate a Download Page

Asked

Viewed 349 times

2

How do I generate a download page for users to click on a link or download button.

This is my remote upload script:

<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>

<?php

set_time_limit (24 * 60 * 60);

if (!isset($_POST['submit'])) die();

$destination_folder = 'files/';

$url = $_POST['url'];
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");

  if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}

if ($file) {
  fclose($file);
}

if ($newf) {
  fclose($newf);
}

?>
</center>
  • I didn’t understand very well, you asked about how to download but posted an upload code?! the location of that file is or will be stored in the database? Want to generate a download link after uploading? I suggest a better elaboration of the question.

  • this is the remote upload script and I want the uploads that make on it after the end of the upload appear a link to the download page, Type the 4shared or Mega.

  • It is out of focus what I will comment but to say that this is remote upload, I think wrong. What this script does is to read a page in a user-determined URL and write the content in a local file. There is no upload process in this, by the way, this above can be done with "2 lines of code" using file_get_contents() and file_put_contents().

1 answer

2

Ok, after uploading then you redirect to another address and pass by parameter the generated name for the file and in this script you do as follows:

<?php
  $file = "files/" . $_GET["arquivo"];

  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;
  }
?>

More information on: http://php.net/manual/en/function.readfile.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.