After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");
Related
What I'm trying to do:
Use PHP ftp_nlist to retrieve the contents of a directory on the FTP server
The problem:
For directories that contain a lot of files (the one I encountered the problem on has nearly 40 thousand files, and no subfolders), the ftp_nlist function is returning false. For directories that are not as large, the ftp_nlist function returns an array of filenames as expected.
What I've tried:
Enabling passive mode (it already was enabled, but I see it as a common suggestion)
Adding ftp_set_option($conn_id, FTP_USEPASVADDRESS, false); after my ftp_login
using ftp_chdir, although my folder names never have spaces anyways
echoing error_get_last() after ftp_nlist returns false. The error show seems unrelated, but is shown below.
My code:
In case it is useful, here is the function I have created. What it is supposed to do is...
take in $fm (filemaker, unrelated to this problem)
take in $FTPConnectionID (the ftp connection I established in the prior to the function call)
take in $FolderPath (the path of the folder on the FTP server for which I want to list files/subfolders recursively - ex: "SomeFolder/Testing")
take in $TextFile (I am writing the paths of every file found on the FTP server to a text file, which was created prior to calling the function)
function createAuditFile($fm, $FTPConnectionID, $FolderPath, $TextFile) {
echo "createAuditFile called for " . $FolderPath . "\n";
//Get the contents of the given path. Will include files and folders.
$FolderContents = ftp_nlist($FTPConnectionID, $FolderPath);
if($FolderContents == false) {
echo "Couldn't get " . $FolderPath . "\n";
echo "ERROR: " . print_r(error_get_last()) . "\n";
} else {
print_r($FolderContents);
}
//Loop through the array, call this function recursively if a folder is found.
if(is_array($FolderContents)) {
foreach($FolderContents as $Content) {
//Create a varaible for the folder path
$ContentPath = $FolderPath . "/" . $Content;
//Call the function recursively if a folder is found
if(pathinfo($Content, PATHINFO_EXTENSION) == "") {
createAuditFile($fm, $FTPConnectionID, $ContentPath, $TextFile);
echo "Recursive call for " . $ContentPath . "\n";
//If a file is found, add the file ftp path to our array
} else {
echo "Writing to file: " . $ContentPath . "\n";
fwrite($TextFile, $ContentPath . "\n");
}
}
}
}
I can provide other code if needed, but I think my question is less of a coding issue, and more of an understanding ftp_nlist issue. I've been stuck on this for hours, so any help is appreciated. And like I said, this function works just fine for most folder paths passed to it, the problem is when there are tens of thousands of files within the folder. Thank you!
This is the dilemma. I have a script that works perfectly for writing small video files into S3 bucket and local docker.
I need to update the script so it can handle larger files. In order to do that I am using the exec() method in which I run a php script to upload the file so it runs it in the background. This is the code I'm using:
$tempFile = $_FILES['form-file-input']['tmp_name'][$i];
$directory = $config['content_directory'];
echo (is_file($tempFile) ? 'Tempfile is a file' : 'Tempfile is not a file');
echo (is_readable($tempFile) ? ' and tempfile is readable.' : ' and tempfile is not readable.');
echo '<br>'.'This is the file name: '.$newFileName.'<br>';//already defined
chdir('/var/www/webApp/_apps/training_videos/');//required to find i-did-it.php - tested and doesn't affect saving functionality
exec("php i-did-it.php $tempFile $directory $newFileName 2>&1", $out);
var_dump($out);
//$resultado = move_uploaded_file($tempFile, $directory . '/_' . 'videos' . '/' . $newFileName);
//var_dump($resultado);
exit;
BELOW ARE THE CONTENTS OF i-did-it.php
Note that the code reviews before and after executing if the the param is a readable file and that the other params have a valid value. Also note that I set all errors, set a log file and try to catch the last error.
//Set all errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('log_errors',1);
ini_set('error_log','/var/www/siteContent/logs/log.txt');//verified that log file can be written to
//Get the params
$tempFile = $argv[1];
$directory = $argv[2];
$newFileName = $argv[3];
//Verify that it arrives as a readable file
echo (is_file($tempFile) ? 'Tempfile is a file' : 'Tempfile is not a file');
echo (is_readable($tempFile) ? ' and tempfile is readable.' : ' and tempfile is not readable.');
//Display the params
echo ' Temp file name: '.$tempFile;
echo ', the directory: '.$directory;
echo ' and new file name: '.$newFileName.' *** ';
//Move the file and dump results
$putResult = move_uploaded_file($tempFile, $directory . '/_' . 'videos' . '/' . $newFileName);//original
var_dump($putResult);
print_r(error_get_last());
So what is the problem?
move_uploaded_file commmand is not working when called via exec().
Note that if I replace this line
exec("php i-did-it.php $tempFile $directory $newFileName 2>&1", $out);
with this one
$resultado = move_uploaded_file($tempFile, $directory . '/_' . 'videos' . '/' . $newFileName);
it works!
Note that it is commented in the first snippet.
Those are the results I get
In order to troubleshoot I need to know why the move_uploaded_file command is not working since it only throws true or false. It's known it is false since it doesn't work. I would like to get more errors than that. The method recommended for the task I cannot use since my file is not set like this: $_FILES['file'] but as a variable (It has to be done that way since I'm passing it as a param) so I cannot check errors towards: $_FILES['file']['error']
Reference for the error obtaining approach
https://www.php.net/manual/en/features.file-upload.errors.php
Following the previous example I tried applying it to the var that holds the file to no avail:
if ($tempFile === UPLOAD_ERR_OK) {
//uploading successfully done
} else {
throw new UploadException($tempFile);
}
What do I need to continue?
At this point it would be helpful to know these 2 things:
How can I get the error/warning from move_uploaded_file that is coming back as false?
Any ideas why move_file_upload will work perfectly fine when called directly but fails when called via exec("php myfile.php $param1 $param2 $param3 2>&1", $output)
I'm developing a new portal for my business.. I have to start many batch files for different pc names.. so I try to run my batch with one parameter..
$path = $db->givePath($service);
$path = $path . " " . "PCNAME";
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
But if I run this command, nothing happens..
The old version worked:
$path = $db->givePath($service);
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
Can someone assist me here?
You are likely encountering issues when passing the PCNAME as an argument because it has characters that need to be escaped. You can read more on escapeshellarg the TL;DR is it escapes any control characters that would cause the execution to exit in an unexpected way. Using something like UNC path for PCNAME withouth using escapshellarg() would trick windows into thinking there was another argument being specified.
$path = $db->givePath($service);
$batchCmd = "C:\{$path} " . escapeshellarg('PCNAME');
if(exec("cmd /c {$batchCmd}")) {
echo 'Successfully sent';
} else {
echo 'Error';
}
You can to use cmd shell:
system("cmd /c C:" . $path_to_file);
i am trying to run a file sytem for dropbox ff4d ( from github) in background using php
the purpose if that user will get his dropbox files mount on the server and then i will give the path to a web based explorer (like eXtplorer) so user can manage his file
the script is working fine when using command shell
but when i using the exec function it working printing out the last line of the command shell
and that it . i can not get the folder mount
here the code in php :
$folder = $_POST['foldername'];
$oldumask = umask(0000);
$path = "/var/www/cloudsite/" . $folder;
if(mkdir($path, 0777, true)) {
echo $path . " success directory created ";
} else {
echo $path . "error directory not created ";
}
umask($oldumask);
#$command = '/usr/bin/python /var/www/cloudsite/ff4d/./ff4d.py '. $path .'c7ZYof8Bl0YAAAAAAAAAARThZwAUexbukmv3rMEprPJWFcoQSKGWtWHQBYM40OgC';
$result = exec($command);
if ($result){
echo " </br> execution success";
}else{
echo "</br> error not success";
}
echo $result;
and here what i get in the browser it seems like it working but just hang here nothing mount in the created directory :
var/www/cloudsite/chao success directory created
execution successStarting FUSE...
Within the latest release of my ff4d.py script I've added a "background" switch (-bg).
https://github.com/realriot/ff4d
BTW: Please remove your access key (and revoke it afterwards) because it holds your personal information and ALL your data...
I have a simple script that all I need it to do is create a directory with the name of the GET variable. When I run this script, it doesn't seem to create the directory. I would like this directory to be in the same directory as the PHP file.
$dir = $_GET['dir'];
umask(000);
mkdir($_SERVER['DOCUMENT_ROOT']."/".$dir."/",0777);
Put some error handling in there. Most of the time the error is self evident. The following snippet, lifted from PHP manual, shows you how.
$rs = #mkdir( $dirPath, 0777 );
if( $rs )
{
// success
}else{
// print error information
echo 'an error occurred. Attempting create folder';
echo '<br>dirPath: ' . $dirPath;
echo '<br>php_errormsg: ' . $php_errormsg;
}