I get the following error from PHP when running from the server
PHP Warning: rename(var/www/vhosts/example.com/httpdocs/dir1/papers/1632813138.pdf,var/www/vhosts/example.com/httpdocs/dir1/papers/00000006.pdf): No such file or directory in /var/www/vhosts/example.com/httpdocs/dir1/processes/generate_abstract_pages.php on line 98
If I then run
less var/www/vhosts/example.com/httpdocs/dir1/papers/1632813138.pdf on the command line it finds the file. I'm copy/pasting the address so it's not accidentally getting changed.
I also tried giving rename a relative path of ../papers/1632813138.pdf but that gave the same error.
The relative path works when run in browser, https://example.com/dir1/processes/generate_abstract_pages.php
<?php
$abstract["pdf_url"] = "1632813138.pdf";
$abstractId = 7;
$pdfFilename = str_pad($abstractId, 8, "0", STR_PAD_LEFT).".pdf";
$fileRenameSuccess = rename("../papers/$abstract[pdf_url]","../papers/$pdfFilename");
echo ($fileRenameSuccess ? "y":"n");
Running PHP 7.1.23
Running CentOS Linux 7.6.1810 (Core)
You can use getcwd() to get the current working directory of your script to make sure that it's running from where you expect for your relative path to work.
You need a starting "/" for the absolute path to work.
Related
Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = #file_get_contents($url);
$hit_count++;
#file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
#file_get_contents is working fine and the header location change to the downloaded file also works, but either the hit_count increase or #file_put_contents isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."
You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.
try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>
I just switched a database over to a Plesk server on GoDaddy. Now, my connection script include fails with this error:
PHP Warning: include(/rev/scripts/connection.php): failed to
open stream: No such file or directory in
G:\PleskVhosts\mysite.com\httpdocs\rev\db_administration\complete_backup.php
on line 5 PHP Warning: include(): Failed opening
'/rev/scripts/connection.php' for inclusion
(include_path='.;.\includes;.\pear')
I'm not sure why it is prepending 'G:\PleskVhosts\' to the url or even if this is what is crashing it. Can anyone tell me what I am doing wrong? This code has worked for a long time, so I don't know why it needs to change with the new server.
What is strange to me is that if I run this from a browser (including the edit below), it works. But I need this to run from scheduled tasks, and there is where I get the error.
Here is the code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$connection = $path . '/rev/scripts/connection.php';
include ($connection);
echo "success";
?>
EDIT: Per suggestion below, I changed my code to this, which allows it run from the browser. But I still can't get it to run from scheduled tasks:
$path = !empty($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'];
Looking at the include path in your error message ... include(/rev/scripts/connection.php) ..., it looks like $path is an empty string. A bit of googling shows that GoDaddy doesn't always translate $_SERVER['DOCUMENT_ROOT']; correctly for some of its add-on domains and subdomains. Try replace $_SERVER['DOCUMENT_ROOT'] with $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];.
So essentially adding,
$path = !empty($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] : $_SERVER['DOCUMENT_ROOT'];
or
$path = dirname(__FILE__); // this might or might not work.
UPDATE
$_SERVER won't work when attempting to run this from a scheduled task (presumably, via cron). Try this:
define('DOCUMENT_ROOT', substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1));
It's supposed to get you the same data as $_SERVER['DOCUMENT_ROOT'] for cron jobs.
All I want to do is fopen() a text file in my PHP script. It's the most important part of the module that I'm building. Unfortunately, while testing the script, I found that the script cannot see the text file that I'd like to open. I have the script in the same directory as the text file, but I keep getting met with the same error. I'm posting the script code snippet and the command line prompt/response below.
Code
$membersfile = fopen('./members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
Command Line
507 elersong:~$ php /Users/elersong/Desktop/SS2014/register_member.php
Warning: fopen(./members.txt): failed to open stream: No such file
or directory in
/Users/elersong/Desktop/SS2014/register_member.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in
/Users/elersong/Desktop/SS2014/register_member.php on line 13
I have no clue why PHP can't see the file since it most certainly exists, and it's in the same directory as the script. Please help me make sense of this.
Some PHP setups have problems with relative parts, so try this instead:
fopen(__DIR__ . '/members.txt', 'r');
__DIR__ contains the path to the folder where the currently executing PHP file is.
Your are currently doing:
$membersfile = fopen(__DIR__ .'/members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
But $membersfile contains just the handle, a number so that PHP nows which opened file you are meaning. If you print that you will just see the handle number. You need to tell pHP to read the file using:
$file = __DIR__ .'/members.txt';
$membersfile = fopen($file, 'r');
echo fread($membersfile, filesize($file))
fclose($membersfile);
or use the php shorthand function:
echo file_get_contents( __DIR__ .'/members.txt');
After trying a number of the suggestions posted here, I was finally able to figure out that the PHP script isn't using a relative path to the text file based on the directory that holds the PHP script.
User #andrew suggested that I add the following line to my script in order to learn which directory the script uses as its starting point.
echo '<pre>';var_dump(glob('*'));
From the output of this line, I was able to see that the script uses the user's root folder as the starting point in fopen() on my OSX machine. I simply needed to preface the filename with the remainder of the file path, and PHP could read it. The amended script is as follows:
$membersfile = fopen('Desktop/SS2014/members.txt', 'r');
Thanks so much, #andrew and everyone else who submitted suggestions!
I am struggeling with a quite strange problem within ZendStudio. Including or requiring files inside a phar archive using the phar extensions phar:// stream wrapper will not work from within ZendStudio.
Here is what I have done.
Given the following file structure:
phar/Test.php
pharbuilder.php
usephar.php
With the following content:
phar/Test.php:
class Test {}
pharbuilder.php:
$phar = new Phar(__DIR__ . '/test.phar');
$phar->buildFromDirectory(__DIR__ . '/phar/');
usephar.php:
var_dump(file_exists('phar://' . __DIR__ . '/test.phar/Test.php'));
include('phar://' . __DIR__ . '/test.phar/Test.php');
$test = new Test();
var_dump($test);
Now, if you first run the pharbuilder.php in ZendStudio (ZS 8: right click on file, run as PHP Script) the phar file (test.phar) will be successfully created (before running it, you have to add the ini option phar.readonly=0 to the corresponding php.ini). However, running the usephar.php inside ZS, will result in an error.
bool(true)
Warning: include(): Failed opening 'phar:///somepath/test.phar/Test.php' for inclusion
So obviously, the file can be found, but it cannot be included. I can also access the content of the Test.php within the file using file_get_contents('phar://' . __DIR__ . '/test.phar/Test.php')
Also, I noticed that this will not happen, if I modify the run configuration for this script and disable the checkbox "Display debug information when running". So I assume it has something to do with the ZendDebugger, but I'm not quite sure.
When I run the same scripts from a console, everything works just fine.
I tried this with ZendStudio 8 and 10 and with all in ZS provided php versions (5.4.11, 5.3.21, 5.3.3 all in CGI and CLI).
Since I could not find anybody else with the same problem, I assume that I am doing something wrong and am open to any suggestions. Maybe some secret php.ini directive or something like this.
This issue is now fixed in Zend Studio 10.6.2.
Please mind that as mentioned above you need to modify php.ini file (located in -<studio_home>/plugins/com.zend.php.debug.debugger.win32.x86_\resources\php)
and add
phar.readonly=0
I've been trying to debug this error for over three hours now, changing filenames, trying to use GeoIP Lite instead of GeoCity (the latter has a 27mb file to be included, so did this thinking fopen() had a max), etc.
Here's my structure file structure: index.php -> include("configuration/config.php") - config.php -> include("inc/geo_text.php") -> geo_text.php
The contents of geo_text.php is:
$ip = $_SERVER['REMOTE_ADDR'];
include("GeoIP/geoip.inc");
$gi = geoip_open("GeoIP/GeoIP.dat",GEOIP_STANDARD);
$count_name = geoip_country_name_by_addr($gi, $ip);
geoip_close($gi);
echo($count_name);
Now, if I access geo_text.php no errors are given, and just to make sure I placed echo($count_name) in geo_text.php and it returned, as it should, my country.
However, when I run config.php it returns the error:
Warning: fopen(GeoIP/GeoIP.dat) [function.fopen]: failed to open stream: No such file or directory in /nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/geoip.inc on line 399
Can not open GeoIP/GeoIP.dat
Has anyone got any ideas why this could be?
SSH into your server and run the following command (assuming it's a Linux server):
cd /nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/
ls -lah
Then paste the output here for us to see. My guess is that that path doesn't exist.
That's very strange. As a test, try moving both geo files into the same directory as your code files and then alter the paths in your code accordingly.
It's checking for the files in domain.com, but you just cd'd into themeplated.com, that's the problem. Your code needs to point to the themeplated.com directory.
/nfs/c09/h02/mnt/177978/domains/domain.com/html/labs/final/configuration/inc/GeoIP/
/nfs/c09/h02/mnt/127878/domains/themeplated.com/html/labs/final/configuration/inc/GeoIP/
It's a path issue.
geoip_open("/absolute/path/to/GeoIP/GeoIP.dat",GEOIP_STANDARD);
should work.