failed opendir / readdir

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

User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

failed opendir / readdir

Post by ol4pr0 »

I will get straight to the point.

i got these errors from my script ..

Code: Select all

Warning: opendir(htdocs/tms/up/files/down/): failed to open dir: Invalid argument in C:\Apache2\htdocs\tms\up\download.php on line 25

Warning: readdir(): supplied argument is not a valid Directory resource in C:\Apache2\htdocs\tms\up\download.php on line 26
this is my php code

Code: Select all

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded
$limitedext = array(".php",".jpg",".rar",".zip",".html",".exe"); //Extensions you want files uploaded limited to.
$sizelimit = "yes"; //Do you want a size limit, yes or no?
$sizebytes = "10000000"; //size limit in bytes
$dl = "http://localhost/tms/up/files/down/"; //url where files are uploaded
$absolute_path = "htdocs/tms/up/files/down/"; //Absolute path to where files are uploaded
$websiteurl = "http://localhost"; //Url to you website
$websitename = "http://localhost";

if (!isset($action)) {
    $action = "900";
}

switch($action) {
default:
echo "
<html>
<head>
<title>File Download</title>
</head>
<body><a href=$websiteurl>Return to $websitename</a>";
$list = "<table width=700 border=1 bordercolor=#000000 style="border-collapse: collapse">";
$list .= "<tr><td width=700><center><b>Click To Download</b></center></td></tr>";
$dir = opendir($absolute_path);
while($file = readdir($dir)) {
if (($file != "..") and ($file != ".")) {
//Download files with spaces fix by Kokesh
$list .= "<tr><td width=700><a href='$dl/$file'>$file</a></center></td></tr>";
}
}
$list .= "</table>";
echo $list;
echo"
<br><br>
<a href=http://tms.com/>TMS</a>
</body>
</html>";
break;
}
I checked the directory its there :lol:

____________________-

Also i used to be able to use this

Code: Select all

$PHP_SELF?action=
Its doesnt anymore..

Any suggustions?
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

You running your server on windows? If you are, i have found that you cant seem to use absolute paths per say, unless you do the full path like so:

c:/Apache2/htdocs/tms/up/files/down/

Im not sure if this is an apache config thing, or just a prob with using windows over unix. anway, Hope that helps
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Weird thing i used this script a long time ago..

with Easyphp ( never with apache ) and yes on a windows machine

going to try it with linux ( or better say, it has to work with linux lol )

just using w2k for testing ..

C:\<-- tried that one.. no succes
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Easiest way to get an absolute path would be:

Code: Select all

<?php
$absolute_path = $_SERVER['DOCUMENT_ROOT'] . "path/to/file/";
?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Hmm.. didnt do much for me

i also added a

Code: Select all

error_reporting(15);
To see if it would return something up in my logs..

However i do not have any errors in my logs concerning this script
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

= is assignment operator
$var ='php'; // assign data "php" to variable $var

== is equality operator wheth things on both sides are equal
if ($var=="php") //returns true if same

!= is equality operator which returns true if the both sides are not same.
if ($var!="php") //returns true if not same

there is one another
=== which shows that data on the both side is of same type.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

devork, what did that have to do with anything??

ol4pr0, post the code that your using
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

devork, i think you meant to post that here!
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Here is the code ( had it cut out of all the html form <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>.. to make it somewhat more clearly

Code: Select all

<?
error_reporting(15);
if(!isset($upload)) {
$upload = "900";
}
switch($upload) {
default:
include "config.php";
echo "
<html>

<head>
<title>Upload</title>
// alot of html form jabiedabie hehe..


<tr>
      <td bgcolor="#818EA0"><font size="2">The following restrictions apply:</font><ul type="square">
        <li><font size="2">File extension must be <b>";
        if (($extensions == "") or ($extensions == "") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
           echo "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            echo "&nbsp; $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
        }
        echo"</b></font></li>
        <li><font size="2">Maximum file size is $limit_size</font></li>
        <li><font size="2">No spaces in the filename</font></li>
        <li><font size="2">Filename cannot contain illegal characters 
        (/,*,\,etc)</font><BR>
        </li>
      </ul>
      <form method="POST" action="upload.php?upload=doupload" enctype="multipart/form-data">
<p align="center">
<input type=file name=file size=30 style="font-family: v; font-size: 10pt; color: #5E6A7B; border: 1px solid #5E6A7B; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1"><br>

// alot more of that html .. 

</html>";
break;
case "doupload":
include "config.php";
$endresult = "<font size="2">File Was Uploaded</font>";
if ($file_name == "") {
$endresult = "<font size="2">No file selected</font>";
}
else{
if(file_exists("$absolute_path/$file_name")) {
$endresult = "<font size="2">File Already Existed</font>";
} else {
if (($size_limit == "yes") && ($limit_size < $file_size)) {
$endresult = "<font size="2">File was to big</font>";
} else {
$ext = strrchr($file_name,'.');
if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
$endresult = "<font size="2">File is wrong type</font>";
}else{
@copy($file, "$absolute_path/$file_name") or $endresult = "<font size="2">Couldn't Copy File To Server</font>";
}
}
}
}
echo "
<html>

Code: Select all

// config 
<?
$absolute_path = "/htdocs/tms/up/files/down/"; //Absolute path to where files are uploaded
$size_limit = "yes"; //do you want a size limit yes or no.
$limit_size = "10000000"; //How big do you want size limit to be in bytes
$limit_ext = "yes"; //do you want to limit the extensions of files uploaded
$ext_count = "4"; //total number of extensions in array below
$extensions = array(".xml", ".txt", ".doc", ".xls"); //List extensions you want files uploaded to be
?>
Also have problems with this.. $php_self in the following code

Code: Select all

<body>
 <a href=$PHP_SELF?action=download>Download File</a>
 <a href=$websiteurl>Return to $websitename</a>
<br><br>
// and ...

<form method=POST action=$PHP_SELF?action=doupload enctype=multipart/form-data>
<p>File to upload:<br>

// tried the $_SERVER['PHP_SELF']; statement .. however that didnt 
// went well
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

why dont you just remove the php_self and put the real filename in there? It always works for me...
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Because i am not using a download.php page ;-)


me is using a

Code: Select all

case "download":
// the code. / with form bla bla...

break;
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ok, back to what i said, why dont you just remove the $PHP_SELF and replace it with the filename??

instead of

Code: Select all

<a href=$PHP_SELF?action=download>Download File</a>
why not

Code: Select all

<a href=mypage.php?action=download>Download File</a>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Well i am currently working on it right about now..

also change the

Code: Select all

\  &lt;-- normally on *nix
to --&gt; / for the windows..
That did solve me of some erorrs.. which shouldnt really mather i think
meaning the / \ differance.
Also gave me a bunc back tho..

1 of them..

so i am back to the / .. /

Code: Select all

Parse error: parse error, unexpected '=' in download.php line 7

// line 7

absolute_path = "htdocs\tms\up\files\down"; // by changing the / 2 \
Some how it wont reconize the directories.

Code: Select all

Warning: opendir(htdocs/tms/up/files/down/): failed to open dir: Invalid argument in C:\Apache2\htdocs\tms\up\download.php on line 25

Warning: readdir(): supplied argument is not a valid Directory resource
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Maybe you're path is wrong? Try doing this:

Code: Select all

<?php
$absolute_path = "/htdocs/tms/up/files/down/";
if(is_dir($absolute_path))  {
echo "Directory is OK";
}
else  {
echo "Bad file path";
}
?>
Then you will know if the path is causing the problem or not.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

If your using apache on windows, then you donbt need to include the htdocs because /htdocs is the default folder
Post Reply