Jump to content

Create Pages (with File-Upload Field) via API


MatthewSchenker

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
Link to comment
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!

Link to comment
Share on other sites

  • 1 year later...

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);
    }
Link to comment
Share on other sites

  • 2 weeks later...

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
Link to comment
Share on other sites

  • 4 weeks later...

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.

Link to comment
Share on other sites

  • 5 months later...

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.

 

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
×
×
  • Create New...