dongsui5464 2011-10-06 03:42
浏览 76
已采纳

来自Windows的PHP文件在Ubuntu中不起作用

I have copied PHP files developed in windows to ubuntu, but when I want to browse these files on ubuntu, they don't excute anything. why ?

edit

No error message, I open them in the browser but it seems that if they don't have any code. for instance, if a file has the code <?php echo "hello"; ?> this file don't do anything.

If I create a new file it works fine, but when I copy a similar file, it doesn't do anything .

  • 写回答

4条回答 默认 最新

  • dsarttv037029 2011-10-06 04:15
    关注

    In order for a php file to run automatically under Unix/Linux, it needs two things:

    1. the file must be executable
    2. the file must have a valid 'hashbang' line as the first line in the file.

    Files that you transfer from windows to Linux probably won't have the correct permissions, and they almost certainly won't have the correct hashbang line. Use 'ls -l' and chmod to view and change the permissions (I'll leave this as an exercise to the reader).

    The hashbang line for php will looks like this on my Ubuntu box:

    #!/usr/bin/php
    

    So your example would actually look like this:

    #!/usr/bin/php
    <?php echo "hello"; ?>
    

    The actual path can be found using

    command -v php
    

    There is a subtle issue about the hashbang line that you do have to take in to account when transferring files from windows to unix, and it is in fact what skyline mentioned: Windows uses the ' ' line feed combination, unix uses only ' '. This means that the hashbang line from a file that was edited on a windows box will actually look like this:

    #!/usr/bin/php
    

    You won't see the '' character (it's a carriage return after all), but the operating system will try to execute 'php' rather than 'php'... so yes, you do have to use dos2unix or frdos to remove the carriage returns from the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部