Ok. I was getting errors because the variables I was using were not defined in core.functions.php I have gone away from using variables for the <a> record and just implemented Mark’s code and it works for only one entry. I also tried to wrap my own <a> record around it, but when I do outside the php, the file name seem to disappear. And if the array worked for more than one entry the <a> records won’t. Any ideas? Thanks
Ok. I was getting errors because the variables I was using were not defined in core.functions.php
When I get those errors, I usually look at the PHP I have on the template… It kinda sounds like the PHP code you have implemented on the template page is funky, not core.functions.php.
I have gone away from using variables for the <a> record and just implemented Mark’s code and it works for only one entry. I also tried to wrap my own <a> record around it, but when I do outside the php, the file name seem to disappear. And if the array worked for more than one entry the <a> records won’t. Any ideas? Thanks
I quickly wrote this function:
function get_file($x) {
if(!empty($x)) {
$cust = $x; // Name of custom field.
$parts = explode(',', $cust) // array of file name
$junk = array_pop($parts) // Get rid of last key.
foreach($parts as $part) { // Loop through array and echo results.
$filename = basename($part) // name of file with extension.
$result = explode('.', $filename) // $result[0] = file name, $result[1] = extension.
echo 'File name without extension: '.$result[0].' (ext: '.$result[1].')'."\n"; // You could add to, and return an array of values.
}
} else { echo 'Error: Make sure you uploaded files.'; }
}
EDIT: Forum is messing with my code! Stupid html entities!
I put that in a file called functions.inc.php and put in a folder called “includes” on my server root.
Then, at the top of my template, I call the above file like so:
<?php
$doc_root = $_SERVER['DOCUMENT_ROOT'];
include($doc_root.'/includes/functions.inc.php')
...
...
// Other PHP includes and such here.
...
...
?>
Then on my template prefs, I allow PHP and set the “PHP Parsing Stage” to “output”.
Next, within my weblog entries tag, I call the function like so:
<?php get_file('{article_images}{article_image},{/article_images}') ?>
(Note: Notice the comma?)
Above code will produce this:
File name without extension: wtf (ext: jpg)
File name without extension: wtf_1 (ext: jpg)
File name without extension: rightquote (ext: gif)
File name without extension: leftquote (ext: gif)
I hope that helps… The function could be more robust and do other things… Maybe a bit more secure and few more error checking features? Also, working with PHP on INPUT might make things easier – Test it out for yourself. ;)
Hth’s. Micky
EDIT/UPDATE: Hmm, looks like setting the PHP Parsing Stage to INPUT makes no difference when it comes to the list of files (I was hoping I could get an array of files straight from the File Extension)… Below, I set my template to allow PHP with the PHP Parsing Stage set to INPUT:
function get_file2($x) {
if(!empty($x)) {
$cust = $x; // Name of custom field.
print_r($cust); // Does PHP set to input make a difference? Will it give us an array vs. a string?
} else { echo 'Error: Make sure you uploaded files.'; }
}
...
<html stuff>
...
<?php get_file2('{article_images}{article_image}{/article_images}'); ?>
...
The output:
http://www.mysite.com/images/articles/wtf.jpghttp://www.mysite.com/images/articles/wtf_1.jpghttp://www.mysite.com/images/articles/rightquote.gifhttp://www.mysite.com/images/articles/leftquote.gif
FYI… This also works (PHP on, and Parsing set to OUTPUT):
<?php
function get_file2($x) {
if(!empty($x)) {
foreach($x as $part) { // Loop through array and echo results.
$filename = basename($part); // name of file with extension.
$result = explode('.', $filename); // $result[0] = file name, $result[1] = extension.
echo 'File name without extension: '.$result[0].' (ext: '.$result[1].')'."\n"; // You could add to, and return an array of values.
}
} else { echo 'Error: Make sure you uploaded files.'; }
}
?>
...
..<template>...
..<template>...
...
{exp:weblog:entries}
<?php $hldr = array(); // Initialize array. ?>
{article_images}<?php $hldr[] .= '{article_image}'; ?>{/article_images}
<?php get_file2($hldr); ?>
{/exp:weblog:entries}
Outputs this:
File name without extension: wtf (ext: jpg)
File name without extension: wtf_1 (ext: jpg)
File name without extension: rightquote (ext: gif)
File name without extension: leftquote (ext: gif)
EDIT: Even more simple of an approach (with same template settings as above):
function get_file3($x) {
$filename = basename($x); // name of file with extension.
$result = explode('.', $filename); // $result[0] = file name, $result[1] = extension.
echo 'File name without extension: '.$result[0].' (ext: '.$result[1].')'."\n";
}
...
..<template>...
..<template>...
...
{exp:weblog:entries}
{article_images}<?php get_file3('{article_image}'); ?>{/article_images}
{/exp:weblog:entries}
Dear Wild Garden, Thanks so much, I implemented the EDIT approach and it works perfectly. Yep, my php code was causing the errors, not functions.php. I forgot to include the \ in front of each ” in my <a> record. Regardless, your solution is perfect for me. Thanks so much. D [EDIT] Sorry for the repeat
MySQL ERROR: Error Number: 1054 Description: Unknown column 'field_id_1_img' in 'field list'
Is any body else getting SQL errors with ‘_img’ using SAEF? The query: INSERT INTO, is trying to insert into a custom field name with _img . The _img should be removed in the extension. When publishing from the control panel, everything works fine.
[EDIT] I followed the advice before on just copying the html (and JS addrows) from the CP publish page. But forgot to edit mod.weblog_standalone.php. I’m sorry for not looking deep enough. Thanks so much Mark.
This is what shows up when I try to use this extension:
{multi_relatedfiles}{multi_relatedfiles}{/multi_relatedfiles}
This is my template src:
{multi_relatedfiles}<a href="http://{multi_relatedfiles}">{multi_relatedfiles}</a>{/multi_relatedfiles}
My field name is ‘relatedfiles’. I have multiple files enabled in the extension settings. I have re-enabled the extension, checked file upload paths, and fixed the warning notice as advised.
Any idea as to why it’s not showing up?
Hmmm, thanks Wild Garden, however when I try that:
{relatedfiles}<a href="http://{relatedfile}">{relatedfile}</a>{/relatedfiles}
All I get is this:
http://www.tear.org.au.php5-1.websitetestlink.com/uploads/resources/0{relatedfile}{/relatedfiles}
And the source code shows this:
http://www.tear.org.au.php5-1.websitetestlink.com/uploads/resources/0<a href="http://{relatedfile}">{relatedfile}</a>{/relatedfiles}
Weird?
Also, I don’t suppose there is a way to test if the field is not empty?
TY
Also, I don’t suppose there is a way to test if the field is not empty?
I just had a chance to test, and these work for me (using a multiple upload file field):
{if article_image}
<div id="articleImage">
...code here...
</div>
{/if}
{if article_image != ""}
<div id="articleImage">
...code here...
</div>
{/if}
First, kudos to Mark…great extension (makes life easy on the clients)…thank you!
Second, I can’t seem to troubleshoot this error:
Warning: getimagesize(): open_basedir restriction in effect. File(/tmp/phpAVqzh4) is not within the allowed path(s): (/vservers/hlwiker) in /vservers/hlwiker/htdocs/system/extensions/ext.file.php on line 492
Warning: getimagesize(/tmp/phpAVqzh4): failed to open stream: Operation not permitted in /vservers/hlwiker/htdocs/system/extensions/ext.file.php on line 492
Warning: Cannot modify header information - headers already sent by (output started at /vservers/hlwiker/htdocs/system/extensions/ext.file.php:492) in /vservers/hlwiker/htdocs/system/core/core.functions.php on line 710
Have you (or anyone) seen this. Thoughs? Suggestions?
This error occurs after I upload an image and submit the entry changes.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.