I'm trying to switch index files using PHP and have gone for a method where I rename respective index files, like this:
<?php
rename("/index.html", "/REALindex.html");
rename("/index2.html","index.html");
?>
The PHP script is located in a folder child of the index files, but when I run this php nothing happens to the files, so I am both wondering how I'm supposed to debug and also why it's not working..?
Thanks a lot.
If your PHP script has permissions to edit the local directories you're running in (based upon the comment you made), your HTML files are one level up from the php, so try:
rename("../index.html", "../REALindex.html");
rename("../index2.html","../index.html");
Note the forward slash at the beginning of the paths. Could it be that this is accidentally and you're unwillingly trying to move files from/to the filesystem root, where they just aren't?
SOULTION:
chmod -R 777 /var/www/html
+
rename("../index.html", "../REALindex.html");
rename("../index2.html","../index.html");
Related
I have a domain root which is a folder within the server root. How can I mkdir inside a different folder within the domain root?
ROOT > DOMAIN ROOT FOLDER > PHP MKDIR
ROOT > IMAGE FOLDER > MKDIR HERE
I need the php in the 'domain root folder' to create a directory within the 'image folder'. Any help?
You can usually do something like this:
mkdir('../images/newfolder');
You may have to add more ../../ on to the front of this, depending on where you are running the script from exactly. Each ../ moves you up another level, so it seems like just one would work here given your example, but you have to be sure you are where you think you are.
You could also use absolute path in your php-script.
mkdir('/root/images/new_folder');
Make sure that your web-server has permissions for writing to specific folder.
I usally work from the directory my current script is located in and maneuver my way to the desired location from there. Works fine as long as you don't move you script. In addition, it will be easier to keep track of where everything is relative to the script.
You can get the current path with:
$path = dirname(__FILE__);
Then appending ../ to go deeper etc.
I'm using OpenSuse 12.2 now. To learn and test PHP code I use public_html directory in my home folder: /home/wojtek/public_html/ I access project files in the following way:
http://localhost/~wojtek/projects/foo/bar.php
But I have not clue how to make move_uploaded_file to work. The server root is /srv/www. I set upload_tmp_dir to /srv/www/tmp (I have really no idea where it should go).
When I set destination of move_upladed_file to /srv/www/images, files landed there. But I'd like to make use of public_html folder as I'm keeping all my files there.
For example having:
public_html/projects/foo/
public_html/projects/foo/bar.php
public_html/projects/foo/images/baz.jpg
Can I somehow use the relative destination ('images/') of move_uploaded_file in the bar.php? So that I can access images relatively in IMG tag like from bar.php?
Sorry if it sounds a bit chaotic but I'm new to PHP.
No you wont be able to access the /srv/www/images folder from the /home/wojtek/public_html/projects/foo/bar.php web page.
Id suggest either
1) setting the move_uploaded_file to /home/wojtek/public_html/projects/foo/images
or
2) using a symbolic link to access the images folder from your public_html folder:
cd /home/wojtek/public_html/projects/foo
ln -s /srv/www/images/
and then it will appear that the images are stored in /home/wojtek/public_html/projects/foo/images, but they will actually still be here /srv/www/images
Its possible that you may run into trouble with permissions using this method however
I have a folder that contains many different files:
folder1:
somthing.php
somthingelse.php
blah.php
ect ect....
Now, I don't want the files in that folder to access (include) files outside the folder. So it's okay to include('blah.php') but not okay to include('../blah.php')..
Any ideas on how I'd go about doing this?
This is only possible on the CLI and in a Unix environment. You would have to use chroot to hide other folders.
http://php.net/manual/en/function.chroot.php
I want to run python script 'test.py' from my cgi-bin directory on my webserver. the cgi-bin directory is at 'www/cgi-bin/'. The python scrip is in that directory. The php code I'm executing is at 'www/html/website/index.php'.
what is the correct path that goes here ------> exec('path');
TYVM
edit:
My python script has been chmod +x'd and is executable (I have tested)
Assuming the PHP script is in the same directory as the "www" directory.
<?
exec('./www/cgi-bin/test.py');
?>
or without chmoding, you can run it through Python
<?
exec('python ./www/cgi-bin/test.py');
?>
Your PHP code is executing in the www/html/website/ directory then. You need to go up two directories (arriving in the www/ directory), then go down to the cgi-bin/ subdirectory. So this should work:
exec('../../cgi-bin/test.py');
Note that this is relying on the current work directory being the PHP script directory. This might not always be the case, particularly if something in the PHP script changes the current work directory explicitly or simply if this PHP script is included from a different script. So it is better to use absolute paths (e.g. put the absolute path of the base directory into a config file).
i was using this basic script:
$folderPath = "../path/to/$folder/";
mkdir("$folderPath");
i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser
The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos
I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all
All the older folders were being created with: -w- rwx r-x r-x
and I can't get this recreated
I've tried adding a chmod line into my script:
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
but I can't recreate the same permissions, I'm trying to understand how chmod works, but I can't figure out how to get this very basic function working properly again
Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-
In the folder "root_folder/sales/inventory/$folder/", where "$folder" is as mentioned in your code.
If not found, try in the folder "root_folder/sales/inventory/".
If not found, try in the folder "root_folder/sales/".
If not found, try in the folder "root_folder/".
When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder", where the code will be "Options Indexes".
Also in the PHP page, the logic must be like this:-
<?php
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
// Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable.
chmod($targetFile, 0755);
?>
This will help you when you will be unlinking / deleting the uploaded files from the "$folder" folder.
Hope it helps.
If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.
Imagine that $folder = '/store1/ally23/shelf42'; so your final directory structure is
../sales/inventory/store1/ally23/shelf42, and you want 0777 permisions.
You do:
mkdir($folderPath, 0777, true) || chmod($folderPath, 0777);
Only the final directory shelf42 is chmoded to 0777. The intermediary directories are created with default permissions (in my case 0744).
There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.
If you're in a shared environment, you may also want to chown after upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.
Simply umask means the default permissions for new files/directories:
<?php
umask(022);
?>
This sets the default permissions for user, groups, and others respectively:
0 - read, write and execute
1 - read and write
2 - read and execute
3 - read only
4 - write and execute
5 - write only
6 - execute only
7 - no permissions