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

来自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 .

我已将windows中开发的PHP文件复制到ubuntu,但是当我想在ubuntu上浏览这些文件时,他们不会 不执行任何事情。 为什么? p>

编辑 h2>

没有错误消息,我在浏览器中打开它们但似乎没有任何代码。 例如,如果文件的代码为&lt;?php echo“hello”; ?&gt; code>这个文件没有做任何事情。 p>

如果我创建一个新文件它工作正常,但是当我复制一个类似的文件时,它什么都不做 。 p> div>

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.

It maybe because you dont have the necessary infrastructure in place to run PHP files locally, i.e. in the case of WAMP: http://www.wampserver.com/phorum/read.php?2,44101,44101, you may need to install LAMP or similar. Its hard to determine wihtout additional detail..

Also you might need to convert your line endings from Windows to Unix format.

You can do this in Notepad++.

Edit>EOLConversion>Unix Format

Check the file permissions with ls -la. They should be readable by the Apache user (www-data in the standard case).

Try running chmod -R 755 /path/to/website and see if it works.