jacek wrote:Okay. Well readfile() expects a file path, not an image ID. you you need to work out that path to the image and pass that in instead. it will probably just be a matter of adding the folder too.
my files are saved in the directory with its id not the name
i tried a different approach which is when the download link is clicked it creates a zip files and moves to downloads folder, and the file is downloaded form the directory
down_load.php
<?php
require('init.php');
if(isset($_GET['download'])){
include_once('func/zip.php');
$d_load = (int)$_GET['download'];
//----------------------
clear_folder();
if($row = check_owner_ship($d_load)){
//------------------------
if($link = make_zip($row)){
header('Location:'.$link);
}
//------------------------
} else {
if(!header('Location:'.$_SERVER['HTTP_REFERER'])){
header('Location: folders.php'); // Or where ever you want
}
}
//----------------------
} else {
if(!header('Location:'.$_SERVER['HTTP_REFERER'])){
header('Location: folders.php'); // Or where ever you want
}
}
function clear_folder(){
$files = scandir('downloads');
unset($files[0], $files[1]);
foreach($files as $file){
unlink('downloads/'.$file);
}
}
function check_owner_ship($d_load){
if($q = mysql_query("SELECT * FROM `files` WHERE `file_id`='{$d_load}' AND `user_id`=".$_SESSION['user_id'])){
return $row = mysql_fetch_row($q);
} else {
return false;
}
}
function make_zip($row){
$t = time();
copy('uploads/'.$row[2].'/'.$row[0].'.'.$row[4], 'downloads/'.$row[0].'.'.$row[4]);
$files_to_zip = array(
'downloads/'.$row[0].'.'.$row[4] );
//if true, good; if false, zip creation failed
if(create_zip($files_to_zip, 'downloads/'.$row[3].'.zip')){
return $link = 'downloads/'.$row[3].'.zip';
} else {
return false;
}
}
?>
zip.php
<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
?>
i cant seem to make it work can you please help me out