Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

/p>

Strict//EN"

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

View Files

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

$Dir = "files";

$DirEntries = scandir($Dir);

echo "

";

echo "

";

echo "

";

echo "

";

echo "

";

echo "

";

echo "

";

echo "

";

foreach ($DirEntries as $Entry) {

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

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

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

echo "

";

}

}

echo "

Directory listing for

" . htmlentities($Dir) . "

NameOwner IDPermissions

Size
";

if (is_file($FullEntryName))

echo "" .

htmlentities($Entry). "";

else

echo htmlentities($Entry);

echo "

" .

fileowner($FullEntryName);

if (is_file($FullEntryName)) {

$perms = fileperms($FullEntryName);

$perms = decoct($perms % 01000);

echo "

0$perms";

echo "

" .

number_format(filesize($FullEntryName), 0) .

" bytes";

}

else

echo "

align='center'>

";

echo "

";

?>

. // End

FileUploader.php:

/p>

Strict//EN"

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

File Uploader

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

$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.


";

}

else

echo "There was an error

uploading \"" .

htmlentities($_FILES['new_file']

['name']) .

"\".
";

}

}

?>

enctype="multipart/form-data">

value="25000" />

File to upload:


(25,000 byte limit)


//End

FileDownloader.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;

?>

/p>

Strict//EN"

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

File Download Error

There was an error downloading "

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

}

?> //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.

image text in transcribed

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions