Jump to content

images->path files->path


jamienk
 Share

Recommended Posts

I've upgraded to PW 2.2 and some of my code has stopped working. I allow my authenticated users to upload pdfs, but I can no longer upload or get the path or url of the uploads.

This used to work (edited code to only show the relevent part):

$newpage = new Page();
$path=$newpage->images->path;
$relpath=$newpage->images->url;

Then I save the page with the correct fields filled in. Then I upload the file:

$name = strtolower($sanitizer->name($_FILES['upload']['name']));
$file = $path . $name;
if(move_uploaded_file($_FILES['upload']['tmp_name'], $file)) {
	// add it to the page
	$newpage->report_request->add($file);
	/// save the page
	$newpage->save();	  
}

But now $path and $relpath don't return anything. I get the error "Notice: Trying to get property of non-object" which I think refers to images->path and images->url

I've tried files->path and files->url (in some of the sample code from these forums I picked up that possibility), but that doesn't work either.

How do I access the filesystem path and the url path, both to upload the file and to refer to it?

Thank you,

Jamie

Link to comment
Share on other sites

Hi jamienk and welcome!

I can't reproduce your issue with $page->image->url/path. They still work as expected in latest PW. I don't see what could be wrong with your code, just confirming that it works.

Edit: Either your page or most likely field "images" doesn't seem to exists/work in your created page.

Link to comment
Share on other sites

I think what's missing here is that $newpage doesn't yet have a template assigned, or an ID, so technically there is no images/files path that exists yet. You would need to assign a template to $newpage before it would even have an images field, and then you'd need to save the $newpage before it would create any directories for file storage.

$newpage = new Page();
$newpage->template = 'template_with_images_field';
$newpage->parent = '/';
$newpage->title = 'New Page';
$newpage->save();

// now these should work.
echo $newpage->images->path; 
echo $newpage->images->url;
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.
×
×
  • Create New...