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

  • 2 years later...
On 7/31/2023 at 2:03 PM, Bike said:

Hello everyone,

I have the exact same issue right now. I'm just wondering where to put this piece of code?!

Thanks for any help!

The code injects any files uploaded to the page into the field with name files. You go edit the page in question and paste this into the tracy debugger console. Be aware that you need to change any $page->files to match your field name e.g. $page->my_field_name

Also, this only works correctly if there is only one Images / Fields field present in the template (if there are multiple, the field would just contain all the files ever uploaded to the page in question).

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

×
×
  • Create New...