Jump to content

[SOLVED] Upload an image file and document file at the same time from a form


Marvin
 Share

Recommended Posts

Hi,

My name is Marvin, and i want to ask about a processwire, i'm new at processwire, and want to learn make a website using a php and processwire, but i have a problem. My problem is, when i want to uploading an image file, and a document file like a pdf, or doc, but every time i upload it, my image didn't upload to the back-end, just doc file was uploaded to the back-end. Any suggestion where i was wrong? Thank you

NB : i attach my code below
This is for uploading process

<?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'));
      $images = $newImg->execute();

      if(!count($images)) {
        $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 if for a form

<div class="frow-container">
  <form method="post" enctype="multipart/form-data">
    <div class="frow mt-10">
      <div class="col-md-1-1">
        <a href="<?= $pages->get("/files/")->httpUrl ?>"><i class="fas fa-arrow-left"></i> Kembali ke Files</a>
      </div>
      <div class="frow gutters col-md-1-1">
        <div class="col-md-1-2 mt-15">
          <label>Original Song Title <input type="text" name="original"></label>
          <label>Indonesia Song Title <input type="text" name="indonesia"></label>
          <label>Other Song Title <input type="text" name="other"></label>
          <label>Composer <input type="text" name="composer"></label>
          <label>File <input type="file" name="song_files"></label>
        </div>
        <div class="col-md-1-2 mt-15">
          <label>Youtube Video Code <input type="text" name="video"></label>
          <label>Audio Video Code <input type="text" name="audio"></label>
          <label>Image <input type="file" id="attach" name="img_files" accept="image/jpg,image/jpeg/,image/gif,image/png"></label>
        </div>
        <input type="submit" name="submit" value="Save">
      </div>
    </div>
  </form>
</div>

Thank you very much for all help

  • Like 1
Link to comment
Share on other sites

Your code processes both the song_files and img_files uploads, but only adds the song_files to the newly created page. This part of your code adds the song_files upload to the files field of the new page:

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

You need something similiar to add the uploaded $images to the page.

As a sidenote, duplicating code like this is not ideal. It increases the verbosity of your code and thereby makes it harder to spot errors like this one. See how the sections of your code processing $newImage and $newFile are almost identical? A better approach than duplicating that code would be to write one or two reusable functions that perform the necessary steps (processing the uploaded file as a WireUpload and adding it to the page). You can use function arguments to express the differences between the two uploads (name of the upload field, accepted file types, name of the page field to store the upload in). This reduces duplication, makes your code shorter and easier to understand and maintain.

  • Like 1
Link to comment
Share on other sites

Hi @MoritzLost

Sorry my late reply, i was busy yesterday. Thank you for your suggest i will report i later, so i need to create a function for proccessing $newImg and $newFile then Uploading with create $newPage at the same time

 

Thank you very much, i will try it and make an update very soon

Link to comment
Share on other sites

Hi @MoritzLost,

i has solved a problem, thanks for your solution, it's very help me. After i saw my code again, i realise that no function to proccessing an image and after i  make it, the image is uploading, but i still create another function, so i can upload a $newImg and $newFiles with one function. Thank you for your help

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hi @Marco Ro, sorry for may late reply, yes i can share it

this is my function to upload an image and a file at the same time

 

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'));
      $images = $newImg->execute();

      if(!count($images)) {
        $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->text_4 = $video;
      $newPage->text_5 = $audio;
      $newPage->of(false);
      $newPage->save();

      foreach($files as $filename) {
        $filepath = $upload_path . '/' . $filename;
        $newPage->files->add($filepath);
        $newPage->message("Add file : $filename");
        unlink($filepath);
      }
      foreach($images as $filename) {  //add this foreach iteration to upload an image
        $filepath = $upload_path.'/'.$filename;
        $newPage->images->add($filepath);
        $newPage->message("Add Image : $filename");
        unlink($filepath);
      }
      $newPage->save();
    }

 

Edited by Marvin
Add some comment on code
  • Thanks 1
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.
×
×
  • Create New...