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

Create a set of php programs to allow users of the web site to upload various fi

ID: 3546017 • Letter: C

Question

Create a set of php programs to allow users of the web site to upload various files to our server.                  Details:                 
  • use the standard "php upload" routine                         
  • be sure to allow for file size up to 300 megabytes                         
  • limit files to type: txt, pdf, dxf, png                         
  • store the files in a subdirectory: .tmpFiles [be sure to use the "dot" in front of the file name                         
  • rename the files when uploaded to a unique name, in case two users happen to upload the same file, but retain the extension [txt, pdf, dxf,                            png]                         
  • add a "list" feature to show what files are in the temp folder                         
  • add one "bell or whistle" that comes to mind as a "nice to have" feature of this exercise
Details:                 
  • use the standard "php upload" routine                         
  • be sure to allow for file size up to 300 megabytes                         
  • limit files to type: txt, pdf, dxf, png                         
  • store the files in a subdirectory: .tmpFiles [be sure to use the "dot" in front of the file name                         
  • rename the files when uploaded to a unique name, in case two users happen to upload the same file, but retain the extension [txt, pdf, dxf,                            png]                         
  • add a "list" feature to show what files are in the temp folder                         
  • add one "bell or whistle" that comes to mind as a "nice to have" feature of this exercise
Create a set of php programs to allow users of the web site to upload various files to our server. Details: use the standard "php upload" routine be sure to allow for file size up to 300 megabytes limit files to type: txt, pdf, dxf, png store the files in a subdirectory: .tmpFiles [be sure to use the "dot" in front of the file name rename the files when uploaded to a unique name, in case two users happen to upload the same file, but retain the extension [txt, pdf, dxf, png] add a "list" feature to show what files are in the temp folder add one "bell or whistle" that comes to mind as a "nice to have" feature of this exercise

Explanation / Answer

1) Look at the following HTML form for uploading files:


<html>

<body>


<form action="upload_file.php" method="post"

enctype="multipart/form-data">

<label for="file">Filename:</label>

<input type="file" name="file" id="file"><br>

<input type="submit" name="submit" value="Submit">

</form>


</body>

</html>


2)The "upload_file.php" file contains the code for uploading a file:


<?php

if ($_FILES["file"]["error"] > 0)

{

echo "Error: " . $_FILES["file"]["error"] . "<br>";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "<br>";

echo "Type: " . $_FILES["file"]["type"] . "<br>";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

echo "Stored in: " . $_FILES["file"]["tmp_name"];

}

?>



3)

Restrictions on Upload

In this script we add some restrictions to the file upload. The user may upload txt, pdf, dxf and png files; and the file size must be under 300 MB:


<?php

$allowedExts = array("txt", "pdf", "dxf", "png");

$temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);

if ((($_FILES["file"]["type"] == "text/plain")// this file is txt

|| ($_FILES["file"]["type"] == "text/pdf")//this file is pdf

|| ($_FILES["file"]["type"] == "text/dxf")//this file is dxf

|| ($_FILES["file"]["type"] == "image/png"))// this file is png

&& ($_FILES["file"]["size"] < 307200)

&& in_array($extension, $allowedExts))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Error: " . $_FILES["file"]["error"] . "<br>";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "<br>";

echo "Type: " . $_FILES["file"]["type"] . "<br>";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " mB<br>";

echo "Stored in: " . $_FILES["file"]["tmp_name"];

}

}

else

{

echo "Invalid file";

}

?>


4)


Saving the Uploaded File

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.


The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:


<?php

$allowedExts = array("txt", "pdf", "dxf", "png");

$temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);


if ((($_FILES["file"]["type"] == "text/plain")// this file is txt

|| ($_FILES["file"]["type"] == "text/pdf")//this file is pdf

|| ($_FILES["file"]["type"] == "text/dxf")//this file is dxf

|| ($_FILES["file"]["type"] == "image/png"))// this file is png

&& ($_FILES["file"]["size"] < 307200)

&& in_array($extension, $allowedExts))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Return Code: " . $_FILES["file"]["error"] . "<br>";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "<br>";

echo "Type: " . $_FILES["file"]["type"] . "<br>";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " mB<br>";

echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";


if (file_exists("upload/" . $_FILES["file"]["name"]))

{

echo $_FILES["file"]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES["file"][".tmpFiles"],

"upload/" . $_FILES["file"]["name"]);

echo "Stored in: " . "tmpFiles/" . $_FILES["file"]["name"];

}

}

}

else

{

echo "Invalid file";

}

?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote