Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <!doctype html>
  2. <!--
  3. PHP Batch Download Script
  4.  
  5. @author Bastos <stef.des@laposte.net>
  6. @copyright none: free and opensource
  7. @link http://www.real-univers.fr/
  8. -->
  9. <html lang="en-US">
  10. <head>
  11. <meta charset="UTF-8">
  12. <title>Batch Downloader</title>
  13. <style>
  14. textarea {
  15. width: 100%;
  16. min-height: 10em;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>Batch Downloader</h1>
  22. <?php
  23.  
  24. //config
  25. error_reporting(E_ALL | E_STRICT);
  26. ini_set('display_errors','On');
  27. set_time_limit ( 180 ); //facultative: to avoid timeout
  28. ob_flush(); flush();
  29.  
  30. //recursive rmdir from php.net
  31. function delTree($dir) {
  32. $files = array_diff(scandir($dir), array('.','..'));
  33. foreach ($files as $file) {
  34. (is_dir($dir.'/'.$file)) ? delTree($dir.'/'.$file) : unlink($dir.'/'.$file);
  35. }
  36. return rmdir($dir);
  37. }
  38.  
  39. $errors = array();
  40. $urlString = '';
  41.  
  42. //a list of URLs to download was given
  43. if(isset($_POST['urls'])
  44. && isset($_POST['directory'])
  45. && strpos($_POST['directory'], '..') === false
  46. && strpos($_POST['directory'], '/') === false
  47. ) {
  48. ?>
  49. <ul>
  50. <?php
  51.  
  52. $urls = explode(PHP_EOL, $_POST['urls']);
  53. $directory = trim($_POST['directory']);
  54. if(empty($directory)) { $directory = 'files'; }
  55. $zipFile = $directory.'.zip';
  56.  
  57. //temporary directory to put the files into
  58. if(!file_exists($directory)) {
  59. mkdir($directory);
  60. }
  61. //temporary archive file for download
  62. if(file_exists($zipFile)) {
  63. unlink($zipFile);
  64. }
  65.  
  66. //try and download each file
  67. foreach($urls as $url) {
  68. $url = trim($url);
  69. $filename = basename($url);
  70. $success = copy($url, $directory.DIRECTORY_SEPARATOR.$filename);
  71. if($success) {
  72. echo '<li>'.$filename.' : OK</li>';
  73. } else {
  74. $errors[] = $url;
  75. echo '<li>'.$filename.' : KO (source = <a href="'.$url.'">'.$url.'</a>)</li>';
  76. }
  77. ob_flush(); flush();
  78. }
  79.  
  80. //zip files
  81. $zip = new ZipArchive();
  82. if ($zip->open($zipFile, ZIPARCHIVE::CREATE) === true) {
  83. foreach (glob($directory.'/*') as $file) {
  84. $zip->addFile($file);
  85. }
  86. $zip->close();
  87. }
  88.  
  89. //delete directory
  90. delTree($directory);
  91.  
  92. ?>
  93. </ul>
  94. <p>
  95. <?php echo count($urls); ?> file(s),
  96. <?php echo count($urls) - count($errors); ?> successe(s),
  97. <?php echo count($errors); ?> error(s)
  98. </p>
  99. <p>Download <a href="<?php echo $zipFile; ?>"><?php echo $zipFile; ?></a></p>
  100. <p>Errors :</p>
  101. <ul>
  102. <?php foreach($errors as $error) { ?>
  103. <li><?php echo $errors; ?></li>
  104. <?php } // end loop on errors ?>
  105. </ul>
  106. <?php } // end form post ?>
  107. <form method="POST" target="">
  108. <input type="text" name="directory" placeholder="Destination file...">.zip
  109. <textarea name="urls" placeholder="Put each URL on a different line">
  110. <?php
  111. foreach($errors as $error) {
  112. echo $error.PHP_EOL;
  113. }
  114. ?></textarea>
  115. <input type="submit" name="submitURLs" value="Download">
  116. </form>
  117. </body>
  118. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement