Hello guyz,

I have this code bellow which is to download stored files on directory. I was using $reload=$_SERVER; for page reload and I was curious if would it be possible to use $_SERVER and reload php page two or more times?.. cause when I clicked on a link 'song title' I made, it does not download it.. I think my code for downloading the file is correct cuz i've tried it on the other page. any ideas please?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta charset="utf-8">
	<title></title>
	<head>
		<?php
			$reload=$_SERVER['PHP_SELF'];
		?>
	</head>
	<body>
		<form method='post' action="<?php echo $reload;?>">//first reload
			<p>Search:<input type='text' name='search'/>
					  <input type='submit' name='submit' value='search'/></p>
		</form>

<?php
	if(isset($_POST['submit']))
	{
		include 'dbconnect.php';
		$query="SELECT userfiles FROM userprofile";
		$result=mysql_query($query);
		$numrows=mysql_num_rows($result);
		$search=$_POST['search'];
		if($numrows>0)
			while($path=mysql_fetch_assoc($result))
			{
				$path=$path['userfiles'];
				
				if($dir=opendir($path))
				{
					while(($file=readdir($dir))!==false)
					{			
						if((filetype($path."/".$file))!="dir")
						{
							$found=stripos($file, $search);
							if($found!==false)
							{
								$reload=$_SERVER['PHP_SELF']."?file=$file&path=$path";
								echo "<br/><a href='$reload'>$file</a>";//second reload
							}
						}
					}
				}
			}
                 //I think the problem starts here. It didn't even get into the statement IF even though I have successfully set the file variable.
		if(isset($_GET['file'])) 
		{
			echo 'set';
			
			$file=$_GET['file'];
			$path=$_GET['path'];
			$path=$path."/".$file;
			header('Content-Description: File Transfer');
			header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename='.basename($path));
			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($path));
			ob_clean();
			flush();
			readfile("$path");
		}
	}
?>
	</body>
</html>

Thanks in advance...

Hello guyz,

I have this code bellow which is to download stored files on directory. I was using $reload=$_SERVER; for page reload and I was curious if would it be possible to use $_SERVER and reload php page two or more times?.. cause when I clicked on a link 'song title' I made, it does not download it.. I think my code for downloading the file is correct cuz i've tried it on the other page. any ideas please?

...					while(($file=readdir($dir))!==false)
					{			
						if((filetype($path."/".$file))!="dir")
						{
							$found=stripos($file, $search);
							if($found!==false)
							{
// This might also be a problem								$reload=$_SERVER['PHP_SELF']."?file=$file&path=$path";
								echo "<br/><a href='$reload'>$file</a>";
							}
						}
					}
				}
			}
                 //I think the problem starts here. It didn't even get into the statement IF even though I have successfully set the file variable.
		if(isset($_GET['file'])) 
		{
			echo 'set';
			
			$file=$_GET['file'];
			$path=$_GET['path'];
			$path=$path."/".$file;
			header('Content-Description: File Transfer');
			header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename='.basename($path));
			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($path));
			ob_clean();
			flush();
			readfile("$path");
		}
	}
?>
	</body>
</html>

Thanks in advance...

Hello,

I have inserted a comment into your code. You are generating a link URL for the file. URLs should be URL-encoded. Some browsers and some servers are picky about special characters inside the URL.

<?php
// ...
// This might also be a problem	

			$reload =$_SERVER['PHP_SELF'].'?file='.urlencode($file).'&path='.urlencode($path);
				echo "<br/><a href=\"$reload\">$file</a>";
// ...
?>

So this would be my proposal: urlencode file and path, put the HREF in double quotes.

Regards
Maba

One more thing I noticed, looking at your code: there is an even more fundamental problem. You are sending the headers after you have already started the output in line 1. Doing this will only work if your PHP.INI is configured to have an output buffer.

Otherwise you should get a "NOTICE" error telling you "Headers already sent in line 1 ..:".

So for your code to work independently of the PHP.INI setting for output buffering, you have to rearrange it.

<?php
   if (isset($_GET['file'])) {

// ...

?>
<html>
...

The sending of header must happen before any other output starts.

Regards
Maba

thanks.. got it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.