hi guys, just wanna know, how could i download a file using php script? it is possible with visual basic 6 but the problem is how would i do that using php???

To download just the single html file or store it in a variable then the following will do the trick:

<?
$webpage=file_get_contents('http://www.php.net/');
//now to save the variable to a html file
file_put_contents('index.html',$webpage);
?>

Hope that helps.

sir, what if i have several files, i mean i have excel, word, pdf and then different formats, how about that?

If you mean multiple url's then the following will loop through an array to save each of those files:

<?
$links=array('http://www.example.com/',
'http://www.example.com/test.doc',
'http://www.mysite.com/file.xls',
'http://www.example.com/webpage.html');
foreach ($links AS $link) {
$webpage=file_get_contents($link);
file_put_contents('saved_'.basename($link),$webpage);
}
?>

Also, I haven't tested the above script for errors just to let you know.

the thing is that it's already downloaded, what i want is that the user will have an option either open, save or cancel the download.

how can i do thaT?

Simply link to another page or same page where filesaving fails to pass if statement. Below is an example:

<?
if ($_GET['id']=='save') {
$links=array('http://www.example.com/',
'http://www.example.com/test.doc',
'http://www.mysite.com/file.xls',
'http://www.example.com/webpage.html');
foreach ($links AS $link) {
$webpage=file_get_contents($link);
file_put_contents('saved_'.basename($link),$webpage);
}
}
echo '<a href="index.php?id=save">Save files</a><br>';
if ($_GET['id']=='save') {
    echo '<a href="index.php"><b>Cancel Download</b></a>';
    }
?>

That should do the trick.

where did those files go sir?

Same folder as the script was placed.

savefile.php

<?php 
if(!$file) return false;
elseif(!$content) return false;
else { header("Content-disposition: attachment; filename=$file");
header('Content-type: $content');
readfile("$file"); }
?>
<a href='./savefile.php?file=filename&content=contenttype'>download filename</a>

keep a folder full of downloads
this script as index.php in that folder lets me dump anything in it and it autoupdates the displayed list
line 20 lets you set the filetypes it will handle

<?php ob_start("ob_gzhandler"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>forms</title>
<H4>Forms</h4>
<?php $p = split('/', $_SERVER['SCRIPT_FILENAME']);
$script_name = $p[count($p)-1];
$path = str_replace($script_name, '', $_SERVER['SCRIPT_FILENAME']);
$dir_handle = @opendir($path) or die("Unable to open $path");
Function get_Extension($m_FileName){
$path_parts = pathinfo($m_FileName);
if ($path_parts["extension"]) { $m_Extension = strtolower($path_parts["extension"]);
return(strtoupper($m_Extension));  		}
else { return "unknown file types"; }
 }
 function check_image($filename){
 $temp=strtoupper(get_Extension($filename));
 if(($temp=="PDF")||($temp=="XLS")||($temp=="DOC")) return (true);
 else return (false);
 }
 Function get_Files($path) {
if ($handle = opendir($path)) {	
while (false !== ($file = readdir($handle))) { 
if(!is_dir($file) && substr($file,O,1) != "."){ $m_Files[]=$file; 	}
}
 closedir($handle); 
}
 if(sizeof($m_Files)>1) asort($m_Files);
 return $m_Files;
 }
 $files=get_Files($path); 
 $filter_files=array_filter($files,"check_image");
 $maxnr=sizeof($filter_files)-1;
 sort($filter_files);
for ($i=0;$i<sizeof($filter_files);$i++){
echo "<div style='margin:4px;'><a href='./savefile.php?file=$filter_files[$i]'>";
echo substr($filter_files[$i], 0, strlen($filter_files[$i])-4);
echo "</a></div>";
 }
closedir($dir_handle); ?>
</body></html>
<?php ob_flush(); ?>

thank you very much sir to your help. i already 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.