Link a js on a PHP - php

Hi I'm a Amateur programer and I need help for my web's programer subject. I'm trying to link this js but on the page show me this on the top Warning: file_get_contents(script404.js): Failed to open stream: No such file or directory in C:\xampp\apps\wordpress\htdocs\wp-content\themes\villanueva-s_theme-brown_nights-\404.php on line 58
<?php
$script = file_get_contents('script404.js');
echo "<script>".$script."</script>";
?>

You must have an invalid path.
You could use __DIR__.'/script404.js' if this file is in the same folder as script you running.
See Evert's answer in Why include __DIR__ in the require_once?

You can just use:
echo "<script src=\"script404.js\"></script>";
Or, you should point to the right directory, like:
$script = file_get_contents('C:/path/to/script404.js');
echo "<script>".$script."</script>";
Or, I wouldn't advice you to do this, but if it is in the same directory:
$script = file_get_contents('./script404.js');
echo "<script>".$script."</script>";

Related

In php code, use shell_exec correctly in my case

I have question about how to save log in correctly directory. My php code:
public static function log(test $testination) {
echo($testination->getDetails()."\n");
$log_filename = shell_exec("cd; ~/log/");
$log_filename2 = "log";
if (!file_exists($log_filename2))
{
mkdir($log_filename, 0777, true);
}
$log_file_data =$log_filename.'/logs_' . date('d-M-Y') . '.log';
file_put_contents($log_file_data, var_dump($testination) . "\n", FILE_APPEND);
}
in this code, I create a folder in the same place as my PHP file, then create folder log if this not exist.
I want to save in begining (just cd) then create log folder if this not exist I try this code
$log_filename = shell_exec("cd; ~/log/");
i get warning/error
PHP Warning: file_put_contents(/logs_18-Aug-2020.log): failed to open stream: Permission denied in /test/test/Helper.php on line 318
Any idea how to do it? Please im stuck.
Any help will be appreciated.
That's not how you should use file_put_contents: it does not know anything about that attempt to change the directory, as this will not affect the execution of PHP. If you want to get the directory of the current PHP file, you can use basename(__DIR__)
Additionall, cd; ~/log/ won't work in your shell as well.

Problems with accessing files

Hello stackoverflow community, how can i solve this problem, here it is my error:
Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
the that query.class.php is in the folder:
$_SERVER['DOCUMENT_ROOT']/classes/query.class.php
Im trying to access it from:
$_SERVER['DOCUMENT_ROOT']/modules/tasks/ajax/file.php here.
When i try to use:
require_once($_SERVER['DOCUMENT_ROOT'].'/classes/query.class.php'):
It works like a charm. But i need to acces that file using DP_BASE_DIR. So when i write:
require_once DP_BASE_DIR."/classes/query.class.php";
I get an error. How can i solve this problem?
Try to see if this makes sense to you, I have tested this out using:
test.php
<?php
define('DP_BASE_DIR', '/var/www/devspace');
if (!(defined('DP_BASE_DIR'))) { die('This file should not be called directly.'); }
require DP_BASE_DIR."/test2.php";
var_dump($string);exit;
// outputs string(4) "test"
?>
test2.php
<?php
$string = 'test';
?>
Isn't that exactly what you're doing? I feel that something else is coming into play here and being left out of the info that has been provided.
The only real issue that I could spot here is that maybe $_SERVER['DOCUMENT_ROOT'] did not hold the exact same value as your DP_BASE_DIR but as you have confirmed these to be the same I am scratching my head about other possibilities as this should just work.
EDIT:
do this right before the require:
$test = DP_BASE_DIR."/classes/query.class.php";
var_dump($test);exit;
Post the result?
Warning: require_once(DP_BASE_DIR/classes/query.class.php):
failed to open stream: No such file or directory in
Seems DP_BASE_DIR constant does not exists. If constant does not exists will use the name of constant, and you get a notice.
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.

php __autoload() is not working

I haven't seen any error in my files but when I run my code it shows me the following error:
Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7
Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7
My code is:
classes/Core.php
<?php
class Core {
public function run() {
ob_start();
require_once(Url::getPage());
ob_get_flush();
}
}
inc/autoload.php
<?php
require_once('config.php');
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
index.php
<?php
require_once('inc/autoload.php');
$core = new Core();
$core->run();
I know it's a bit late. I was researching on this because I had the same problem. This is what i tried and it worked for me.
// this code is for the inc/autoload.php file.
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(dirname(__FILE__) . "/config.php");
//require_once(inc/config.php);
function __autoload($className){
$class = explode("_",$className);
$path = implode("/",$class).".php";
require_once($path);
}
Please I just started learning PHP and i stand corrected by you guys.
I hope this helps.
Your Core class is apparently defined at:
C:\xampp\htdocs\completed\classes\Core.php
But you attempt to load this file:
C:\xampp\htdocs\completed\Core.php
It isn't a good idea to build a class auto-loader that works with relative paths*. I suggest you add a prefix here to build an absolute path:
$path = implode("/", $class).".php";
E.g.:
$path = __DIR__ . '/../classes/' . implode("/", $class).".php";
(*) Among other reasons, because relative paths in PHP are relative to the main script (rather than the file when path usage happens) so the source directory depends on what script you load autoload.php from.
I've faced the same problem with yours. I think it's late for you. But may be it's helpful for others.
I didn't included the config.php file in autoload.php file
inc/autoload.php
//require_once('config.php');
I included config.php file in the index.php like this:
index.php
require_once('inc/config.php');
require_once('inc/autoload.php');
And it's worked fore me, I hope this for you, too :). Thanks for your time to read my comment.
because in same folder
require_once('inc/autoload.php');
require_once('config.php');
so.. i think may be should like this
require_once('inc/config.php');

file_put_contents: Failed to open stream, no such file or directory

I am trying to use dompdf to save a form to an easily-readable .pdf file, and my processing script is below. I am receiving the error Warning: file_put_contents(/files/grantapps/NAME0.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in /home/username/public_html/subdir/apps/dompdf/filename.php on line 39. (Line 39 is the final line in my full script.) I don't know how to resolve this issue.
allow_url_fopen is on, and I have tried all kinds of file paths, including the full directory (/home/username/public_html/subdir/files/grantapps/) and what's in the code below, but nothing worked.
require_once("dompdf_config.inc.php");
date_default_timezone_set('America/Detroit');
$html = 'code and whatnot is here';
$name = str_replace(" ", "", $_POST['form2name']);
$i = 0;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
while(file_exists("files/grantapps/" . $name . $i . ".pdf")) {
$i++;
}
file_put_contents("files/grantapps/" . $name . $i . ".pdf", $dompdf->output());
There is definitly a problem with the destination folder path.
Your above error message says, it wants to put the contents to a file in the directory /files/grantapps/, which would be beyond your vhost, but somewhere in the system (see the leading absolute slash )
You should double check:
Is the directory /home/username/public_html/files/grantapps/ really present.
Contains your loop and your file_put_contents-Statement the absolute path /home/username/public_html/files/grantapps/
I was also stuck on the same kind of problem and I followed the simple steps below.
Just get the exact url of the file to which you want to copy, for example:
http://www.test.com/test.txt (file to copy)
Then pass the exact absolute folder path with filename where you do want to write that file.
If you are on a Windows machine then
d:/xampp/htdocs/upload/test.txt
If you are on a Linux machine then
/var/www/html/upload/test.txt
You can get the document root with the PHP function $_SERVER['DOCUMENT_ROOT'].

PHP4 problems with include() within a file created by fwrite()

I have a file called generator.php that uses fwrite() to create a result.php on the server (Apache, PHP4).
One of the lines in result.php is a PHP include() statement.
So, in generator.php:
if (!is_file($fname)){
$resultfile = fopen($current_path . "/" . $fname, "w+");
}
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "'. '/inc/footer.php"); ?>' . "\n");
fclose($resultfile);
chmod($current_path . "/" . $fname, 0755);
And in result.php:
<h2>Sponsored Links</h2>
<!-- begin sidebar_top ad -->
<?php echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php"); ?>
<!-- end sidebar_top ad -->
But that include() statement doesn't work when I visit result.php in a browser. The echo statement does, so I know the path is correct.
Another test.php with the same code, which I uploaded using FTP into the same folder, works fine.
The code in the same in both files, when recovered via FTP.
In test.php: (works, echoes and includes correctly.)
<?php
echo $_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php" . "<hr />";
include($_SERVER['DOCUMENT_ROOT'] . "/ads/sidebar_top.php");
?>
Any idea why the include() is working in test.php (created manually) and not in result.php (created using fwrite()), when both are in the same folder?
The only differences I know of between the files:
Owner could be different (wouldn't result.php be created by user nobody?)
Permissions are originally different. FTP'd file (working) is 0775, while the ones created using fwrite() (include not working) had 664, and is chmoded by the generator.php to 0775.
Working test.php file was edited on a Mac with Smultron and uploaded via FTP, while result.php was created by fwrite() in generator.php on Linux, called from a browser.
fwrite($resultfile, '<?php include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); ?>' . "\n");
you had an extra " in there i think
When PHP4 safe mode is on, the result.php, being written by another uid, cannot not access the included file, which belongs to another uid.
SAFE MODE Restriction in effect. The script whose uid is 48 is not allowed to access
/var/www/vhosts/example.com/httpdocs/ads/sidebar_top.php owned by uid 10010
in /var/www/vhosts/example.com/httpdocs/results/result.php on line 130
I resolved this by opening php.ini and changing to safe_mode_gid = On, and adding my includes directory to safe_mode_include_dir.
I also had to restart Apache to let the changes take effect.

Categories