Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP programming. Working with Files and Directories. I am trying to create an di

ID: 3732298 • Letter: P

Question

PHP programming. Working with Files and Directories. I am trying to create an directory that will let an user upload or download files to an directory. I'm using notepad++ and xampp for this task. I currently have three files, ViewFiles.php, FileUploader.php, and FileDownloader.php. Here are the codes below:

ViewFile.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>View Files</title>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

<body>

<?php

$Dir = "files";

$DirEntries = scandir($Dir);

echo "<table border='1' width='100%' > ";

echo "<tr><th colspan='4'>Directory listing for

<strong>" . htmlentities($Dir) . "</strong></th>

</tr> ";

echo "<tr>";

echo "<th><strong><em>Name</em></strong></th>";

echo "<th><strong><em>Owner ID</em></strong></th>";

echo "<th><strong><em>Permissions</em></strong>

</th>";

echo "<th><strong><em>Size</em></strong></th>";

echo "</tr> ";

foreach ($DirEntries as $Entry) {

if ((strcmp($Entry, '.') != 0) &&

(strcmp($Entry, '..') != 0)) {

$FullEntryName=$Dir . "/" . $Entry;

echo "<tr><td>";

if (is_file($FullEntryName))

echo "<a href="$FullEntryName">" .

htmlentities($Entry). "</a>";

else

echo htmlentities($Entry);

echo "</td><td align='center'>" .

fileowner($FullEntryName);

if (is_file($FullEntryName)) {

$perms = fileperms($FullEntryName);

$perms = decoct($perms % 01000);

echo "</td><td align='center'>

0$perms";

echo "</td><td align='right'>" .

number_format(filesize($FullEntryName), 0) .

" bytes";

}

else

echo "</td><td colspan='2'

align='center'>&lt;DIR&gt;";

echo "</td></tr> ";

}

}

echo "</table> ";

?>

</body>

</html>. // End

FileUploader.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>File Uploader</title>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

        <body>

<?php

$Dir = "files";

if (isset($_POST['upload'])) {

if (isset($_FILES['new_file'])) {

if (move_uploaded_file(

$_FILES['new_file']['tmp_name'],

$Dir . "/" . $_FILES['new_file']

['name']) == TRUE) {

chmod($Dir . "/" . $_FILES['new_file']

['name'], 0644);

echo "File "" .

htmlentities($_FILES['new_file']

['name']) .

""successfully uploaded.

<br /> ";

}

else

echo "There was an error

uploading "" .

htmlentities($_FILES['new_file']

['name']) .

"".<br /> ";

}

}

?>

<form action="FileUploader.php" method="POST"

enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE"

value="25000" /><br />

File to upload:<br />

<input type="file" name="new_file" /><br />

(25,000 byte limit) <br />

<input type="submit" name="upload" value="Upload the

File" />

<br />

</form> //End

FileDownloader.php:

<?php

$Dir = "files";

if (isset($_GET['filename'])) {

$FileToGet = $Dir . "/" . stripslashes

($_GET['filename']);

if (is_readable($FileToGet)) {

}

else {

$ErrorMsg = "Cannot read "$FileToGet"";

$ShowErrorPage = TRUE;

}

}

else {

$ErrorMsg = "No filename specified";

$ShowErrorPage = TRUE;

}

if ($ShowErrorPage) {

                header("Content-Description: File Transfer");

header("Content-Type: application/force-download");

header("Content-Disposition: attachment;

filename="" . $_GET['filename'] . """);

header("Content-Transfer-Encoding: base64");

header("Content-Length: " . filesize($FileToGet));

readfi le($FileToGet);

$ShowErrorPage = FALSE;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>File Download Error</title>

<meta http-equiv="content-type" content="text/html;

charset=iso-8859-1" />

</head>

<body>

<p>There was an error downloading "<?php echo

htmlentities($_GET['filename']); ?>"</p>

<p><?php echo htmlentities($ErrorMsg); ?></p>

</body>

</html>

<?php

}

?> //End

The first problem is this is that the file directory is not coming out how it is suppose to.

Also, when in the ViewFiles.php file, I could not get the browser to download the files when selected.

Nee View Files Mozilla Firefox Directory lsting tor Sle Ele Edit ew Higtory Bookmarks Iools Help . >

Explanation / Answer

your file destination folder is wrong thats the error now i fixed it.kindly check it

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>File Uploader</title>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

<body>

<?php

define ('SITE_ROOT', realpath(dirname(__FILE__)));

$Dir = "docs";

if (isset($_POST['upload'])) {

if (isset($_FILES['new_file'])) {

$uploads_dir = 'www/test/docs/';

$uploadfile = "uploads/".$_FILES['new_file']['name'];

if (is_uploaded_file($_FILES['new_file']['tmp_name']))

{

move_uploaded_file($_FILES['new_file']['tmp_name'], $uploadfile);

echo 'moved file to destination directory';

exit;

}

} else {

echo "Upload failed";

}

echo "</p>";

echo '<pre>';

echo 'Here is some more debugging info:';

print_r($_FILES);

print "</pre>";

}

?>

<form action="FileUploader.php" method="POST"

enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE"

value="" /><br />

File to upload:<br />

<input type="file" name="new_file" /><br />

(25,000 byte limit) <br />

<input type="submit" name="upload" value="Upload the

File" />

<br />

</form>

To download the image in the list we dont need this much script. we have new facility in html 5 called downoload attribute by simply adding like this.

echo "<a href="$FullEntryName" download="$FullEntryName">" .

htmlentities($Entry). "</a>";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>View Files</title>

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

<body>

<?php

$Dir = "docs";

$DirEntries = scandir($Dir);

echo "<table border='2' width='100%' > ";

echo "<tr><th colspan='4'>Directory listing for

<strong>" . htmlentities($Dir) . "</strong></th>

</tr> ";

echo "<tr>";

echo "<th><strong><em>Name</em></strong></th>";

echo "<th><strong><em>Owner ID</em></strong></th>";

echo "<th><strong><em>Permissions</em></strong>

</th>";

echo "<th><strong><em>Size</em></strong></th>";

echo "</tr> ";

foreach ($DirEntries as $Entry) {

if ((strcmp($Entry, '.') != 0) &&

(strcmp($Entry, '..') != 0)) {

$FullEntryName=$Dir . "/" . $Entry;

echo "<tr><td>";

if (is_file($FullEntryName))

echo "<a href="$FullEntryName" download="$FullEntryName">" .

htmlentities($Entry). "</a>";

else

echo htmlentities($Entry);

echo "</td><td align='center'>" .

fileowner($FullEntryName);

if (is_file($FullEntryName)) {

$perms = fileperms($FullEntryName);

$perms = decoct($perms % 01000);

echo "</td><td align='center'>

0$perms";

echo "</td><td align='right'>" .

number_format(filesize($FullEntryName), 0) .

" bytes";

}

else

echo "</td><td colspan='2'

align='center'>&lt;DIR&gt;";

echo "</td></tr> ";

}

}

echo "</table> ";

?>

</body>

</html>