Jump to content
MatthewSchenker

Create Pages (with File-Upload Field) via API

Recommended Posts

@videokid: ok that with readability is personal. agread

but could you explain why that did the trick for me?

 
but what trick does it?
 

if($upload_path.$filename !== $upload_path . $filename) echo 'Horst is wrong!';
else echo 'This is nonsense!';
  • Like 2

Share this post


Link to post
Share on other sites

The dot concatenates the right and left variables (strings), no matter when you use spaces or not. 

oftopic:

// 'concatenating numbers'
echo  6 . 6; //echo's the string 66
echo 6.6;    //echo's the float 6.6
  • Like 1

Share this post


Link to post
Share on other sites

I was always under the impression it didn't matter...

I've done some testing and in the end it seems

'This is nonsense! Horst is right'

however, putting back the spaces results in a not deleted file in the temp directory.... without the spaces... woosh temp file gone...  ???

Debug time!

Share this post


Link to post
Share on other sites

Hello Matthew

I saw in your code  under  "HANDLING MULTIPLE FILE-UPLOAD FIELDS IN THE SAME FORM".. I have done this by adding multiple in HTML form. This way there was no need to keep extra fields and run extra executes (Run foreach). Every thing was working fine until I put the file field inside a repeater. Now the new file is replacing older one inside repeater.

I am searching for solution but No luck. Any help is much appreciated.

here is my code:

$p2= pages->get($id);
$pdfs = new WireUpload('f_plan');
    $pdfs->setMaxFiles(8);
    $pdfs->setOverwrite(false);
    $pdfs->setDestinationPath($upload_path);
    $pdfs->setValidExtensions(array('pdf','xps'));

// execute upload and check for errors
    $pdf_files = $pdfs->execute();
    echo count($pdf_files); //count is showing number of files
    if(!count($pdf_files))
    {
    $pdfs->error("Sorry, but you need to add a file!");
    return false;
    }
    
    foreach($pdf_files as $filename)
    {
    $p2->of(false);
    $new_rep = $p2->floor_plan_rep->getNew(); //floor_plan_rep is my repeater with one file field inside named floor_plan.
    $p2->save();
    echo $pathname = $upload_path . $filename; //all the urls are okey
    $new_rep->floor_plan = $pathname;
    $p2->save();
    $p2->of(true);
    unlink($pathname);
    }

Share this post


Link to post
Share on other sites

After a few days of research I found this solution for my above problem.

Complete code is--

<?php

if ( isset($_POST['submit']) ) {

 $upload_path = $config->paths->templates . "images/";
$pdfs = new WireUpload('upload');
    $pdfs->setMaxFiles(8);
    $pdfs->setOverwrite(false);
    $pdfs->setDestinationPath($upload_path);
    $pdfs->setValidExtensions(array('pdf','xps','pptx'));

// execute upload and check for errors
   $pdf_files = $pdfs->execute();

    if(!count($pdf_files))
    {
    $pdfs->error("Sorry, but you need to add a photo!");
    return false;
    }
    $rep_id = array();
         $page->setOutputFormatting(false);
    foreach($pdf_files as $pdf){

        $building = $page->test_rep->getNew();
        $building->save();
        $rep_id[] = $building->id;
        $page->test_rep->add($building);

    }
            $page->save();
            $page->setOutputFormatting(true);



     if(count($rep_id) === count($pdf_files)){
        $count = 0;

        foreach($pdf_files as $pdf){
            $page->setOutputFormatting(false);
            $repeater_id = $rep_id[$count];
            $field_rep = $page->test_rep->get("id=$repeater_id"); // test_rep is table and test_rep_field is field of the table
            $field_rep->of(false);
            $field_rep->test_rep_field = $upload_path.$pdf;
            $field_rep->save();
            $page->save("test_rep");
            $page->setOutputFormatting(true);
            $count++;
        }

     }


  }
?>

<form action="<?php $_SERVER['PHP_SELF'] ;?>" method="post" enctype="multipart/form-data">

<p>Click the "Select Files" button below to upload your photo.</p>
<input type="file" name="upload[]" multiple />
<button type="submit" name="submit">Submit</button>
</form>

I hope this will help someone while uploading files in repeater fields.

  • Like 2

Share this post


Link to post
Share on other sites

How can i check if the user has choosen a valid file?

For example: User choose pdf but allowed is only jpg.

Share this post


Link to post
Share on other sites

How can i check if the user has choosen a valid file?

For example: User choose pdf but allowed is only jpg.

Check or prevent? If prevent:

$u = new WireUpload('files');
$u->setValidExtensions(array('pdf','xps','pptx'));
// etc..

Share this post


Link to post
Share on other sites

I used the code from here and modified it. And i have this:

// New wire upload
$service_upload = new WireUpload('service_file_upload_sw'); // References the name of the field in the HTML form that uploads the photo
$service_upload->setMaxFiles(5);
$service_upload->setOverwrite(false);
$service_upload->setDestinationPath($upload_path);
$service_upload->setValidExtensions(array('jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif'));

When i now choose a pdf file and hit the submit button then there is no wrong file message.

Share this post


Link to post
Share on other sites

Hello Marcel,

in my case I had two level of validation,first via html form pattern,Second the above one. try to get error message with 

$service_upload->error();

Also if it dosent help get file names and extension with core php

$file_ext = $_FILES['my-file']['type'] ;

and check for right file.

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By marcus
      wireshell 1.0.0 is out    
      See Bea's post
       


      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By benbyf
      I have a a form in my site footer that can be accessed anywhere on site, I've added the form in the _inc.php file and added the render in the pages footer.php. However, this works well on the homepage e.g. you can submit said form and get a thank you on reload, doesnt work at all on other pages... Just lots like a fresh reload. Any thing im doing wrong here or ways to diagnose as there isn't an error log for formbuilder etc...?
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...