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 Joachim
      Long time user and huge fan of PW, but this time I can't find an answer to my question this time:
      For my social media buttons, I have a Repeater field called var_link_web with two fields: one is for the URL, and the other is an Images field containing two images that are used as a background-image for a <div>, of which the second is the ':hover' version (although activated through JavaScript here). 
      There are four instances of this Repeater, of which two are 'turned off'.
      I use the following PHP in my _main.php to call them, wrapped in <p><?php == ?></p>:
      $s_m_button = $variables->var_link_web; foreach($s_m_button as $button){ $button_image = $button->var_link_image->first->height(80); $button_image2 = $button->var_link_image->eq(1)->height(80); echo " <a href='$button->var_link_url'> <div class='image_link' style='background-image:url({$button_image->url})' onMouseOver='this.style.backgroundImage=url({$button_image2->url})' onMouseOut='this.style.backgroundImage=url({$button_image->url})'> </div> </a>"; }; ($variables leads to an unpublished page with several fields I want to have easy access to, and is defined in _init.php.)
      However, this gives me the following result:

      I have no idea where the extra <p>'s come from. The URL field has the 'HTML Entity Encoder' turned on. What's even weirder is that the HTML source file seemingly renders correctly:
      <p> <a href='https://www.facebook.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1045/icons-facebook-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square.0x80.png)"'> </div> </a> <a href='https://www.instagram.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1046/icons-instagram-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square.0x80.png)"'> </div> </a> </p> Removing the JavaScript has no effect. I'm probably missing something obvious, but am at a loss here.
       
      Thank you in advance!
    • 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.
×
×
  • Create New...