Results 1 to 8 of 8

Thread: File Management help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    100

    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>

  2. #2
    New Member
    Join Date
    Dec 2008
    Posts
    9

    Re: File Management help

    Code:
    unlink(filepath);
    Deletes files in PHP.

    It wasn't the best name for this function imo.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    100

    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.

  4. #4
    New Member
    Join Date
    Dec 2008
    Posts
    9

    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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: File Management help

    Quote Originally Posted by Sakesaru
    Code:
    unlink(filepath);
    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.

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: File Management help

    Quote 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:

    Quote Originally Posted by PHP.net
    Deletes filename . Similar to the Unix C unlink() function.
    My usual boring signature: Something

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width