Forum Moderators: coopster

Message Too Old, No Replies

File Manager/Unlink usage

implementing additional code

         

fireman gcfd

2:09 am on Feb 25, 2005 (gmt 0)

10+ Year Member



Hello Everybody.

Another newbie here toughing my way through the PHP world. I have been plugging away at this upload/download script for some time now. I have it working just great with one exception...I can't delete any files after they have server their purpose. This started out as a borrowed script with my twists and changes to it making it work for me. All I want it to do is list the files in the directory so I can choose which ones to delete.

Here is what I have, please have a look and maybe guide me in the right direction. If anyone is looking for a great upload/download script this is a good beginner one.

<html>
<head>
<title>CLAS File Management System</title>
</head>
<body>
<p align="center"><img border="0" src="../images/boardareatitle.jpg" width="590" height="225"></p>
<?
$extlimit = "no"; //limit the extensions of files uploaded
$limitedext = array(".gif",".jpg",".png",".jpeg",".doc",".xls"); //uploaded limited to.
$sizelimit = "no"; //size limit, yes or no?
$sizebytes = "2000000"; //size limit in bytes
$dl = "http://www.example.ca/CLASBoardMember/General_Information"; //url uploaded
$absolute_path = "/usr/home/html/www.example.ca/CLASBoardMember/General_Information"; //Absolute path $websiteurl = "http://www.example.ca/CLASBoardMember/index.htm"; //Url
$websitename = "Society Board Area";
$pass = "clasup05";

switch($action) {
default:
case "download":
echo "
<html>
<head>
<title>CLAS Board General Information File Download</title>
</head>
<body>";
$list = "<center><table width=500 border=0 bordercolor=#CCCCCC style=\"border-collapse: collapse\">";
$list .= "<tr><td width=500><center><b>Click To File Name Below to Open</b></center></td></tr>";
$list .= "<tr><td width=590><center>or right click and \"Save Target as\"</center></td></tr>";
$dir = opendir($absolute_path);
while($file = readdir($dir)) {
if (($file!= "..") and ($file!= ".")) {
$list .= "<tr><td width=500><center><a href='$dl/$file'>$file</a></center></td></tr>";
}
}
$list .= "</table></center>";
echo $list;
echo"
<br><br><br>
<center><a href=$websiteurl>Back to Selections</a><br>
Having Trouble? Contact the <a href=mailto:webmaster@example.ca>Webmaster</a>
</body>
</html>";
break;

case "upload":
echo"
<html>

<head>
<title>General Information File Upload</title>
</head>

<body>
<center>
<form method=POST action=$PHP_SELF?action=doupload enctype=multipart/form-data>
<p><b>File to upload into General Information folder:</b><br>
<input type=file name=file size=30>
<p><b>Password:</b><br>
<input type=password name=password>
<p><button name=submit type=submit>
Upload
</button>
</form>
<br><br>
Having Trouble? Contact the <a href=mailto:webmaster@example.ca>Webmaster</a>
</body>

</html>";
break;

//File Upload
case "doupload":
$dir = "dir";
if($_POST['password']!= $pass){
die("Error. Incorrect password. You do not have permission to upload files.");
}
if ($file!= "") {

if (file_exists("$absolute_path/$file_name")) {
die("File already exists");
}

if (($sizelimit == "yes") && ($file_size > $sizebytes)) {
die("File is to big. It must be $sizebytes bytes or less.");
}

$ext = strrchr($file_name,'.');
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
die("The file you are uploading doesn't have the correct extension.");
}

@copy($file, "$absolute_path/$file_name") or die("The file you are trying to upload couldn't be copied to the server");

} else {
die("Must select file to upload");
}
echo "
<html>
<head>
<title>File Uploaded</title>
</head>
<body>
<center><b>";
echo $file_name." was uploaded";
echo "</b><br><br>
<a href=$PHP_SELF?action=upload>Upload Another File</a><br>
<a href=$PHP_SELF?action=download> Download File</a><br>
<a href=$websiteurl> Return to $websitename</a><br><br>
Having Trouble? Contact the <a href=mailto:webmaster@example.ca>Webmaster</a>
</body>
</html>";
break;

}
?>

[edited by: jatar_k at 3:25 am (utc) on Feb. 25, 2005]
[edit reason] generalized urls [/edit]

badone

2:20 am on Feb 25, 2005 (gmt 0)

10+ Year Member



Check the permissions of the directories you're working in. The apache user needs x permision to overwrite or delete.

Cheers,
BAD

jatar_k

3:33 am on Feb 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld fireman gcfd,

I have had this before the prolem usually is that since php runs as nobody you have to have nobody permissions to remove anything it creates.

So you need a delete script.

for your switch you need a case for 'delete' where it just does something like

case "delete":
$file_id = $_GET['fid'];
unlink($file_id);
echo '<p>I just deleted the file with the id of ',$file_id;
break;

now this doesn't really fit exactly, you may have to pass the filename or whatever you use to keep track of the files. How you could pass it is to have a link, or a form and use $_POST, that goes to this script and passes it the filename or file id that it needs to delete.

I would suggest this method for some type of private area or you may find files deleted that you still wanted.

that's roughly the logic of the whole thing though.

fireman gcfd

7:02 am on Feb 25, 2005 (gmt 0)

10+ Year Member



I'll give it a try and see what I can come up with. Let you know my results in a day or so.

Thanx

Jaz