Table of Contents
回复内容:
Home Backend Development PHP Tutorial cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行

cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行

Jun 06, 2016 pm 08:29 PM
cmd php

首先感谢抽时间阅读!
我尝试通过浏览器登陆本地服务器运行我的PHP文件,无法正常运行。我安装的是Appserver安装包2.6.0版本(Appserver网站),包内包含的软件版本为:Apache 2.2.8PHP 6.0.0-dev。为了找出错误原因,我进行了如下步骤:
1、从命令行运行PHP文件,文件名为julia.php。运行成功。运行过程如下:
php文件代码为:

<code><?php exec(" julia 12.jl");
    echo "finished!";
?>

</code>
Copy after login
Copy after login

命令行运行:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
同时,php文件中还有一行指令"exec('julia 12.jl')"也执行成功了。这行代码意思是运用julia语言(一种新的编程语言)的REPL环境运行12.jl这个julia文件,12.jl运行成功的话,会在桌面输出一个txt文件。在此处txt文件也成功出现在桌面上了。
但是,当我使用浏览器,从服务器访问julia.php的时候,结果如下:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
虽然最后一行代码echo "finished!"顺利执行了,但是"exec('julia 12.jl')"并没有执行成功,因为桌面上没有生成txt文件。
2、我百度谷歌了很久看到一个貌似是查看错误的方法,我也不是很清楚是怎么回事,抱着试试看的态度操作了一下:
php文件代码如下:

<code><?php function my_exec($cmd, $input='') 
         {$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes); 
          fwrite($pipes[0], $input);fclose($pipes[0]); 
          $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); 
          $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); 
          $rtn=proc_close($proc); 
          return array('stdout'=>$stdout, 
                       'stderr'=>$stderr, 
                       'return'=>$rtn 
                      ); 
         }
$str = " julia 12.jl";  //此处为我要检测是否执行成功的指令" julia 12.jl"
var_export(my_exec($str)); 
    echo "finished!";
?>

</code>
Copy after login
Copy after login

命令行运行该php文件结果:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
浏览器访问结果:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
百度谷歌了其中的

"ERROR:key not found: "HOMEDRIVE"

并没有找到满意的答案。
请问我这是遇到什么问题了?有什么办法能让我顺利执行该文件中的exec(" julia 12.jl")指令么?
另外,我把其中的调用julia软件运行脚本的命令换成调用MATLAB软件的命令后,无论是在命令行还是在浏览器访问的情况下,均成功运行了MATLAB的m文件。我的php.ini文件也关闭了安全模式。
万分期待你的解答,谢谢!

回复内容:

首先感谢抽时间阅读!
我尝试通过浏览器登陆本地服务器运行我的PHP文件,无法正常运行。我安装的是Appserver安装包2.6.0版本(Appserver网站),包内包含的软件版本为:Apache 2.2.8PHP 6.0.0-dev。为了找出错误原因,我进行了如下步骤:
1、从命令行运行PHP文件,文件名为julia.php。运行成功。运行过程如下:
php文件代码为:

<code><?php exec(" julia 12.jl");
    echo "finished!";
?>

</code>
Copy after login
Copy after login

命令行运行:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
同时,php文件中还有一行指令"exec('julia 12.jl')"也执行成功了。这行代码意思是运用julia语言(一种新的编程语言)的REPL环境运行12.jl这个julia文件,12.jl运行成功的话,会在桌面输出一个txt文件。在此处txt文件也成功出现在桌面上了。
但是,当我使用浏览器,从服务器访问julia.php的时候,结果如下:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
虽然最后一行代码echo "finished!"顺利执行了,但是"exec('julia 12.jl')"并没有执行成功,因为桌面上没有生成txt文件。
2、我百度谷歌了很久看到一个貌似是查看错误的方法,我也不是很清楚是怎么回事,抱着试试看的态度操作了一下:
php文件代码如下:

<code><?php function my_exec($cmd, $input='') 
         {$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes); 
          fwrite($pipes[0], $input);fclose($pipes[0]); 
          $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); 
          $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); 
          $rtn=proc_close($proc); 
          return array('stdout'=>$stdout, 
                       'stderr'=>$stderr, 
                       'return'=>$rtn 
                      ); 
         }
$str = " julia 12.jl";  //此处为我要检测是否执行成功的指令" julia 12.jl"
var_export(my_exec($str)); 
    echo "finished!";
?>

</code>
Copy after login
Copy after login

命令行运行该php文件结果:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
浏览器访问结果:
cmd - php文件在命令行可以顺利运行,在浏览器上无法正常运行
百度谷歌了其中的

"ERROR:key not found: "HOMEDRIVE"

并没有找到满意的答案。
请问我这是遇到什么问题了?有什么办法能让我顺利执行该文件中的exec(" julia 12.jl")指令么?
另外,我把其中的调用julia软件运行脚本的命令换成调用MATLAB软件的命令后,无论是在命令行还是在浏览器访问的情况下,均成功运行了MATLAB的m文件。我的php.ini文件也关闭了安全模式。
万分期待你的解答,谢谢!

多次尝试后,问题得以解决
解决方法:增加了两个系统环境变量:HOMEDRIVEHOMEPATH
之前无法解决原因:
1、不清楚Key Not Found的含义,后来尝试才知道,指的是系统的环境变量,而非Apache或者PHP的环境变量,在此感谢xelz的点拨。
2、初次尝试增加HOMEDRIVE系统环境变量,测试后没反应,其实需要重启电脑,windows就这个蛋疼。
3、初次尝试增加HOMEDRIVE系统环境变量,重启电脑后,依然报错,一开始我没注意以为还是缺少HOMEDRIVE系统环境变量,其实这次的错误略有不同,显示的是缺少HOMEPATH系统环境变量。
4、意识到以上错误后,我再次添加HOMEPATH环境变量,重启电脑后,服务器成功运行了PHP文件,成功调用Julia软件运行了jl文件。
十分感谢大家之前的热心回答。

以下答案只是猜的,希望有帮助

julia需要HOMEDRIVE这个环境变量,命令行运行的时候是以你自己的身份运行的
但是运行apache服务器的用户没有设置这个环境变量,因此报错

不懂julia,如果你写文件用到了相对路径,可以尝试改成绝对路径
如果本来就是绝对路径,或者改成绝对路径也不行, 那么尝试在系统环境变量里设置一个HOMEDRIVE

Apache 过滤掉了一些环境变量大概?用 SetEnv 还是什么指令在配置文件里设置一下试试。

浏览器上本来就不能运行php

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles