Jump to content

[SOLVED] Showing an uploaded file at the table and open it at new tab


Marvin
 Share

Recommended Posts

Hello,

I'm new at process wire and i want to make an web using upload file and showing it at the table as a link to open it at the new tab. I wa succed while upload a file, but how i showing it as a link at the table to open it at the new tab of my browser? Any suggestion may helpfull

Here i attach my code below :

This code is for upload it to back-end (process wire)


<?php
  $note = $note2 = $hidden ="";
  if($input->post->submit){
    $upload_path = $config->paths->assets.'files/upload/';
    if(!is_dir($upload_path)){
      if(!wireMkdir($upload_path)) throw new WireException("No upload path");
    }
    $original = $sanitizer->text($input->post->original);
    $indonesia = $sanitizer->text($input->post->indonesia);
    $other = $sanitizer->text($input->post->other);
    $composer = $sanitizer->text($input->post->composer);

    if(!$original || !$indonesia || !$other || !$composer){
      $note = "Data tidak lengkap";
    } else {
      $newFile = new WireUpload("song_files");
      $newFile->setMaxFiles(1);
      $newFile->setOverwrite(false);
      $newFile->setDestinationPath($upload_path);
      $newFile->setValidExtensions(array('pdf','docx','doc'));
      $files = $newFile->execute();

      if(!count($files)) {
        $newFile->error("No files received, so not creating page.");
        return false; 
      }

      $newImg = new WireUpload("img_files");
      $newImg->setMaxFiles(1);
      $newImg->setOverwrite(false);
      $newImg->setDestinationPath($upload_path);
      $newImg->setValidExtensions(array('jpeg','jpg','png','gif'));
      $files = $newImg->execute();

      if(!count($files)) {
        $newImg->error("No files received, so not creating page.");
        return false; 
      }

      $newPage = new Page();
      $newPage->template = "files";
      $newPage->parent = $pages->get("/files/");
      $newPage->title = $original;
      $newPage->text_1 = $indonesia;
      $newPage->text_2 = $other;
      $newPage->text_3 = $composer;
      $newPage->of(false);
      $newPage->save();

      foreach($files as $filename) {
        $filepath = $upload_path . $filename;
        $newPage->files->add($filepath);
        $newPage->message("Add file : $filename");
        unlink($filepath);
      }
      $newPage->save();
    }
  }
?>

and this code to showing it as a link at the table

<table class="border">
          <tr>
            <th>No.</th>
            <th>Original Song Title</th>
            <th>Indonesia Song Title</th>
            <th>Other Song Title</th>
            <th>Composer</th>
            <th>File (pdf)</th>
          </tr>
          <?php
            $num = 1;
            $song;
            foreach($pages->get("/files/")->children as $child) { //showing every child at files parent directory
              $page == $child;
              $song = $pages->get("/files/".$child->id."/")->files; //showing uploaded files at child directory
              echo $child->id;
              echo "<tr><td>".$num++.".</td><td>".$child->title."</td><td>".$child->text_1."</td><td>".$child->text_2."</td><td>".$child->text_3."</td><td><a href='".$song->httpUrl."'</a>".$song->name."</td></tr>";
            }
          ?>
        </table>

Thank you for any suggestion

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...