Download fisier prin URL

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
Stefan
Mesaje: 73

Download fisier prin URL

Salut, lucrez la un script de listare a fisierelor si directoarelor si am anumite optiuni ( imgur.com/a/PM1X9 ), iau totul pe rand si momentan am facut doar stergerea, cum as putea face sa downloadez un fisier prin url? P.s script ul este la inceput

COD:

Cod: Selectaţi tot

<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
		<script defer src="https://use.fontawesome.com/releases/v5.0.10/js/all.js" integrity="sha384-slN8GvtUJGnv6ca26v8EzVaR9DC58QEwsIk9q1QXdCU8Yu8ck/tL/5szYlBbqmS+" crossorigin="anonymous"></script>
		<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
	</head>

	<body class="container">
		<?php
		
		// echo "http://" . $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ;
		// echo dirname(__FILE__).'<br><br>';
		
		//Verificam daca link-ul este corect
		if(isset($_GET['actiune']) && !empty($_GET['actiune'])){
			
			//Preluam "actiune" si "director"
			$actiune  = $_GET['actiune'];
			$director = (isset($_GET['director']) ? $_GET['director'] : ".");
			
			//Daca parametrul 'director' este gol afisam eroare
			if(empty($director))
				die('Nu exista un director');
			
			if(isset($_GET["file"])){
				
				
				// COD PENTRU DOWNLOAD
			}
			
						
			//Daca actiune == listare ii atribuim lui $dir locatia actuala
			if($actiune == 'listare'){
				
		?>
			<table class="table table-hover">
					<tr>
						<th>
							<a href="http://
							<?php 
							echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
							
							if($director == '.'){
								echo '&director=';
								echo urlencode('./../');
							} else
								echo urlencode('/..');
							?>"><i class="fa fa-arrow-left"></i> Inapoi</a>
						</th>	
						<th>
							*
						</th>	
					</tr>
					<?php
					//Verificam daca fisierul este un director
					if(is_dir($director)){
						
						//Preluam fisierele fara "." si ".."
						$fisiere = array_diff(scandir($director), array('.', ".."));

						//Aranjam directoarele primele
						usort($fisiere, function($a, $b) {
							if(is_dir($a) == is_dir($b))
								return strnatcasecmp($a, $b);
							else
								return is_dir($a) ? -1 : 1;
						});

						//Afisam fisierele/directoarele
						$i=1;
						$j=0;
						foreach($fisiere as $denumire){
							
							//Preluam locatia fisierului/folderului
							$locatie = getcwd().$director.'\\'.$denumire;
					?>
					<tr>
						<th>
							<?php
							
								if(is_dir($locatie))
									echo '<i class="fa fa-folder fa-fw"></i> <a href="?actiune=listare&director='. urlencode("{$director}/{$denumire}") . '">'. $denumire .'</a>';
								else if(is_file($locatie))
									echo '<i class="fa fa-code fa-fw"></i> '.$denumire;
								else
									echo 'eroare';
									
							?>
						</th>
						<?php if(is_file($locatie)){ ?>
						<td>
							<div class="row">
								<button class="btn btn-info"><i class="fa fa-eye"></i></button>
								&nbsp
								<button class="btn btn-primary"><i class="fa fa-edit"></i></button>
								&nbsp
								<a href="<?php echo "http://" . $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'&file=' . urlencode($denumire) . '"'; ?>"><button class="btn btn-success"><i class="fa fa-download"></i></button></a>
								&nbsp
								<button class="btn btn-danger" data-toggle="modal" data-target="#sterge<?php echo $i; ?>"><i class="fa fa-trash"></i></button>
							</div>
							<div class="modal fade" id="sterge<?php echo $i; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
								<div class="modal-dialog" role="document">
									<div class="modal-content">
										<div class="modal-header">
											<h5 class="modal-title" id="exampleModalLabel">Stergere fisier</h5>
											<button type="button" class="close" data-dismiss="modal" aria-label="Close">
												<span aria-hidden="true">&times;</span>
											</button>
										</div>
										<div class="modal-body">
											Sunteti sigur ca vreti sa stergeti fisierul <b><i><?php echo $denumire; ?></i></b> ?
										</div>
										<div class="modal-footer">
											<button type="button" class="btn btn-secondary" data-dismiss="modal">Inchide</button>
											<button type="button" class="btn btn-danger">Sterge</button>
										</div>
									</div>
								</div>
							</div>
						</td>
						<?php $i++; } else if(is_dir($locatie)){?>
						<td>
							<div class="row">
								
								<button class="btn btn-primary"><i class="fa fa-file-archive"></i></button>
								&nbsp
								<button class="btn btn-danger" data-toggle="modal" data-target="#sterge<?php echo $j; ?>"><i class="fa fa-trash"></i></button>
								<div class="modal fade" id="sterge<?php echo $j; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
									<div class="modal-dialog" role="document">
										<div class="modal-content">
											<div class="modal-header">
												<h5 class="modal-title" id="exampleModalLabel">Stergere folder</h5>
												<button type="button" class="close" data-dismiss="modal" aria-label="Close">
													<span aria-hidden="true">&times;</span>
												</button>
											</div>
											<div class="modal-body">
												Sunteti sigur ca vreti sa stergeti directorul <b><i><?php echo $denumire; ?></i></b> ?
											</div>
											<div class="modal-footer">
												<button type="button" class="btn btn-secondary" data-dismiss="modal">Inchide</button>
												<button type="button" class="btn btn-danger">Sterge</button>
											</div>
										</div>
									</div>
								</div>
							</div>
						</td>
						<?php $j--; } else echo '<td>err act</td>';?>
					</tr>		
				<?php
						}
				
					}
				?>
			</table>
			
		<?php
			}

			//Stergere
			if($_GET['actiune'] == 'sterge' && isset($_GET['nume'])){
				$act = getcwd().$director.'\\'.$_GET['nume'];
				unlink($act);
			}

		} else {
			echo 'Nu se afișează nimic';
		}
		?>
	</body>
</html>

MarPlo
Cum adica sa descarci un fisier prin url?
Prin url poti sa iei continutul din browser returnat de la acea adresa url.
Prin url nu poti descarca fisiere de tip php, ci doar rezultatul returnat de fisierul respectiv.
Se pot descarca fisiere php (si alt tip) de pe acelasi server printr-un alt script php.
- Exemplu:

HTML

Cod: Selectaţi tot

<a href="download.php?dwl=file.php" title="Download File">Download File</a>
download.php

Cod: Selectaţi tot

function downloadFile($f){
  $finfo = finfo_open(FILEINFO_MIME_TYPE);
  header('Content-Type: '.finfo_file($finfo, $f));

  $finfo = finfo_open(FILEINFO_MIME_ENCODING);
  header('Content-Transfer-Encoding: '.finfo_file($finfo, $f));
  header('Content-disposition: attachment; filename="'.basename($f).'"');
  readfile($f);
  exit;
}

if(isset($_REQUEST['dwl'])){
  downloadFile(strip_tags($_REQUEST['dwl']));
}

Subiecte similare