Jump to content

downloading problem


sowmithrii

Recommended Posts

Am presently debugging the code snipped which someone has written, and am pretty new to php. just 15 days old.

 

I am facing few problems in the code.... mainly in the download section. i have gone through all the code realated to that section... but i dint find any problem

The actuall problem am facing is... the customers are supposed to download our companies software.. and the logic is working fine.. but suddenly the download page was getting blank

this download page contains the code which redirects to a function x, and that function sends the file to the user. in that function x... the previous coder has used @readfile($file_path)

to send the download...

is this occassional error is just becos of readfile function or some other... please help me guys...

 

Link to comment
Share on other sites

 

This is all the code... pls get me a solution///////////////////////////////////////////////////////////

 

 

function sendDownLoadFile($base_directory, $file_name)

{

$allowed_ext = array (

 

  'txt' => 'text/text',

 

  // archives

  'zip' => 'application/zip',

 

  // documents

  'pdf' => 'application/pdf',

  'doc' => 'application/msword',

  'xls' => 'application/vnd.ms-excel',

  'ppt' => 'application/vnd.ms-powerpoint',

 

  // executables

  'exe' => 'application/octet-stream',

 

  // images

  'gif' => 'image/gif',

  'png' => 'image/png',

  'jpg' => 'image/jpeg',

  'jpeg' => 'image/jpeg',

 

  // audio

  'mp3' => 'audio/mpeg',

  'wav' => 'audio/x-wav',

 

  // video

  'mpeg' => 'video/mpeg',

  'mpg' => 'video/mpeg',

  'mpe' => 'video/mpeg',

  'mov' => 'video/quicktime',

  'avi' => 'video/x-msvideo'

);

 

####################################################################

###  DO NOT CHANGE BELOW

####################################################################

// Make sure program execution doesn't time out

// Set maximum script execution time in seconds (0 means no limit)

set_time_limit(0);

// Get real file name.

// Remove any path info to avoid hacking by adding relative path, etc.

$fname = basename($file_name);

// Check if the file exists

// Check in subfolders too

function find_file ($dirname, $fname, &$file_path) {

 

  $dir = opendir($dirname);

 

  while ($file = readdir($dir)) {

    if (empty($file_path) && $file != '.' && $file != '..') {

      if (is_dir($dirname.'/'.$file)) {

        find_file($dirname.'/'.$file, $fname, $file_path);

      }

      else {

        if (file_exists($dirname.'/'.$fname)) {

          $file_path = $dirname.'/'.$fname;

          return;

        }

      }

    }

  }

 

} // find_file

 

// get full file path (including subfolders)

$file_path = '';

find_file($base_directory, $fname, $file_path);

if (!is_file($file_path)) {

  return false;

}

// file size in bytes

$fsize = filesize($file_path);

// file extension

$fext = strtolower(substr(strrchr($fname,"."),1));

// check if allowed extension

if (!array_key_exists($fext, $allowed_ext)) {

  return false;

}

// get mime type

if ($allowed_ext[$fext] == '') {

  $mtype = '';

  // mime type is not set, get from server settings

  if (function_exists('mime_content_type')) {

    $mtype = mime_content_type($file_path);

  }

  else if (function_exists('finfo_file')) {

    $finfo = finfo_open(FILEINFO_MIME); // return mime type

    $mtype = finfo_file($finfo, $file_path);

    finfo_close($finfo); 

  }

  if ($mtype == '') {

    $mtype = "application/force-download";

  }

}

else {

  // get mime type defined by admin

  $mtype = $allowed_ext[$fext];

}

// Browser will try to save file with this filename, regardless original filename.

// You can override it if needed.

$asfname = $fname;

// set headers

header("Pragma: public");

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: public");

header("Content-Description: File Transfer");

header("Content-Type: $mtype");

header("Content-Disposition: attachment; filename=\"$asfname\"");

header("Content-Transfer-Encoding: binary");

header("Content-Length: " . $fsize);

 

// download

@readfile($file_path);

}

Link to comment
Share on other sites

fixed

 

<?php

$curpath = dirname(__FILE__)."/";
sendDownLoadFile($curpath,"test.jpg");


function sendDownLoadFile($base_directory, $file_name)
{
$allowed_ext = array (

  'txt' => 'text/text',

  // archives
  'zip' => 'application/zip',

  // documents
  'pdf' => 'application/pdf',
  'doc' => 'application/msword',
  'xls' => 'application/vnd.ms-excel',
  'ppt' => 'application/vnd.ms-powerpoint',

  // executables
  'exe' => 'application/octet-stream',

  // images
  'gif' => 'image/gif',
  'png' => 'image/png',
  'jpg' => 'image/jpeg',
  'jpeg' => 'image/jpeg',

  // audio
  'mp3' => 'audio/mpeg',
  'wav' => 'audio/x-wav',

  // video
  'mpeg' => 'video/mpeg',
  'mpg' => 'video/mpeg',
  'mpe' => 'video/mpeg',
  'mov' => 'video/quicktime',
  'avi' => 'video/x-msvideo'
);

####################################################################
###  DO NOT CHANGE BELOW
####################################################################
// Make sure program execution doesn't time out
// Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);
// Get real file name.
// Remove any path info to avoid hacking by adding relative path, etc.
$fname = basename($file_name);
// Check if the file exists
// Check in subfolders too

// get full file path (including subfolders)
$file_path = '';
$file_path = find_file($base_directory, $fname, $file_path);
if (!is_file($file_path))
{
  return false;
}

// file size in bytes
$fsize = filesize($file_path);
// file extension
$fext = strtolower(substr(strrchr($fname,"."),1));
// check if allowed extension
if (!array_key_exists($fext, $allowed_ext)) {
  return false;
}
// get mime type
if ($allowed_ext[$fext] == '') {
  $mtype = '';
  // mime type is not set, get from server settings
  if (function_exists('mime_content_type')) {
    $mtype = mime_content_type($file_path);
  }
  else if (function_exists('finfo_file')) {
    $finfo = finfo_open(FILEINFO_MIME); // return mime type
    $mtype = finfo_file($finfo, $file_path);
    finfo_close($finfo); 
  }
  if ($mtype == '') {
    $mtype = "application/force-download";
  }
}
else {
  // get mime type defined by admin
  $mtype = $allowed_ext[$fext];
}
// Browser will try to save file with this filename, regardless original filename.
// You can override it if needed.
$asfname = $fname;
// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
@readfile($file_path);
}

function find_file ($dirname, $fname, &$file_path) {

  $dir = opendir($dirname);

  while ($file = readdir($dir)) {
    if (empty($file_path) && $file != '.' && $file != '..') {
      if (is_dir($dirname.'/'.$file)) {
        find_file($dirname.'/'.$file, $fname, $file_path);
      }
      else {
        if (file_exists($dirname.'/'.$fname)) {
          $file_path = $dirname.'/'.$fname;
          return $file_path;
        }
      }
    }
  }
return false;
} // find_file
?>

Link to comment
Share on other sites

hei,, man,.

 

    whats fixed... how u fixed that.. so simply.  can u explain me the error in my code. (am verymuch new to php...)

 

        [  i am calling  the function senddownloadfile($base_directory, $file_name) from different form ]

 

    let me tell u the flow , assume form1.php, form2.php. i have written function senddownloadfile(......) in form2.php and in form1.php am calling that function by passing parameters.

 

    and one more thing is,, my code is executing perfectly most of the time , but occassionally i am getting the error of blank display of the form2.ph

 

I have used smarty templates for the display purpose

 

 

Link to comment
Share on other sites

Main problem was here

 

<?php
$file_path = '';
find_file($base_directory, $fname, $file_path);

if (!is_file($file_path))
{
  return false;
}
?>

 

$file_path was always empty this always returning false (ending the script)

changes

<?php
$file_path = find_file($base_directory, $fname, $file_path);
if (!is_file($file_path))
{
  return false;
}

?>

 

also

function find_file returnd nothing if the file was found or not..

so

i changed to return $file_path;

 

Link to comment
Share on other sites

Thanks man,,.. it will take some time for me to test whether ur solution works fine for me or not.. it seems ur solution is correct..

 

  what does these two conditions mean in my code for function find_file()

 

  $file != '.'

  $file != '..'

 

thanks anyway..

Link to comment
Share on other sites

from the code i sent

<?php

$curpath = dirname(__FILE__)."/";
sendDownLoadFile($curpath,"test.jpg");

?>

 

Now

$curpath = dirname(__FILE__)."/";

is the current path (where the script is)

test.jpg = the file i am looking for, the download looks for this file in the current folder and all sub folders.. if none is found it will have a blank screen

to fix this

change

if (!is_file($file_path))
{
  return false;
}

to

if (!is_file($file_path))
{
  die("No file found");
}

 

also

you can change

if (!array_key_exists($fext, $allowed_ext)) {
  return false;
}

to

if (!array_key_exists($fext, $allowed_ext)) {
  die("Bad FileType");
}

 

 

as for the . and ..

. = parent folder

.. = back one folder

these exist in ever folder thus we exlude them from the search

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.