Skip to content

Instantly share code, notes, and snippets.

@alex-oliveira
Last active June 6, 2018 19:09
Show Gist options
  • Save alex-oliveira/62b64979149727170d229e069ce199a0 to your computer and use it in GitHub Desktop.
Save alex-oliveira/62b64979149727170d229e069ce199a0 to your computer and use it in GitHub Desktop.
delete after download
<?php
$folder = 'C:\Users\alexdeoliveira\Downloads\selemium';
$files = [];
$dir = opendir($folder);
while ($filename = readdir($dir)) {
if (!in_array($filename, ['.', '..'])) {
$files[] = $filename;
}
}
if (count($files) == 0) {
throw new \Exception('File not found.');
}
$name = $files[0];
$path = $folder . '/' . $name;
header("Content-Length:" . filesize($path));
header("Content-Disposition: attachment; filename=$name");
$input = fopen($path, 'rb');
$output = fopen('php://output', 'wb');
stream_copy_to_stream($input, $output);
fclose($input);
fclose($output);
foreach ($files as $filename) {
unlink($folder . '/' . $filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment