7 Replies - 1094 Views - Last Post: 14 October 2009 - 11:45 PM Rate Topic: -----

#1 indiguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-September 09

downloading files in current webpage problem!

Posted 05 October 2009 - 11:59 PM

<?php // Download files script, just need to change the current_dir for each section below if reuse this code.

  $current_dir = "$DOCUMENT_ROOT"."c:/wamp/www/joomla/Quality_Files/section1";	//Put in second part, the directory - without a leading slash but with a trailing slash!
  
  // declear html current directy pointer for the link.
  $public_dir = "/joomla/Quality_Files/section1";
  $dir = opendir($current_dir);		// Open the sucker
 
  echo ("<p><h1>List of available files:</h1></p><hr><br />");
  
  while ($file = readdir($dir))			// while loop
	{
	$parts = explode(".", $file);				   // pull apart the name and dissect by period
	if (is_array($parts) && count($parts) > 1) {	// does the dissected array have more than one part
		$extensions = end($parts);		// set to we can see last file extensions
	   
	if (preg_match('/docx*/i',$extensions) or preg_match('/pf*df*/i',$extensions)) // is extensions ext or EXT ?
			 echo "<li> <a href=\"$public_dir .\"/\". $file\" target=\"_blank\"> $file </a></li><br />";	// If so, echo it out else do nothing cos it's not what we want
		}
		
	}
  echo "<hr><br />";
  closedir($dir);		// Close the directory after we are done
?>




Problem:

This will list all the doc and pdf files in my directory.
However, upon clicking the file link, it will open a new page that mimics a linux look.. showing the files and showing the parent directory link to change directories.
I dont want this. I need it to open in the same page as the files are listed in (the formatted and designed webpage)

How to make the actual file open the browsers download thing when a user clicks on the file link?

Im new to this.
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: downloading files in current webpage problem!

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: downloading files in current webpage problem!

Posted 06 October 2009 - 05:39 AM

Personally I find something like this:
echo "<li><a href=".$public_dir."/".$file."> ".$file." </a></li>";
much easier to read and debug since teh variables themselves are not embedded within the text, but that is a personal choice.

There is nothing forcing you to have quotes around the href attribute, it is not a standard. Removing all the escaped characters can help resolve issues too, but single quotes would also achieve this.

My best bet is a mistake in the href portion, you have 2 periods between the path and filename which I believe is a mistake, also the target attribute is for opening a new tab/window and thus is not necessary so I removed it, but it shouldn't affect the working of the links and can be put back in.

Give it a try, if it still doesn't work, I'll actually run it and see if I can't solve it that way.
Was This Post Helpful? 0
  • +
  • -

#3 indiguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-September 09

Re: downloading files in current webpage problem!

Posted 07 October 2009 - 02:03 AM

wow, something so simply.
Thanks a lot, it work! yay.

Hey, it doesnt allow downloading of files with spaces in the name: "this is a doc.doc" =file not found. but its listed.
Do you know how to accept that?
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: downloading files in current webpage problem!

Posted 07 October 2009 - 02:32 AM

not sure but try this function with your file name htmlspecialchars()

:)
Was This Post Helpful? 0
  • +
  • -

#5 indiguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-September 09

Re: downloading files in current webpage problem!

Posted 07 October 2009 - 02:40 AM

  $current_dir = "$DOCUMENT_ROOT"."/  directory remove for this forum /";	 //Put in second part, the directory - without a leading slash but with a trailing slash!
  
  // declear html current directy pointer for the link.  
  $public_dir = "/ directory remove for this forum /";
  $dir = opendir($current_dir);		// Open the sucker 
 
  echo ("<p><h1>List of available files:</h1></p><hr><br />");
  
  while ($file = readdir($dir))			// while loop
	{
	$parts = explode(".",  $file);				   // pull apart the name and dissect by period
	if (is_array($parts) && count($parts) > 1) {	// does the dissected array have more than one part
		$extensions = end($parts);		// set to we can see last file extensions
	   
	if (preg_match('/docx*/i',$extensions) or preg_match('/pf*df*/i',$extensions)) // is extensions ext or EXT ?
			 echo "<li><a href=".$public_dir."/". $file."> ". $file." </a></li>";	// If so, echo it out else do nothing cos it's not what we want
		}
		
	}
  echo "<hr><br />";
  closedir($dir);		// Close the directory after we are done




Ive tried that, and urlencode($file). they just add a ' + ' in the white space, but same result. file not found on server.
I added it to the href link and tried in the explode areas..
Was This Post Helpful? 0
  • +
  • -

#6 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: downloading files in current webpage problem!

Posted 07 October 2009 - 02:53 AM

try to replace space with something else like str_replace(" ","+"); :)
Was This Post Helpful? 0
  • +
  • -

#7 indiguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-September 09

Re: downloading files in current webpage problem!

Posted 11 October 2009 - 09:03 PM

	 $file = $_GET['$file'];

				// start sending the headers
				// The name of the download file is stored in $file 
				header("Cache-Control: public"); 
				header("Pragma: public"); 
				header("Content-Description: File Transfer"); 
				header("Content-type: application/octet-stream");
				header('Content-Disposition: attachment; filename="'.$file');
				header("Content-Transfer-Encoding: binary");

				// start sending the file
				// The .pdf source is stored on the server as $file.pdf
				readfile($file); 




How would i add this (above) code in to the same php file so that it executes when a user click the href link as below?

Ive tried but when the page opens it automatically tries to download rubbish.. no files in the directory is listed!

 $current_dir = "$DOCUMENT_ROOT"."/section1/";	 //Put in second part, the directory - without a leading slash but with a trailing slash!
  
  // declear html current directy pointer for the link.  
  $public_dir = "/section1/";
  $dir = opendir($current_dir);		// Open the sucker  
 
  echo ("<p><h1>List of available files:</h1></p><hr><br />");
  
  while ($file = readdir($dir))			// while loop
	{
	$parts = explode(".", $file);				   // pull apart the name and dissect by period
	if (is_array($parts) && count($parts) > 1) {	// does the dissected array have more than one part
		$extensions = end($parts);		// set to we can see last file extensions
	   
	if (preg_match('/docx*/i',$extensions) or preg_match('/pf*df*/i',$extensions)) // is extensions ext or EXT ?
			 echo "<li><a href=".$public_dir."/". $file."> ". $file." </a></li>";	// If so, echo it out else do nothing cos it's not what we want
		}
		
	}
  echo "<hr><br />";
  closedir($dir);		// Close the directory after we are done


Was This Post Helpful? 0
  • +
  • -

#8 indiguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-September 09

Re: downloading files in current webpage problem!

Posted 14 October 2009 - 11:45 PM

SOLVED
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1