Script to execute command to all files in folder

2019-03-04 09:36发布

I am new to Freebsd/nginx, so similar QAs did not help me ;( I was running a code:

$ext = pathinfo ($_FILES['rawexcel']['name'][$i], PATHINFO_EXTENSION);

//get extension of file, run different converters depending on extension
if ($ext == 'xlsx' ) {   exec("/usr/local/bin/cnvt   /var/tmp/xls/result.xlsx  /var/tmp/result.csv "); } 
else 
if ($ext == 'xls' ) {   exec("/usr/local/bin/xls2csv -x /var/tmp/xls/result.xls* -b WINDOWS-1251 -c /var/tmp/result.csv -a UTF-8"); } 
/var/tmp/xls/

when my file names were constant(result.xls which then converted to result.csv) & I was processing files one by one; cnvt and xls2csv are converters. Now I have ajax upload and want to perform exec command for all files in a folder, keeping original file name. I tried to run same code with result.xls replaced with *(star) but it did not work.

what I have:

folder tmp/xls with excel files

what I want:

convert all excel files inna folder to .csv keeping their original names. Will appreciate any help.

UPDATE: the best way to execute a command for all files in directory to create/edit a script to make that, operating on all files in directory much easier in bash itself. Good luck.

标签: php nginx exec
1条回答
Luminary・发光体
2楼-- · 2019-03-04 10:22

See how you extracted the extension...

$ext = pathinfo ($_FILES['rawexcel']['name'][$i], PATHINFO_EXTENSION);

You can check the pathinfo docs and you'll see how to get the filename without path or extension:

$filename = pathinfo($_FILES['rawexcel']['names'][$i], PATHINFO_FILENAME);

Then you can use that...

if ($ext == 'xlsx' ) {
    exec("/usr/local/bin/cnvt   /var/tmp/xls/$filename.xlsx /var/tmp/$filename.csv ");
} else if ($ext == 'xls' ) {
    exec("/usr/local/bin/xls2csv -x /var/tmp/xls/$filename.xls -b WINDOWS-1251 -c /var/tmp/$filename.csv -a UTF-8");
} 
查看更多
登录 后发表回答