简体   繁体   中英

Stuck at os.rename()

i am ubuntu user.. i have a command in php that exec a python file.. the python file is set to executable.. so, my php command is:-

 shell_exec("try.py");

the python file is located at desktop.. but the php file is located in www folder..

 /var/www/try.php

in the try.py, i have a code to rename a file on the desktop as follow:-

 print "enter"
 os.rename("a.txt", "b.txt")
 print "exit"

so, the try.py and a.txt are in desktop..

my problem is, when i execute the php file, it shows the "enter" only but not with the "exit".. so i guess it cannot execute the os.rename maybe because of the root privilege or anything that i dont know.. i have tried some solutions to disable password for sudo but still i didnt show the "exit".

but, if i execute the try.py directly by double click it on the desktop and execute it, the command can be done and the output shows:-

enter
exit

so, anyone knows how to execute it using php?

您可以使用rename功能直接在php重命名文件。

rename($oldname, $newname);

The problem is when you are doing os.rename("a.txt", "b.txt") it is looking for a.txt in the directory from where the application is running (so it is looking for a.txt in /var/www/ .

You should give both a.txt and b.txt the full path:

os.rename('/home/user/Desktop/a.txt', '/home/user/Desktop/b.txt')

You will also have to make sure that www-data (or whatever user is running Apache) can write to the Desktop directory; however from a security perspective this is a very bad idea - any script running on the server can read the contents of your desktop (and even delete the files).

finally, i got the solution..

to be able to rename the file in desktop using os.rename, we need to edit some of the configurations in sudoers..

it involved some steps in visudo and also the php script..

in visudo (using command "sudo visudo" in terminal:)-

#user privilege specification
root All=(ALL:ALL) ALL
www-data ALL=(ALL:ALL) ALL

#includedir /etc/sudoers.d

ubuntu ALL=(ALL) NOPASSWD: ALL //use your username.. mine is ubuntu
www-data ALL=(ALL) NOPASSWD: ALL //the place where i put the php file

in php file:-

<?php
$output = exec("sudo python /home/ubuntu/Desktop/[filename.py]");
?>

for python file is just the same..

os.rename("a.txt", "b.txt")

ok.. that is the solution.. now i can run the python script which it will change the name of a file in desktop from php..

Thank you for all the comments and suggestion for the solutions.. =))

You need something like:

my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))

That will equate to the directory try.py is actually in.

Then you can execute:

os.rename(os.path.join(my_dir, 'a.txt'), os.path.join(my_dir, 'b.txt'))

As mentioned above, this isn't necessarily a great idea from a security PoV though, since you have to grant write permissions to the php script.

I also usually put the my_dir code in an if sys.argv[0]: so that I can still debug from python REPL:

if sys.argv[0]:
    # running as executable
    my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
else:
    # imported into interpreter
    my_dir = '.'

That way I can still import the module into REPL to debug it, as long as I'm in the same directory as the file in question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM