Jump to content

Upload file to files field via ftp


joeck
 Share

Recommended Posts

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);

 

Link to comment
Share on other sites

Is your echo showing the file? I would have used:

$directory = $config->paths->assets . "files/1048/"; // files uploaded here via ftp

or maybe wire()->config ... depending on context

Is 'file' the name of your files field?

Link to comment
Share on other sites

yes the echo is showing the file name (handout.pdf).

one issue I see is that the file name in the PW "file" field is all lowercase however the name of the file is not.
I tried renaming the file to the newly added entry in the array of the "file" field but that didn't work either.

echo rename($directory . '/' . $file,$directory . '/' . $page->file->last()->name);

I then saw the entry in the "file" field in PW and it always showed 1kB as the size. When clicking on the name to open the document in a new tab the path would be correct and a loading animation appears but the file is not shown. Not sure why.

Link to comment
Share on other sites

Yes absolutely! I am using Tracy Debuggers console to try the script but I wasn't sure if it is at all possible what I'm trying to achieve and if my assumption was correct.

Quote

I'm assuming the files field is just an array with the file names that are then searched in site/assets/files/1048

But I'll try some more debugging.

Link to comment
Share on other sites

Turns out the pdf file was actually broken and the code was working just fine...

For anyone else who might end up here, this is the code I used (in Tracy Debugger console):

// $page->files targets the field on the page with name "file". Change name with your own
$directory = $directory = $config->paths->assets . "files/$page->id/"; //path to asset folder of current page
$page->of(false);
foreach (scandir($directory) as $file) { // loop through all files in directory
    if ($file !== '.' && $file !== '..') {
    if ($page->files->has($file)) { continue;} //skip if file is already in field
    $page->files->append($file); //append file to field
    $files->rename($directory . $file, $directory . $page->files->last()->name); //rename original file to name in field after sanitizer
    }
}
$page->save();
$page->of(true);

thanks @MarkE for your assistance

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 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 Marvin
      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
    • By theoretic
      Hi there! And thanks for Processwire!
      It appears there's a possible bug in Processwire 3.0.170 concerning file and/or image inputfield. Creating such a field results in the following error:

      Fatal Error: Uncaught Error: Call to a member function get() on null
      The inputfield is created however. The closer look reveals a problem at line 60 in wire\modules\Fieldtype\FieldtypeFile\config.php:
      if(!$value) $value = $fieldtype->get('defaultFileExtensions'); Commenting this line removes the problem, but the newly created inputfield requires 'Allowed file extensions' config option to be set (which is rather expectable since i commented the above-cited line of code). Never faced this problem before, hope it can be resolved.
    • By xportde
      How does one enable executables to be uploaded to the File field? Just adding the file extension to the accepted extensions does not seem to work.

      screenshot attached (ignore the label 'video')

    • By GradDev
      I have a file field named "resume". I want to delete the file saved in it using the API.
      The field is limited to only 1 upload of file and the return type is set to "Automatic" which means it will return a single object if a file is available.
      Now there are two options that I have tried- delete() and deleteAll().
      $page_to_change = $pages->get("name=xyz"); $page_to_change->resume->deleteAll(); //OR //$page_to_change->resume->delete($page_to_change->resume); $page_to_change->of(false); $page_to_change->save(); Both of these give an error like-
      Error: Exception: Method Pagefile::deleteAll does not exist or is not callable in this context(...) I understand that this error is because these two functions work on a wirearray rather than on a file object.
      But how do I delete a single file?
×
×
  • Create New...