<?php
/******************************************************
 * * NOTES:
 *        Some necessary form elements for file uploads
 *        value is measured in bytes
 *        enctype="multipart/form-data"
 *        <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
 */

// Upload File
$directory 'directory/';


// Delete File
if(isset($_POST['deleteButton'])){
    
unlink($directory $_POST['toDelete']);
    print(
"Deleted " basename($_POST['toDelete']));

}
 
 
if(
$_POST['submitButton'] != ''){
    
$filesize $_FILES['file_to_upload']['size'];
    
    if(
$filesize 30000){

        
// Should also add duplicate name checking.
        // Could also do an 'accebtable mime type array' check in here
        
if($_FILES['file_to_upload']['name'] != ''){
            
                if((
$_FILES['file_to_upload']['type'] == "image/jpeg") || ($_FILES['file_to_upload']['type'] == "image/gif")){
            
                        
$target_path $directory $_FILES['file_to_upload']['name'];
                    
                        if(
move_uploaded_file($_FILES['file_to_upload']['tmp_name'] , $target_path)){
                            
chmod("$target_path" 0777);
                            print(
"Uploaded<br />");
                        } else {
                            print(
"There was a problem uploading<br />");
                        }
                    
                } else {
                    print(
"Invalid file type, please upload only .jpg or .gif files<br />");
                }
            
        } else {
            print(
"Nothing to Upload<br />");
        }
        
    } else {
        print(
"File size is too big<br />");
    }


 
 
 
 
?>
<html>
<head>
<title>Week #13 Basic ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>

<table border="0">

<tr>
    <td valign="top">
        <fieldset style="width:300px;">
        <legend>File Upload (29kb Max)</legend>
        <form method="post" action="wk13basic.php" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        <input type="file" name="file_to_upload">&nbsp;<input type="submit" name="submitButton" value="Upload">
        </form>
        </fieldset>
    </td>
    <td rowspan="2" valign="top">
        <fieldset>
        <legend>Viewing Area</legend>
        <form method="POST" action="wk13basic.php">
        <?php
            $directory 
"directory/";
            
            
// Get contents of dir
            
$dh opendir($directory);
            while(
$foldername readdir($dh)){
                if(
$foldername != "." && $foldername != ".."){
                    
$dirList[] = $foldername;
                }
            }
            
closedir($dh);
        
?>
        <select name="selected">
        <option value=""></option>
        <?php
            
foreach($dirList as $value){
            print(
"<option value=$value name=\"choice\">$value</option>");
            }
        
?>
        </select>
        <input type="submit" name="viewButton" value="View" />
        </form>
        
        <?php
            
// View File
            
if(isset($_POST['viewButton'])){
                
$directory 'directory/';
                if(
$_POST['selected'] != ''){
                    print(
"
                    <form method=\"POST\" action=\"wk13basic.php\">
                    <input type=\"hidden\" name=\"toDelete\" value=\"$_POST[selected]\" />
                    <center>
                    $_POST[selected]<br />
                    <img src=\"$directory$_POST[selected]\"><br />
                    <input type=\"submit\" name=\"deleteButton\" value=\"delete\"/>
                    </center>
                    </form>
                    "
);
                }
            }
        
?>
        </fieldset>
    </td>
</tr>

<?php
if($target_path){
?>
<tr>
    <td>
        <fieldset style="width:300px;">
        <legend>Uploaded Image</legend>
        <center>
        <?php print(basename($target_path) . "<br />");?>
        <img src="<?php echo($target_path); ?>" /><br />
        </center>
        </fieldset>
    </td>
</tr>        

<?php
}
?>
</table>






<!-------------  END  ------------->


<br /><br /><a href='../index.html'>Index</a><br />
<iframe src='wk13basicsrc.php' width='100%' height='300'></iframe>
<br />

<a href='../index.html'>Index</a>

</body>
</html>