Problem with opendir() and readdir() on server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Problem with opendir() and readdir() on server

Post by amitdubey2 »

hello all,

I am creating a file download manager for a site.Every thing goes fine when I test it on localhost on my machine.But when I put the code for server the opendir() and readdir() function produces a warning.I have searched on net and came to know that it is some permission problem for the directory.My file(download.php) is in the file_upload folder which comes under the public_html.
It is showing these warnings
Warning: opendir(file_upload/) [function.opendir]: failed to open dir: No such file or directory in /public_html/file_upload/download.php on line 98

Warning: readdir(): supplied argument is not a valid Directory resource in /public_html/file_upload/download.php on line 100
File does not exist. Make sure you specified correct file name.


i have tried all path combinations for this but didn't get the things properly.
This is the code which i am using for file download.

Code: Select all

<?php
include'session.php';

  



define('ALLOWED_REFERRER', '');

// Download folder ( MUST end with slash (i.e. "/" )
define('BASE_DIR','/public_html/file_upload/');

// log downloads?  true/false
define('LOG_DOWNLOADS',true);

// log file name
define('LOG_FILE','downloads.log');

// Allowed extensions list in format 'extension' => 'mime type'

$allowed_ext = array (


  //text
  'txt' => 'text/plain',
  
  
  //for codes
  //'html'=> 'text/html',
  //'php' => 'text/php',

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

  // documents
  'pdf' => 'application/pdf',
  'doc' => 'application/msword',
  'docx'=>  ' ',
  '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
  'flv'  => ' ',
  'mpeg' => 'video/mpeg',
  'mpg' => 'video/mpeg',
  'mpe' => 'video/mpeg',
  'mov' => 'video/quicktime',
  'avi' => 'video/x-msvideo'
);




if (ALLOWED_REFERRER !== ''
&& (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false)
) {
  die("Internal server error. Please contact system administrator.");
}

set_time_limit(0);

if (!isset($_GET['f']) || empty($_GET['f'])) {
  die("Please specify file name for download.");
}

// Get real file name.

$fname = basename($_GET['f']);

// 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;
        }
      }
    }
  }

} 

// get full file path (including subfolders)
$file_path = '';
find_file(BASE_DIR, $fname, $file_path);

if (!is_file($file_path)) {
  die("File does not exist. Make sure you specified correct file name."); 
}

// 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)) {
  die("Not allowed file type."); 
}

// 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.

if (!isset($_GET['fc']) || empty($_GET['fc'])) {
  $asfname = $fname;
}
else {
  // remove some bad chars
  $asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['fc']);
  if ($asfname === '') $asfname = 'NoName';
}

// 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);
$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      @fclose($file);
      die();
    }
  }
  @fclose($file);
}

// log downloads
if (!LOG_DOWNLOADS) die();

$f = @fopen(LOG_FILE, 'a+');
if ($f) {
  @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
  @fclose($f);
}

?>



<form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
<?php

// maximum execution time in seconds
set_time_limit (24 * 60 * 60);

if (!isset($_POST['submit'])) die();

// folder to save downloaded files to. must end with slash
$destination_folder = 'mydownloads/';

$url = $_POST['url'];
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");

  if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}

if ($file) {
  fclose($file);
}

if ($newf) {
  fclose($newf);
}

?>


Any suggetion will be wonderful. :idea:
Thanks :)


Regards
Amit
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Problem with opendir() and readdir() on server

Post by markusn00b »

Open up a terminal, command-prompt, or whatever you want to call it. Type this: cd /public_html/file_uploads

What are you told? I'm guessing you've got your paths mixed up.
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: Problem with opendir() and readdir() on server

Post by amitdubey2 »

by markusn00b » Mon Jun 07, 2010 3:23 pm

Open up a terminal, command-prompt, or whatever you want to call it. Type this: cd /public_html/file_uploads

What are you told? I'm guessing you've got your paths mixed up.
As said above by markusn00b,
i dont know how to check the path for server through command -prompt or something like that. :?:
But i am sure that it is not producing error because of path..As i have tried all combination of paths for server directory where my file is located.and also in localhost it is working fine for me.
i guess some permission problem for opendir() function. :dubious: :dubious:

Any help??? :idea:


regards
amit
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem with opendir() and readdir() on server

Post by Jonah Bron »

On shared hosts, the root of your site is not on the root of the drive. Are you totally sure this folder exists? How do you know?
Post Reply