Jump to content

PHP table file uploader


jaArch

Recommended Posts

Hi:

 

I have written code that will allow a user to upload a file and that will allow the user to view/delete the image when clicking on the link. I'm having trouble in the table where the image information is organized - the table just duplicates information about an image into another column,.how can I fix this?

 

Here's my code:

 

 <?



if ($handle = opendir('images/')) {
    while (false !== ($file = readdir($handle))) {
$TPL['fileList'][] = array(
							'fileName' => $file,
							'fileSize' => filesize('images/'.$file),
							'fileDate' => 'Jan 2, 2029'  );
        }
}

switch ($_REQUEST['act']):
default:
break;

if (empty($_FILES['userfile']['name'])):
	$TPL['errMsg'] = "No file selected";
	$TPL['dispErrMsg'] = true; 
break;
endif;
copy ($_FILES['userfile']['tmp_name'],
				'images/'.$_FILES['userfile']['name']);
	break;


	case 'del':
	echo "delete this file? ($_GET)['userfile']['name']";
endswitch;

?>

<html>
<head>
<title>Comp10065 - Lab 4: File Upload</title>
<style type="text/css">
body {background-color:yellow}
div#container {margin-right: auto;	margin-left: auto;	border: 1px solid #EEEEEE;
	width: 800px;	padding: 10px;  background-color:white}
h1 {text-align: center; color:orange; background-color:white}
table {width: 700px; margin-bottom: 15px}
table th {text-align: left;padding: 0px 10 0px 10px}
table td{padding: 0px 10 0px 10px; border: 1px solid #AAAAAA}
tr.odd {background-color: #EEEEEE}
tr.even {background-color: #CCCCCC}
.message {color:red}
div#debug {border: 1px solid #BBBBBB}
</style>
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.js"></script>
<script type="text/javascript">
function popup(img) {
	var day= new Date();
	var windowParms = 'width=400,height=400,scrollbars,resizable';
	var htmlPage = '<html><body><img src="'  + img + '"><br>' +    
					' <form><input type="button" ' + 
					' onclick="javascript:window.close();" value="Close Window"><br>\n' + 
					' </form></body></html>\n';
	var popupWindow = window.open('',day.getTime(),windowParms);
	popupWindow.document.write(htmlPage);
	popupWindow.document.close();
}
</script> 
<script>
$(function() {
$("div#debug").hide();
$("a#debugLink").click(function () {$("div#debug").toggle("slowly"); });
});
</script>
</head>

<body >
<div id="container">

  <h1> Picture Uploader!</h1>
       
  <p>Click here to <a href="<?= $_SERVER['PHP_SELF'] ?>">refresh</a> this page.</p>
      
      	 		  <table>
		<tr> 
		  <th>File Name (<a href="upload.php?act=sortby&col=filename">sort</a>)</th>
		  <th>Size KBytes (<a href="upload.php?act=sortby&col=size">sort</a>)</th>
		  <th>Date</th>
		</tr>
           </td>
            <? foreach($TPL['fileList'] as $key => $val): ?>

                        <td><a href="<?= 'images/'.$val['fileName'] ?>"><?=$val['fileName'] ?></a></td>
		<td><?= $val['fileName'] ?></td>
		<td><?= $val['fileSize'] ?></td>
		<td><?= $val['fileDate'] ?></td>
		<td><a href="#" onClick="popup('images/uieiuirdi.jpg')"  >View</a></td>
		<td align=center><a href=upload.php?act=del&file=uieiuirdi.jpg>Delete</a></td>
		</tr>
             <? endforeach ?>
				  </table>
            
                     			<p class="message">Records sorted by size</p>

              
       
<form   action=upload.php  method="post" enctype='multipart/form-data'>
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
	<input type="hidden" name="act" value="post">
        <input  type="file" name="userfile" size="60" value="">
        <br>
        <input type="submit" value="UPLOAD FILE" name="submit">Do not upload large files!
      </form>
  
  <? if($TPL['dispErrMsg']): ?>
      <p><?= $TPL['errMsg'] ?></p>
      <? endif; ?>
  
      <hr>
      <p>Click <a href="#" id = "debugLink"> here </a> for debug info. Jquery show/hide effect.</p>

      <div id="debug">
          <p>This debug info is here to help you. You are only required to display the $_POST and $_FILES array</p>
          <pre > <? print_r($TPL) ?></pre> 
      </div>	 
</div> 
</body> </html>

Link to comment
https://forums.phpfreaks.com/topic/249914-php-table-file-uploader/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.