Jump to content

Need help with deleting files


Valery
 Share

Recommended Posts

Hello guys,

I am setting up a page for news publishers to be able to upload PDFs. Some forum browsing helped a lot with gile uploads (thanks, Ryan!).

However, I ran into a different kind of problem: I can add files to pages just fine but I do not know how to replace/delete them after!

Let's say I have uploaded a file to a page. I can see it in the admin backend. Then I try to delete it using an API call ("file" is the name of a file field that I added to the basic-page template):

$wire->pages->get("$refererPageId")->file->pagefiles->delete();

Doesn't work. Says, Error Call to a member function delete() on a non-object

$wire->pages->get("$refererPageId")->file->delete();

does not work either.

I've run out of ideas, and looking through the code did not help (I know php just enough to be able to use pre-defined functions).

Could you please enlighten me on how to delete page files using the API

Thanks a lot in advance.

Link to comment
Share on other sites

Nico, yes. I even renamed the field to something inconspicuous but it did not help.

I try

$wire->pages->get("$refererPageId")->nn_basic_page_file->delete();

($refererPageId equals 1001 in this case)

However, PW says:

Error Uncaught exception 'WireException' with message 'Invalid type to Pagefiles::remove(item)' in /u/www/htdocs/wire/core/Pagefiles.php:189
Stack trace:
#0 /u/www/htdocs/wire/core/Pagefiles.php(175): Pagefiles->remove(NULL)
#1 [internal function]: Pagefiles->___delete()
#2 /u/www/htdocs/wire/core/Wire.php(271): call_user_func_array(Array, Array)
#3 /u/www/htdocs/wire/core/Wire.php(229): Wire->runHooks('delete', Array)
#4 /u/www/htdocs/f.php(68): Wire->__call('delete', Array)
#5 /u/www/htdocs/f.php(68): Pagefiles->delete()
#6 {main}
thrown (line 189 of /u/www/htdocs/wire/core/Pagefiles.php)
Link to comment
Share on other sites

Have a look into the cheatsheet and search for delete,

you get something like this: $files->delete($file)

damn Antti was faster

Thanks, Luis. What I am not getting is what kind of variable $files is. 

is it $page->files?

just $files?

@Apeisa,

Thanks for the hint. I tried something like

$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

The API cheatsheet says that pagefiles returns a $files array. But I keep getting the same error:

Error Call to a member function delete() on a non-object

Maybe somebody has a working code snippet that deletes a page file through the API?

Link to comment
Share on other sites

$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

Where do you get that pagefiles? Also, if you use delete(), you need to specify which file you are removing.

This might be what you are looking for:

$wire->pages->get(1001)->nn_basic_page_file->deleteAll(); 
  • Like 1
Link to comment
Share on other sites

$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

Leave out pagefiles, the name of your field is "nn_basic_page_file"

Try this:

//Delete the first file
$firstFile = $wire->pages->get(1001)->nn_basic_page_file->first();
$wire->pages->get(1001)->nn_basic_page_file->delete($firstFile);
  • Like 2
Link to comment
Share on other sites

apeisa, Wanze,

Thanks a lot for your help. deleteAll() worked, of course.

As did

foreach( $wire->pages->get(1001)->nn_basic_page_file as $value )
{

$wire->pages->get(1001)->nn_basic_page_file->delete($value);
}
 
However, I am still trying to understand how to delete files by ID, not by name. I know that $wire->pages->get(1001)->nn_basic_page_file is an array. Is there a way to access it by numeric keys? Like $wire->pages->get(1001)->nn_basic_page_file[0], for example?
Link to comment
Share on other sites

However, I am still trying to understand how to delete files by ID, not by name. I know that $wire->pages->get(1001)->nn_basic_page_file is an array. Is there a way to access it by numeric keys? Like $wire->pages->get(1001)->nn_basic_page_file[0], for example?

nn_basic_page_file contains Pagefiles object, which is inherited from WireArray, which is actually an object that PW uses pretty much everywhere. It is neat concept and makes working much faster than with regular PHP arrays. See more from here: http://processwire.com/api/arrays/

So from that link you find all the ways to find files from your field. Some examples:

//first, this makes it all nicer:
$p = wire('pages')->get(1001);

$first_file = $p->nn_basic_page_file->first(); // like wanze showed above
$second_file = $p->nn_basic_page_file->eq(1);
$random_file = $p->nn_basic_page_file->getRandom();
$specific = $p->nn_basic_page_file->get("filename=myphoto.jpg");
  • Like 4
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...