I need to make a web site on which people can upload data files that after some treatment will be plotted using the jpgraph. The file is analyzed with a bash script called analfile.sh. The bash script is like this:
#!/bin/bash
file=$1
fecha=`date +%s`
mv $1 $fecha.dat
echo $fecha.dat
So, it gives back another file which name is like: 1321290921.dat. That is the file that I need to plot.
This my current php code:
$target_path = "/home/myhome";
$target_path = $target_path . basename( $_FILES['rdata']['name']);
$target_file = basename( $_FILES['rdata']['name']);
if(move_uploaded_file($_FILES['rdata']['tmp_name'], $target_path)) {
echo "The file ". $target_file. " has been uploaded";
chdir('/home/myhome');
$filetoplot=shell_exec('./analfile.sh'.' '.$target_file);
} else{
echo "There was an error uploading the file, please <a href=\"index.html\">try again!
</a>";
}
//$filetoplot="1321290921.dat"
echo "<br>The file to be opened is ". $filetoplot. "<br>";
if ($file_handle = fopen("$filetoplot", 'r')) {
while ( $line_of_text = fgets($file_handle) ) {
$parts = explode('.', $line_of_text);
echo $line_of_text ;
print $parts[0] . $parts[1]. $parts[2]. "<br>";
}
fclose($file_handle);
}
I have permissions to read and write on the target directory. I find strange that if I uncomment the line $filetoplot="1321290921.dat" then the script works perfectly. I guess I am doing something stupid since this is my first code in php but after some hours googling I was not able to find a solution.
Any help will be appreciated.
First thing I'm seeing is that you don't append trailing slash (/) to your home path, so that the path would be like /home/myhomefoo rather than /home/myhome/foo.
You should also move $target_file earlier, and reuse that within $target_path. There's no reason to do the same thing twice.
If that doesn't help, we'll see what goes next.
Related
I have router path with a '.' in it, my path is downloads/pass.pkpass
I have defined the path in web.php as the following
$router->get('downloads/pass.pkpass', 'PassServerController#downloadPass');
but somehow it's not working. if I remove the '.' it's working fine. what might be the problem here?
It's was working before recently I updated lumen after that it's not working.
The . is the string concatenation operator in PHP.
example
<?php
$string1 = "Test";
$string2 = "working!";
$string = $string1 . $string2;
echo $string;
?>
this will print "Test working!"
You can get your route by this may be
$router->get('downloads/pass.'.'pkpass', 'PassServerController#downloadPass');
But this is some kind of hack we can say.
You can use _ instead of . parameter that will work for sure.
I have an interesting problem that I can't seem to find the answer for. I am creating a simple app that will help my dev department auto launch docker containers with NginX and config files. My problem is, for some reason I can't get the bash script to store the name of a folder, while scanning the directory. Here is an extremely simple example of what I am talking about....
#!/bin/bash
getFolder() {
local __myResultFolder=$1
local folder
for d in */ ; do
$folder=$d
done
__myResultFolder=$folder
return $folder
}
getFolder FOLDER
echo "Using folder: $FOLDER"
I then save that simple script as folder_test.sh and put it in a folder where there is only one folder, change owner to me, and give it correct permissions. However, when I run the script I keep getting the error...
./folder_test.sh: 8 ./folder_test.sh: =test_folder/: not found
I have tried putting the $folder=$d part in different types of quotes, but nothing works. I have tried $folder="'"$d"'", $folder=`$d`, $folder="$d" but none of it works. Driving me insane, any help would be greatly appreciated. Thank you.
If you want to save your result into a named variable, what you're doing is called "indirect assignment"; it's covered in BashFAQ #6.
One way is the following:
#!/bin/bash
# ^^^^ not /bin/sh; bash is needed for printf -v
getFolder() {
local __myResultFolder=$1
local folder d
for d in */ ; do
folder=$d
done
printf -v "$__myResultFolder" %s "$folder"
}
getFolder folderName
echo "$folderName"
Other approaches include:
Using read:
IFS= read -r -d '' "$__myResultFolder" < <(printf '%s\0' "$folder")
Using eval (very, very carefully):
# note \$folder -- we're only trusting the destination variable name
# ...not trusting the content.
eval "$__myResultFolder=\$folder"
Using namevars (only if using new versions of bash):
getFolder() {
local -n __myResultFolder=$1
# ...your other logic here...
__myResultFolder=$folder
}
The culprit is the line
$folder=$d
which is treating the folder names to stored with a = sign before and tried to expand it in that name i.e. literally treats the name =test_folder/ as an executable to be run under shell but does not find a file of that name. Change it to
folder=$d
Also, bash functions' return value is only restricted to integer types and you cannot send a string to the calling function. If you wanted to send a non-zero return code to the calling function on $folder being empty you could add a line
if [ -z "$folder" ]; then return 1; else return 0; fi
(or) if you want to return a string value from the function, do not use return, just do echo of the name and use command-substitution with the function name, i.e.
getFolder() {
local __myResultFolder=$1
local folder
for d in */ ; do
folder=$d
done
__myResultFolder=$folder
echo "$folder"
}
folderName=$(getFolder FOLDER)
echo "$folderName"
use File::Copy;
#Variable with my directory I work on
$dir = "C:/projekty/perl/muzyka";
#Variables used to find all mp3 files
$dir_tmp = $dir."/*.mp3";
#files = glob( $dir_tmp );
#Variable with directory I want to create and put my files to
$new_dir = "C:/projekty/perl/muzyka/new_dir";
#Creating new directory
mkdir ( $new_dir ) or print "MKDIR PROBLEM";
Till this point everything is allright. Now I put the loop:
foreach( #pliki )
{
copy( $_, $new_dir) or print "COPY PROBLEM";
}
or:
foreach( #pliki )
{
move( $_, $new_dir) or print "MOVE PROBLEM";
}
And the problem is: Copy works perfectly fine, but Move doesn't want to do its job. It works sometimes depends on some modifications in code but never in a loop. Simple code with 1 line:
move($a, $b);
works perfectly. But if I use some conditions or loops it stops working even if arguments (directories) seem OK (I checked them with print function put in a loop). Why is it not working? Are there any circumstances that would cause errors?
copy and move are documented to set $! on error. It's also good to check whether the arguments are what you expect them to be. Pay particular attention to the presence of newlines and trailing spaces.
move($_, $new_dir)
or warn("Can't move \"$_\" to \"$new_dir\": $!\n");
I'm having a little problem here. I'm using the famous Aria2C file downloader and I have a list of links that I need to download. I know I can use:
aria2c -o myOutputFile.exe http://www.fooBar.com/fooBarVirus.exe
to download a single file and name it as myOutputFile.exe. The problem is that I must download some files and name it as 0.pdf, 1.pdf, 2.pdf, 3.pdf... because I will assemble all this pdf's later.
I'll try explain better. If I give the following links to aria2c:
http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
...
http://www.fooBar.com/myLastPdf.pdf
I must download these files and name it as a sequence of integers FOLLOWING THE LINKS INSERTION ORDER. With that I mean the filenames is related to the links order, but I don't need to download them in order.
Let's suppose I give Aria2C the previous links. The following output would be a valid output for my problem:
Time: 0
Link: http://www.fooBar.com/crazyPdf.pdf
Filename: 2.pdf
Time: 1
Link: http://www.fooBar.com/myPDF.pdf
Filename: 0.pdf
Time: 2
Link: http://www.fooBar.com/kindness.pdf
Filename: 1.pdf
...
Time: N
Link: http://www.fooBar.com/myLastPdf.pdf
Filename: N.pdf
I know this is a confusing question, so if you have any doubt please make a comment and I'll try answer as soon as possible. Thanks!
Ok, let's start with a pdfs.txt file holding the URLs for the PDFs:
http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
http://www.fooBar.com/myLastPdf.pdf
I throw in a little helper script to turn the URLs from the pdfs.txt file into the file format Aria2c accepts. The script uses the numerical index of an URL in the array as the new filename for the PDF.
<?php
$urls = __DIR__ . '/pdfs.txt';
$downloads = file($urls);
$targetFolder = __DIR__; // define your taget folder
$ariaDownloadList = '';
foreach($downloads as $idx => $url)
{
$pdfName = $idx . '.pdf'; // 0.pdf ... N.pdf
// add new download entry
$ariaDownloadList .= $url;
$ariaDownloadList .= ' dir=' . pathinfo($targetFolder, PATHINFO_DIRNAME) . PHP_EOL;
$ariaDownloadList .= ' out=' . $pdfName . PHP_EOL;
}
file_put_contents('ariaDownloadsFile.txt', $ariaDownloadList);
This outputs a file ariaDownloadsFile.txt with the following content:
http://www.fooBar.com/myPDF.pdf
dir=C:\pdfs
out=0.pdf
http://www.fooBar.com/kindness.pdf
dir=C:\pdfs
out=1.pdf
http://www.fooBar.com/crazyPdf.pdf
dir=C:\pdfs
out=2.pdf
http://www.fooBar.com/myLastPdf.pdf
dir=C:\pdfs
out=3.pdf
Then simply run the following command:
aria2c -i ariaDownloadsFile.txt
Trying to save a csv file on my server with codeigniter.
my syntax is:
$this->load->dbutil();
$query = $this->db->query("SELECT id,customeraccount,band FROM Customer_Bands");
$delimiter = ',';
$newline = "<br>";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
if ( ! write_file(base_url().'/application/authorizations/test.csv', $this->dbutil->csv_from_result($query, $delimiter, $newline)))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
This is rough right now but want to get functionality working first.
Two issues.
Firstly the echo $this->dbutil->csv_from_result($query, $delimiter, $newline); outputs to my view correctly but each field is inside "", how can I remove these?
Secondly, the file is not writing to my server.
base_url() is my codeigniter root folder then inside applications I have created a folder named authorizations so base_url()/application/authorizations
This currently fails and echos 'Unable to write the file'
Thanks in advance