Jump to content

Using $mypage->new page() - is there a way to upload a file?


John W.
 Share

Recommended Posts

$mypage = new Page();
$mypage->template  = "song";
$mypage->parent = $pages->get("/songs"); // root
$mypage->title = "My New Song";
$mypage->song_number = "004";
$mypage->song_commentary ="this is the commentary";
$mypage->save();

 

Using the example above I was wondering if there is a way to supply the name of an mp3 file and when $mypage->save() executes the mp3 will be added to the processwire database and uploaded into its assets folder?

For Example:

I have a field song_mp3_file which is a File Type field.

I have temporary copied all the mp3 files in templates/temp/

song1.mp3

song2.mp3

If I set a value like this, will PW know it is a File Field type and automatically upload it to its assets folder and add it to the database?

$mypage->song_mp3_file = "temp/song1.mp3"

 

The above code (at the top of the post) works fine to quickly add records, I just don't want to have to go into every song's page and manually upload each mp3.

Thanks!

 

 

Link to comment
Share on other sites

You will have to tell ProcessWire to add that file to the file field and supply an absolute path (I don't think a relative path will work). See @adrian's example here if you want to add multiple files or my example here if adding single file. In my example, the file could also be in a sub-directory and the code will still work (i.e., it's recursive). 

Here's some code (it's not recursive and it's not namespaced). Note that you will have to first create the page and save, then add the file(s) and save again. Also note this will add all songs in the /temp/ folder to the one page. If you want different songs in different pages we would have to change the logic.

 

// your previous (create page) code here
$path = $config->paths->templates . 'temp';
$dir = new DirectoryIterator($path);
foreach($dir as $songFile) {
	if($songFile->isDir() || $songFile->isDot()) continue;// skip system files and directories/folders
	$mypage->song_mp3_file->add($songFile->getPathname());
	unlink($songFile->getPathname());// if you want to delete the mp3 files in the temporary folder
}
// save the page again
$mypage->save('song_mp3_file');
//rmdir($dir);// if you wish to remove the temp directory

Untested, but should work

 

 

  • Like 2
Link to comment
Share on other sites

Thanks. I tested it and it uploaded the mp3 from a temp folder then added a new record.

Final (snipped from my) code:

$mp3file =  'temp/' . 'song1.mp3'
$mypage->save();
$mypage->song_mp3->add($mp3file);
$mypage->save();

Link to comment
Share on other sites

I do have another quick question.

Basically when a user edits a song there is a drop down lists of artist names which the data comes from a Page type field called song_artist_selections.

I was wondering when bulk adding records how to set the value of this field?

 pseudo code

$mypage = new Page();

$artistname ='Elvis';

$mypage->set the song_artist_selection to 'Elvis'

... 

$mypage->save()

 

 

P.S. where in the docs did you find information on the add() method?  e.g. $mypage->song_mp3->add($mp3file); ?

Thanks again.

Link to comment
Share on other sites

Use the method 'add:).

$mypage = new Page();
// other code...for $mypage template, parent, title, etc...

// find songs to add
$songSelections = $pages->find('template=song-selections, limit=50');
// if we found songs
if($songSelections->count()) {
	foreach ($songSelections as $song) {
		$mypage->song_artist_selections->add($song);
	}
}

$mypage->save();

References:

  1. http://processwire.com/api/ref/page-array/add/ (of main relevance in your case; see the examples on that page as well)
  2. http://processwire.com/api/ref/page-array/
  3. http://processwire.com/api/arrays/page/
  4. http://processwire.com/api/ref/wire-array/add/ (this and below, mainly FYI)
  5. http://processwire.com/api/ref/wire-array/
  6. http://processwire.com/api/arrays/
  • Like 1
Link to comment
Share on other sites

Hi @holmescreek

I see you are working with mp3 files. One of my first (learning)-projects with PW was a local Music-DB with importfunctions of ID3-tags into page fields. If this is of interest for you, here you can find some examples and libs:

(function to retrieve infos from a mp3 file)

https://github.com/horst-n/LocalAudioFiles/blob/master/LocalAudioFilesImportShellScript.php#L108

(the libs that are used in the importer class)
https://github.com/horst-n/LocalAudioFiles/tree/master/site-default/modules/LocalAudioFiles

(the old project page here in forum)
https://processwire.com/talk/topic/3604-release-localaudiofiles-mp3-db/

(a online demo, where you can log in as "myusername" with the buttons top-right, and inspect the Admin, - how pages and sections are sorted with the auto-importer)
http://pwlaf.biriba.de/dbinfo/
 

 

 

  • Like 3
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...