0

I am so stuck, could someone help, please? I've been learning PHP for a while and currently, I am creating a file upload form.

What needs to happen is this:

  1. File is uploaded
  2. The name of the file is displayed on the main page as a link
  3. Once the link is clicked on, information about the file displays (first sentence or so)

The questions I have are:

  1. File directory. I am supposed to create a folder, let's say called "uploads" - that's where the files are going to be downloaded to and have my form+php inside that folder, correct? - noob question, I know
  2. I managed to get the name of the file appears as a link, but I don't know how to display its contents.

Could someone help, please?

Code: https://pastebin.com/rfUgKzSu

 //file upload on main page


  if (isset($_POST['name'])) {
     move_uploaded_file($_FILES['file']['tmp_name'],'add_article_form.php' . $_POST['name'] . '.txt'); 
     echo   'file ' . $_POST['name'] .  '.txt' . 'uploaded'; 


  }else{
      echo 'There has been a mistake';
  }
      echo '<br>' ;
      echo '<br>' ;


//form on main page
 <form   action = "add_article_form.php" method = "POST">

<input id = "add" type = "submit" value="add"> 

</form>


<?php 

 //display file name

 $resource = opendir('../uploads/');

 while(($entry = readdir($resource))!== FALSE)
 {
     if($entry != '.' && $entry != '..'){
     echo "<a href = \"#\">$entry</a>" . '<br>'; 

    }  else {
        "<a href = \"#\">$entry</a>" . '<br>'; 

    }
 }

PAGE 2

          <!--The upload form, second page -->

<form action = "../uploads/" method="POST" enctype="multipart/form-data" >
Название статьи: <br>
<input type = "text" name = "name" value = "text"><br><br>
Файл:<br>
<input type = "file" name="file"><br><br>
<input id = "add" type = "submit" value="add"><br><br>

</form>
NewBee
  • 394
  • 3
  • 15
Anna Kurmanova
  • 141
  • 1
  • 7
  • A specific directory (or set of directories) for uploads is more common, you don't need to do this, but it helps to keep the files uploaded separate from the application files. Also allows you to use file permissions to stop your app being modified, but allow your uploaded files to be created/edited/deleted as needed. – Nigel Ren Jun 09 '18 at 12:29
  • As for displaying the file - https://stackoverflow.com/questions/9539849/how-to-echo-the-whole-content-of-an-html-file-in-php – Nigel Ren Jun 09 '18 at 12:31

1 Answers1

0

The questions I have are: 1. File directory. I am supposed to create a folder , let's say called "uploads" - that's where the files are going to be downloaded to and have my form+php inside that folder, correct? - noob question, I know

No.

You can keep your upload script and the folder, where the files will be saved, anywhere even on different servers. The only thing that you should be careful that you use the correct links and proper permissions so that they can access each other.

I managed to get the name of the file appears as a link, but I don't know how to display its contents.

Use PHP header function.

http://php.net/manual/en/function.header.php (Go through Example 1.)

NewBee
  • 394
  • 3
  • 15