drupal file_check_path return false - php

I am working on files in drupal. i use local wamp server with drupal6.
my drupal path is localhost/drupal6 .i have a file in this path <drupal root >\files\images\111.jpg .
$image='/drupal6/files/images/111.jpg';
I want copy(or move) this file to another sub folder in this path but give me error in php copy($image,'anotherdestination') or drupal_copy($image,'anotherdestination',FILE_EXISTS_RENAME).
when i use var_dump(file_check_path($image)); it return me false .
the files and iamges folder have all permission for any user in windows.
where is the problem?
notice: i wrote all of this code in a function in a custom module with path sites/all/module/mymodule/mymodule.module

There's a difference between how files are accessed over HTTP and via the file system. Over web /drupal6/files/images/111.jpg is perfectly fine, since it'll be evaluated as http://example.com/drupal6/files/images/111.jpg. But I bet you do not have a drupal6 directory in the root of your file system.
You probably want something like this.
copy('c:\drupal6\files\images\111.jpg', 'c:\drupal6\files\other_images\111.jpg');
I'm not entirely sure about paths in Windows but I think that should be correct.

Related

Saving to "/" on Apache server w/CodeIgniter

This seems like a basic question, but it's stumping me. I have CodeIgniter installed, and I've got a model that manipulates and saves XMLs. My problem is when trying to save it to "/" I get a "Permission denied" PHP error. I need to save them to a separate directory relatively, but I'm not sure where exactly "/" is located on the server. Is it in the "/www" of Apache, or root of the whole server? Once I know this I should be able to navigate to the correct directory
/ is the root directory. The starting point of your directory structure. This is where the Linux system begins. Every other file and directory on your system is under the root directory. Usually the root directory contains only sub-directories, so it's a bad idea to store single files directly under root.
Try specifying the complete path in your application.
Example: /home/user/public_html/yourApplicationFolder/
Or specify a relative path:
Example: ../somePath/.
This article could be helpful.
To get the filesystem path to the document root just use $_SERVER["DOCUMENT_ROOT"];

Reverting to a different file when PHP fails to find a specific file (include function)

Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

Refer to website root in PHP (Windows)

I want to refer to my website root, or more exactly, to the directory above my script's one.
Let's say my website is example.com/test. I made a installation site which writes a config file. But it shouldn't write it to example.com/test/install/config.php, but to example.com/test/config.php. And the biggest pain in the ** is that I run on Windows (my development PC).
How do I do it?
If you want to get the web-site document root, you can use:
$_SERVER['DOCUMENT_ROOT']
That should work regardless of the operating system and gives you a path on the local file system (so no www.etcetc.).
If you want to get the fully-qualified path of your site root, from that file it is:
$root = realpath(dirname(__FILE__) . '/../../..');
The double-dots work their way up the directory structure, then realpath() is used to turn it into a proper path. So if you want to traverse one less folder up, use two sets of dots rather than three.

How to access a file with PHP in a virtual directory on IIS 7

Here is my problem: I have a website running on IIS 7 + PHP 5.3. There is a virtual directory in the website hierarchy called "vd" which contains flash files.
myApplication
- test.php
- vd
- animation1.swf
- animation2.swf
- ...
So, it's easy to reach the swf files with a browser, you just have to put the directory name in the url: http://www.mySite.com/vd/animation1.swf
However, I would like to use the getimagesize() function on my animations in a PHP script. But in this case, php can't find the file:
<?php
// test.php
var_dump(getimagesize('vd/animation1.swf')); // false
?>
It seems to make sense because anyway '/' aren't even the right directory separator for windows. But I just can't figure out how to make getimagesize works through a virtual directory, I tried a lot of stuff without success (using the DIRECTORY_SEPARATOR constant, using realpath() function ...).
Of course, I could use the real path in my script but it would be easier for me to be able to do that throught the virtual path.
Any help would be greaty appreciated,
Thanks!
PHP is not going through the web server but rather the OS to get the file. So, no web server virtual-to-real translations will take place. You will need to use the real path of the file. You could make a web request on the file and deduce the size in that manner but that is very inefficient. It makes much more sense to just do it right through the OS.
Simply create a variable/define/class constant or whatever you prefer to store the real path of the pertinent directory. Then just append animation1.swf to that in your code.

Categories