Jump to content


Photo

images->path files->path


  • Please log in to reply
3 replies to this topic

#1 jamienk

jamienk

    Newbie

  • Members
  • Pip
  • 6 posts
  • 1

Posted 19 April 2012 - 05:38 AM

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

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,188 posts
  • 1745

  • LocationSH, Switzerland

Posted 19 April 2012 - 07:13 AM

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.

@somartist | modules created | support me, flattr my work flattr.com


#3 jamienk

jamienk

    Newbie

  • Members
  • Pip
  • 6 posts
  • 1

Posted 19 April 2012 - 01:47 PM

Thanks. I thought "image" was some sort of built-in object -- I didn't realize it was a field with methods based on the "file" type

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3116

  • LocationAtlanta, GA

Posted 19 April 2012 - 01:48 PM

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;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users