...

View Full Version : Table Problem in loop



saeed
07-15-2008, 03:00 PM
I want table with two columns, one for file name and other one for date on which file was added.

I want it to be sort by end-user in any way they want.

I am having trouble in making table while filelist is being generated.

Actually its treating whole list of files as one cell. dont know where i am wrong. Please help. For a quick tip please see in between line 70 and 90

Thank you very much

index.php


<?php
include('upfiles.php');
//Mmaximum file size. You may increase or decrease.
$MAX_SIZE = 5242880;

//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.zip','.rar','.jpg','.png','.gif','.bmp','.doc','.xls','.ppt','.txt','.pdf');

//Allow file delete? no, if only allow upload only
$DELETABLE = true;


//Setup variables
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";

//Create Upload Directory
if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed.");
}

//Process User's Request
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);

if (strpos($_REQUEST[del],"/.")>0);
else if (strpos($_REQUEST[del],$upload_dir) === false);
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 5 megabyte (5242880 bytes).";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url);

print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";

//List Files
$handle=opendir($upload_dir);
$filelist = "";

while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {


//URL: <b>$upload_url$file</b>
$filelist .= "<a href='$upload_dir$file'>".$file."</a> - ";


if ($DELETABLE)
$filelist .= "Added on ".date("l, dS F, Y", filemtime($upload_dir.$file))."";

//$filelist .= " <a style='text-decoration:none; font-weight:bold' href='?del=$upload_dir".urlencode($file)."' title='Delete the file'><img src='delete.gif' align='absmiddle' border='0'></a>";
$filelist .="<br><br>";
}

}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$file_name was uploaded successfully." :
"Something is wrong with uploading the file.";
return $message;
}

?>


<html>
<head>
<title>EBDM Document Exchange FTP</title>
<link rel=stylesheet href=style.css>
</head>
<body>
<br><br>
<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>

<br>
Maximum File size allowed: <b>5 MegaByte (5242880 bytes)</b><br>
File Extensions allowed: <b>.zip, .rar, .jpg, .png, .gif, .bmp, .doc, .xls, .ppt, .txt, .pdf</b><br>
<br>
<b><u>UPLOADED FILES:</b></u><br><br>

<?=$filelist?>


</sup></small>
</center>

Nightfire
07-16-2008, 07:40 AM
You're not using a table anywhere...You're just making a list, separated each line with two <br>'s

saeed
07-16-2008, 08:44 AM
thank you 4 your reply.

this isnt done by me ... and i was asked by my friend to add table and sort functionality in it.

I dont know if this is right. :s

sorry I dont have any idea about PHP Scripting...




//List Files
$handle=opendir($upload_dir);
$filelist = "";

print("<table>");
print("<tr>");

while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {


//URL: <b>$upload_url$file</b>
$filelist .= "<td><a href='$upload_dir$file'>".$file."</a> </td> ";


if ($DELETABLE)
$filelist .= "<td>Added on ".date("l, dS F, Y", filemtime($upload_dir.$file))."</td>";

//$filelist .= " <a style='text-decoration:none; font-weight:bold' href='?del=$upload_dir".urlencode($file)."' title='Delete the file'><img src='delete.gif' align='absmiddle' border='0'></a>";
$filelist .="<br><br>";

print("<tr>");
print("</table>");
}


}

Nightfire
07-16-2008, 08:47 AM
You can start by deleting


$filelist .="<br><br>";
Look at the line below that... see a simpe html error? ;) No closing tag in the table row

saeed
07-16-2008, 09:00 AM
thank you

its working but i need a result like this.

---------------------------------
|filename |Added on 16 July 2008|
|filename2|Added on 12 July 2008|
.
.
.
---------------------------------

and so on
but right now its showing me like this.


-----------------------------------------------------------------
|filename |Added on 16 July 2008|filename2|Added on 12 July 2008|
-----------------------------------------------------------------

i know its kinda line break problem but where :s

sorry am disturbing u too much.

Nightfire
07-16-2008, 09:05 AM
doh, would teach me for proof reading code. If you looked at your source code, it would show something like



<table>
<tr>
<td>cell 1</td><td>cell 2</td></tr></table>
<td>cell 1</td><td>cell 2</td></tr></table>


This will fix it for you



//List Files
$handle=opendir($upload_dir);
$filelist = "";

print("<table>");

while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {


//URL: <b>$upload_url$file</b>
$filelist .= "<tr><td><a href='$upload_dir$file'>".$file."</a> </td> ";


if ($DELETABLE)
$filelist .= "<td>Added on ".date("l, dS F, Y", filemtime($upload_dir.$file))."</td></tr>";

}


}
print("</table>");


The error was a simple one, and can guarantee you'll do it often. The table row start tag was out of the loops, but needed to be in for it to start a new row. Also, you had the closing table tag in the loop, so would've been creating invalid html :)

saeed
07-16-2008, 09:07 AM
i did it [:)]

thanks for your help

herez the php code



//List Files
$handle=opendir($upload_dir);
$filelist = "";

while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {

print("<table>");
//URL: <b>$upload_url$file</b>
$filelist .= "<tr><td><a href='$upload_dir$file'>".$file."</a> </td>";


if ($DELETABLE)
$filelist .= "<td>Added on ".date("l, dS F, Y", filemtime($upload_dir.$file))."</td></tr>";

//$filelist .= " <a style='text-decoration:none; font-weight:bold' href='?del=$upload_dir".urlencode($file)."' title='Delete the file'><img src='delete.gif' align='absmiddle' border='0'></a>";
//$filelist .="<br>";
print("</table>");
}

}


Now how to add ascending and descending functionality in it? any idea?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum