Note: This blog post is out dated and has not been updated. Please make your way to the PureEdit Community for any questions.
A few people are having a little trouble over at the PureEdit community trying to wrap their heads around displaying files on a PE front-end. There are a couple of topics on this subject so instead of handling both of them separately I felt like I would just address both of them in a blog post here.
What I plan to cover in this post is how PureEdit stores files in the Uploads Center, how PureEdit matches a file to a _file field, and how to display this _file onto the front-end.
Storing files in the Uploads Center.
The Uploads Center is one of the more advanced features PureEdit gives the user and the developer. From a user stand point it's pretty self explanatory...but if you are wanting to do some modification or front-end integration then it takes a little more PHP experience than just your echo basics.
As soon as you hit the upload new file button your actual file is stored in the proper categorized folder inside the /uploads directory. A mapping of each file is then created and stored in the database in the uploads table. PureEdit creates this mapping so that we can work/access these files however we wish to from the front-end.
Let's do a scenario to explain the above. Pretend we are uploading a file called michael.jpg and we want to attach it to an image gallery entry. Once we hit the upload new file button the file is immediately uploaded to the selected file type (by default you may choose images or misc). In our case we are going to choose images. Along with the file being uploaded to the proper folder, it is also added to the database inside the pe_uploads table as seen below:
===================================
| pe_uploads |
===================================
| id | file | folder |
===================================
| 1 | michael.jpg | images |
-----------------------------------
Attaching files to entries.
It is now time to attach this file with a PureEdit entry. We do this by clicking the attach link inside the Uploads Center. Even though we have attached a file to an entry now, we still need to save it, so proceed to save the open entry. When you hit save, PE takes the file from the pe_uploads and attaches ONLY the id of the file to the entries _file column as seen below:
=========================================================
| pe_gallery |
=========================================================
| id | title_txt | image_file | body_txtbox |
=========================================================
| 1 | Michael!! | 1 | Picture of Michael. |
---------------------------------------------------------
Selecting a file from the Uploads Center.
Now that we have gone over the technical side of how PE stores and attaches files, we need to know how to select these files from the Uploads Center for front-end use.
(it is assumed that you have already included the files necessary to connect to your database).
First start by selecting an entry from our database as below:
$getGallery = $Db->select("pe_gallery", 1);
$gallery = $Db->fetch_assoc($getGallery);
We can now access our entry by using variables such as:
echo $gallery ['title_txt']; // displays Michael!!
echo $gallery ['image_file']; // displays 1
The problem is, if you try to use the image_file variable you get an num value instead of a an actual file name. If you remember, PE stores the id of the file. So, to access the file name you need to follow the code below:
$getImage = $Db->select("pe_uploads", $gallery['image_file']);
$image = $Db->fetch_assoc($getImage);
You may now access the image details by using the following:
echo $image['file'];
echo $image['folder'];
In result, you can now populate your img tag by using the code below:
echo '<img src="uploads/file_name_here" border="0" alt="" />';
Conclusion.
From this tutorial, you should now know how to work with the way PureEdit stores and attaches files to entries from the Uploads Center.
If you have any further questions, either post a comment or direct your questions at the generous community.
Until next time, follow me on twitter, @michaeldick