|
-
Dec 24th, 2008, 11:54 AM
#1
Thread Starter
Lively Member
File Management help
Hello,
I need some help with some php script for file management. So far, the code can upload up to 3 files at once and it works fine. However, I would like to have the ability to delete files from the directory that they are uploaded to. I would appreciate some help.
Here's the code I have so far:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Upload a File</title>
</head>
<body>
<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file1']['tmp_name'], "files/{$_FILES['file1']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file1']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file2']['tmp_name'], "files/{$_FILES['file2']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file2']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file3']['tmp_name'], "files/{$_FILES['file3']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file3']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
?>
<form action="index.php" enctype="multipart/form-data" method="post">
<center>
<p>Upload a file using this form: <br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<input type="file" name="file1" /><br /><br />
<input type="file" name="file2" /><br /><br />
<input type="file" name="file3" /><br /><br />
<input type="submit" name="submit" value="Upload This File" />
<br><br>
<a href="files/">View Files</a>
</p>
</center>
</form>
</body>
</html>
-
Dec 24th, 2008, 11:57 AM
#2
New Member
Re: File Management help
Deletes files in PHP.
It wasn't the best name for this function imo.
-
Dec 24th, 2008, 12:10 PM
#3
Thread Starter
Lively Member
Re: File Management help
How can I list all the files in the directory with php?
Btw, I really new to php, thats why I don't know this stuff.
-
Dec 24th, 2008, 12:21 PM
#4
New Member
Re: File Management help
Code:
if ($handle = opendir('C:\Path\To\Files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
} else {
echo 'Directory listing failed because the path provided was invalid';
}
php.net has excellent documentation on all of the functions used above.
Code pulled and modified from http://nz2.php.net/readdir (comment #1) because I'm lazy and I don't have my php files at work.
ALSO: you could shorten the code you have in post #1.
Code:
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file1']['tmp_name'], "files/{$_FILES['file1']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file1']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file2']['tmp_name'], "files/{$_FILES['file2']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file2']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file3']['tmp_name'], "files/{$_FILES['file3']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file3']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
Could be changed to:
Code:
if (isset ($_POST['submit'])) {
if (move_uploaded_file ($_FILES['file1']['tmp_name'], "files/{$_FILES['file1']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file1']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
if (move_uploaded_file ($_FILES['file2']['tmp_name'], "files/{$_FILES['file2']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file2']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
if (move_uploaded_file ($_FILES['file3']['tmp_name'], "files/{$_FILES['file3']['name']}")) {
print '<p>Your file has been uploaded.</p>';
} else {
print '<p>Your file could not be uploaded because: <b>';
switch ($_FILES['file3']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
print 'The file was only partially uploaded';
break;
case 4:
print 'No file was uploaded';
break;
}
print '</b>.</p>';
}
}
because there is no need to check if
Code:
if (isset ($_POST['submit']))
is true three times, in this case at least.
Last edited by Sakesaru; Dec 24th, 2008 at 12:29 PM.
-
Dec 25th, 2008, 11:23 PM
#5
Re: File Management help
if (isset ($_POST['submit']))
What's this for?
Use $_SERVER['REQUEST_METHOD'] if you're trying to validate that the request was a POST request.
-
Dec 25th, 2008, 11:26 PM
#6
Re: File Management help
 Originally Posted by Sakesaru
Deletes files in PHP.
It wasn't the best name for this function imo.
'unlink' is a POSIX standardised function name. PHP's usage probably derives from Perl's.
-
Dec 26th, 2008, 06:07 PM
#7
Re: File Management help
 Originally Posted by penagate
'unlink' is a POSIX standardised function name. PHP's usage probably derives from Perl's.
It is actually taken from C:
 Originally Posted by PHP.net
Deletes filename . Similar to the Unix C unlink() function.
My usual boring signature: Something
-
Dec 26th, 2008, 06:13 PM
#8
Re: File Management help
There is a reason why it is named unlink. This is because it unlinks a file or directory from its parent directory - so the name of the function simply describes what it does.
In the Linux extended file system and many others, a directory is simply a list of links to other directories of files. In Linux, a file can be linked from one or more directories; this so called hard linking is what the unlink command does.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|