PHP file not found error but it is including the file? - php

I have the following code to include a file that runs some SQL and other code
if (isset($_GET["vpnadd"])) {
echo "Swapping to VPN<br />";
$filename = '/home153/sub002/sc00083-LGVN/xxx.co.uk/addmyfile.php';
require $filename;
}
However, when I run the code, it gives an error of: error: No such file or directory
But, it does actually run the file and inserts a database record so I am not sure why it gives the file error?

Related

Including php file

I'm currently building a website using php. I'm trying to include a file named server in a file named User_Reg.php but when I try refreshing the website I get this error:
"This page isn't working. localhost is currently unable to handle this request. HTTP Error 500"
Here is the include statement.
<?php include('server.php'); ?>
I'm using MAMP local server environment.
When I remove the include statement, my code works fine.
1) Maybe you should debug this using the file_exists function
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
2) Debug your code inside server.php, maybe the error is coming from that page/functions.
3) Also, Try "./server.php"

PHP require_once fails but require works

In an application I am calling require_once $file where I have validated that the $file variable represents an absolute path, the file exists, and is readable. PHP 7.3 is giving me the dreaded: "Fatal error: require_once(): Failed opening required" message.
When I change the code to use "require" instead of "require_once" I get further, till the next "require_once" statement.
I am running PHP 7.2.19 on ubuntu 18.04.
This particular code is embedded deep within an application that has already included and required many different files at different times.
I have checked for obvious things like the particular file being included multiple times (it is not)... and including a different file for testing purposes, and that also consistently fails. I have double checked permissions, etc.
$file = "$destdir/lib/include.php";
$file = realpath($file);
if( is_file($file) && is_readable($file) ) {
require_once $file; // fails
// require $file; // works, but fails when require_once is called within this file.
// include $file; // works, but fails when require_once is called within this file
// include_once $file; // fails
}
I expected of course, that the file is required.

"No such file or directory" on localhost copy

EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.
EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.
I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).
Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.
That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:
// output build files
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
copy($source, $dest);
echo "Copy $source to $dest. <br>";
}
}
You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests.
You will have more luck if you change this:
copy(https:\\localhost\intranet\builder.php)
to this:
copy(C:\xampp\htdocs\intranet\builder.php)
EDIT
Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.
You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.
To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:
<?php
$hosted = "https://localhost/intranet/"; <--- UPDATED
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$path = str_replace("\\","/",$path); <--- ADDED
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
$content = file_get_contents(urlencode($source));
file_put_contents(str_replace(".php", ".html", $dest), $content);
echo "Copy $source to $dest. <br>";
}
}
In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.
Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.
EDIT
Added and revised the code a bit above.

Receiving unlink() error after it has been successful

this might be a really stupid question but I am getting the following error after the code has successfully deleted the file and I can not work out why, the code is very simple it gets the name and path of the file to be deleted from the database and then deletes it.
Code:
$getFiles = mysql_query("SELECT * FROM tempFiles WHERE pTID='$passedId'");
$numFiles = mysql_num_rows($getFiles);
for ($f=0;$f<$numFiles;$f++) {
$fileName = mysql_result($getFiles,$f,"fileName");
$deleteFile = "../../".$fileName;
unlink($deleteFile);
}
Warning: unlink(../../files/projects/files/643115.jpg): No such file or directory
The script for deleting the file is in a scripts/php/thefile and the file is in files/projects/files/thefile, so the ../../ is definitely needed and not the issue as far as I can tell. I know that the file is being deleted successfully because it is no longer in the folder after I run the script so I have no idea what is causing the error.
Any ideas why I might be getting the error?
Thank you in advance.
Possible causes to the error:
There are more than 1 record in the tempFiles table with the same fileName, so the first attempt removes it and the second causes the error.
The file didn't exists on the folder when you ran the script (as #AxelAmthor said on comment)
To solve it, just add a verification (as #Sammitch said on comment):
if (is_file($deleteFile)) {
unlink($deleteFile);
}

Reading file properties while using readdir() to get the files

I am trying to print file properties into a table while getting the files with readdir() but I am receiving an error:
"Warning: fileperms() [function.fileperms]: stat failed for 0.54322000 1352164273tunes.txt in C:\Users\Desktop\xampp\tybai5131displayBackups.php on line 24"
The file name is so long because I am naming it using microtime()
I get the same error for every function, not just fileperms()
Here is the PHP code I am using:
<table>
<tr><th>File Name</th><th>Owner ID</th><th>Permissions</th><th>File Size</th></tr>
<?php
//declare backup directory as a variable
$dirBackup = "backups/";
//check if backup directory exists
if(!is_dir($dirBackup)) {
//display error message if backup directory does not exist
print("You do not have a backup directory yet.");
} else {
//else open the directory for reading
$dirOpenedBackup = opendir($dirBackup);
while($backupFile = readdir($dirOpenedBackup)){
if($backupFile !== '.' && $backupFile !== '..'){
print("<tr><td><a href='backups/".$backupFile."'>" .$backupFile. "</a></td><td>".fileowner($backupFile)."</td><td>".fileperms($backupFile)."</td><td>".filesize($backupFile)."</td></tr>");
}
}
}//close !is_dir
?>
</table>
Any ideas of what I can do to get this to work properly?
Next to the physical existance of a file, there can be different other things that can prevent you from accessing the file under a specific user.
You need to verify if you can access the file and the directory the file is located with the user that is used by your PHP script to perform these calls (that depends on your server and PHP configuration). So first find out which is the username.

Categories