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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By joeck
      Hi,
      I'm facing the issue where a customer has only 1GB of RAM on its server available (and doesn't want to upgrade).
      They have a few large files (videos) that they want on the website but when uploading via the processwire backend the progress bar gets stock after some time (assuming because it ran out of RAM).
      I see that files are saved to site/assets/files/1048 and 1048 being the page id. I tried uploading the files via ftp (to avoid the shortage of RAM) to the same folder but the images don't appear in the files field.
      I'm assuming the files field is just an array with the file names that are then searched in site/assets/files/1048 . Is there a way where I can have the field look for other files in that folder and have it automatically added to the array so it appears in the files field of the page?
      My attempt didn't work and I didn't quite understand why:
      $directory = "../../files/1048"; // files uploaded here via ftp $page->of(false); foreach (scandir($directory) as $file) { //go through all files if ($file !== '.' && $file !== '..') { echo $file; $page->file->add($file); } } $page->save(); $page->of(true);  
    • By Marvin
      Hello, excuse me
      I'm new at processwire, i want to ask about a master detail system (Inheritance system) using a processwire. Can we create a Inheritance table using a processwire, and how we create it? Thank you for the help
    • By picarica
      so hello there i have fiel field type i have lots of stuff there, and also an image i can easily get image from that field using
                                                        
      $options = array('quality' => 85, 'upscaling' => true, 'cropping' => 'north', 'sharpening' => 'medium'); $word = ".png"; // Test if string contains the word foreach($childgames->subor_hry as $file) { if(strpos($file, $word) !== false){ /* $imger = $file->size(473, 266, $options); */ echo $file; /* echo $file->url; */ } } so i get the fiel i tried invoking size on it like the commented out part and it doesnt work i get error Oh snizzle… Error:     Exception: Method Pagefile::size does not exist or is not callable in this context (in
      what am i doing wrong? can you guys help me by all logic this should work
    • By Max Esnée
      Hi,
      When I upload a large(ish) SVG (≈120ko in this case) into an image field, it tends to get stuck at 100% and block the whole website. If I force refresh the admin page, the svg has not been uploaded. The file is correctly copied into the assets folder though. Other svg tends to be uploaded fine albeit I've noticed that pages containing svg images in the admin are very slow to load generally. Other pages using the same template but with jpeg or png images are much faster to load.
      I get the following error in the JS console:

      I develop locally using MAMP on macOS and I'm using Firefox. I'm using the last ProcessWire version (3.0.165).
      I also tested in Chrome and got the same error.
    • By Marvin
      Hi,
      I'm still new at processwire, i want to ask, i was create a website with sign in system, but i want to add a access roles/permission for each user at my website. For now, i just can create a login user without any permission and the user data became as a pages in my processwire.
      Here i attach my code for login
      <?php $note = $note2 = $hidden =""; if($input->get->id == "logout") { $session->remove('login_id'); } if($input->post->submit) { $email = $sanitizer->email($input->post->email); $pass = $sanitizer->text($input->post->pass); $result = $pages->find("email=$email, text_1=$pass"); if(!$email || !$pass) { $note = "Data belum lengkap"; } else { if($result->count > 0) { $session->login_id = "$result"; $hidden = "style = 'diplay:none'"; $url=$pages->get("/files/")->httpUrl; header("Location:$url"); die(); } else { $note = "Akun tidak ditemukan"; } } } ?> And this code for login form
      <div class="frow-container"> <div class="frow centered mt-50"> <div class="col-md-1-3"> <div class="box p-40 shadow-light"> <h2 class="mb-20 text-center" <?=$hidden?>>Database Partitur<br>GII HIT</h2> <form method="post" <?=$hidden?>> <label>Username <input type="text" name="email"></label> <label>Password <input type="password" name="pass"></label> <input type="submit" name="submit" value="Masuk"> </form> <p class="notif"><?=$note;?></p> <div class="text-center"><?=$note2;?></div> </div> </div> </div> </div> Just for remember, my user data now is a pages, and i cannot give any permission to user data.
      Thank you very much for help.
×
×
  • Create New...