Hello I have created a program in python that when a button is pushed it plays an audio file and it runs on a Raspberry Pi. I have created a web page when the user can go to the IP address of the Raspberry Pi and upload an MP3's then they can select which file they would like to play. There is some PHP code that looks over the directory to fill in the drop down list. How do I get what the user selects from the drop down list to import into a python program. This is the HTML/PHP main page.
<html>
<body>
<h1> Welcome to the Audio Setup </h1>
<hr />
<h3> Use to add mp3 file to server: </h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select MP3 to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<br />
<br />
<input type="submit" value="Upload MP3" name="submit">
</form>
<br />
<br />
<h3> Select file to play:
<hr />
<form name="sound" method="post" enctype="multipart/form-data" action="/home/pi/PushButton.py">
<label for="file">Select:</label>
<select name="sound">
<option value=""> Select File</option>
<?php
$dirPath = dir('/var/www/html/uploads/');
$imgArray = array();
while (($file = $dirPath->read()) !== false) {
if ((substr($file, -3)=="mp3") || (substr($file, -3)=="MP3")) {
$imgArray[] = trim($file);
}
}
$dirPath->close();
sort($imgArray);
$c = count($imgArray);
for($i=0; $i<$c; $i++) {
echo "<option value=\"" . $imgArray[$i] . "\">" .
$imgArray[$i] . "\n";
}
?>
</select>
</form>
</body>
</html>
This is the python program that it is going into.
import os
import sys
from time import sleep
import RPi.GPIO as GPIO
import mutagen
import cgi
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.IN)
form = cgi.sound()
if form.getvalue() != null:
musicFile = form.getvalue()
timer = musicFile.info.length + 1
while True:
if GPIO.input(23) == False:
os.system("omxplayer -o local /var/www/html/uploads/" + musicFile)
sleep(timer);
I am fairly new to programming and from what I have researched cgi is a way to transfer the selection over but maybe not the best way. If this isn't the correct way or if there is a better way please let me know.
Yes you can do it but you need to build small api at python side and after you select the file from list at php side you make requests with php to your api at python
How about a very simple solution, hopefully it does what you require.
In your php code, create a symlink from the selected file to a fixed name (http://php.net/manual/en/function.symlink.php), for example: /var/www/html/uploads/selected/1.mp3
In your python code, use the fixed name to retrieve the selected file.
Cheers
Related
I've searched all over and haven't been able to find what i'm looking for. This is my first dealing with PHP, and i'm comfortable with powershell. Basically I have a PHP page (demo.php) that will have a file select text box that will prompt a person to select a CSV file that they will have on their local system. When they click the process button, I am wanting the CSV file (or contents) to be sent to the PHP server for use by my ps1 script. When selecting the file, i believe it passes the file location to the ps1, but since it's on my local C:\, the import-csv command can't find it there on the server. (that's just my own thinking, i could totally be off)
Here are my basic examples that will accomplish the issue i'm trying to figure out:
demo.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Process Demo</title>
</head>
<body>
<?php
if(!isset($_POST["submit"]))
{
?>
<form name="ProcessForm" id="ProcessForm" action="demo.php" method="post" />
Select your CSV file: <input type="file" name="formFile" id="formfile" /><br /><br />
<input type="submit" name="submit" id="submit" value="Process" />
</form>
<?php
}
elseif((isset($_POST["submit"])) && (!empty($_POST["formFile"]))) {
$formfile = $_POST["formFile"];
$psScriptPath = "E:\\PS\\test.ps1";
$query = shell_exec("powershell -command $psScriptPath -formfile '$formfile' < NUL");
echo $query;
}
else
{
echo "Sorry, you did not complete all required fields. Please go back and try again.";
}
?>
</body>
</html>
test.ps1:
param([string]$formfile = "formfile");
import-CSV $formFile | forEach {
write-output "Item: $_.Item <br />"
write-output "Type: $_.Type <br />"
write-output "$("-" * 25) <br />"
}
CSV:
Item,Type
1001,Red
1002,Blue
1009,Yellow
Hope I've explained well enough with examples. Let me know if I missed anything. I want the end result to write-output the details of the CSV back to PHP from powershell so that I know I can mold my actual script around it. I think I can continue on with what I need once I figure that out.
Thank you for your time
I have a bash script that's tied into some python scripts that I would like to execute within a webpage and cat the file it creates on to the page. Currently I haven't been able to execute the script at all (tested the script multiple times to ensure it is in fact working), and I can't seem to find where my error is. The first set of code is from my 'dashboard.php' file which is in /var/www. I have changed the permissions for the scripts to 777 and made executable.
<html>
<body>
<form action="welcome.php" method="post">
IP/CIDR Input: <input type="text" name="cidriptext" />
Single IP: <input type="checkbox" name="singlech" value="single" />
CIDR Range: <input type="checkbox" name="cidrch" value="cidr" />
<input type="submit" value="Submit">
</form>
<form action="welcome.php" method="post">
List of IP's: <input type="text" name="listname" />
<input type="submit" value="Submit">
</form>
</body>
</html>
This second script is the page ('welcome.php') it reaches out to execute the script with the input from the form. At this point I don't expect the $listname_text to work properly, but I do expect to get the 'Incorrect Input' error when no boxes are checked, the 'testing' output at the end, and the files being created on the backend.
<?php
$ipcidr_text = $_POST['cidriptext'];
$listname_text = $_POST['listname'];
if !(empty($listname_text):
shell_exec('/var/www/getStarted -l $ipcidr_text');
$output0 = shell_exec('echo "List of IP's"');
echo "<pre>$output0</pre>";
elseif (isset($_POST['cidrch'])):
shell_exec('/var/www/getStarted -c $ipcidr_text');
$output1 = shell_exec('echo "CIDR Range"');
echo "<pre>$output1</pre>";
elseif (isset($_POST['singlech'])):
shell_exec('/var/www/getStarted -s $ipcidr_text');
$output2 = shell_exec('echo "Single IP"');
echo "<pre>$output2</pre>";
else:
$output = shell_exec('echo "Incorrect input"');
echo "<pre>$output</pre>";
endif;
$testing = shell_exec('echo "testing"');
echo "<pre>$testing</pre>";
?>
PHP is working, I am able to execute a basic script:
<?php
$output = shell_exec('echo "Incorrect"');
echo "<pre>$output</pre>";
?>
If you need anymore information, please let me know. Appreciate any help!
I've had issues with shell_exec() in the past and tend to lean on system() instead, also try " instead of ' to encapsulate the command. You may also need to change how you call your script:
From:
/var/www/getStarted
To (call the python interpreter specifically):
/usr/bin/python /var/www/getStarted
I am trying to upload selected image file into my ftp server on 1and1. I am not able to upload the file.
I have created folder called "uploadimages".
I have created a html form which has the following:
<form action="add.php" method="POST" enctype="multipart/form-data">
<div class="logo">
<label for="logoname" class="styled">Upload Logo (jpg / png):</label>
<div class="logofield">
<input type="file" id="logo" name="logo" size="30"/>
</div>
</div>
<input type="submit" value="Upload" name="submit" id="submit"/>
</form>
I have also create a php file which has the following:
<?php
$imagepic = $_FILES["logo"]["name"];
echo $imagepic;
$tempimgloc = $_FILES["logo"]["tmp_name"];
echo $tempimgloc;
$errorimg = $_FILES["logo"]["error"];
echo $errorimg;
if($errorimg > 0)
{
echo "<strong> <font size='18'>There was a problem uploading your Logo. Please try again!</font></strong>";
echo "<BR>";
}
else
{
move_uploaded_file($tempimgloc, "uploadimages/".$imagepic);
}
?>
ECHO printed results are:
1. Filename = testimage.png
2. Temp directory = /tmp/phpHvewUP
3. Error = 0
But they are not getting uploaded..
Where could I go wrong here?
Let me know!
You need to give access to your folder. Try this, chmod 777 uploadimages.
chmod 777 uploadimages
WRITING PERMISSION was the issue. Thanks Shapeshifter!
I am trying to pass an uploaded file as an argument to a python script.
form.php
<html>
<body>
<form method="post" enctype="multipart/form-data" action = "formdata.php">
<label for="file">Upload File:</label>
<input type="file" name="file" id="file" />
<label for="user_email">Email:</label>
<input type="text" name="user_email" id="user_email" />
<input type="submit" id="submit" value="Submit" />
</form>
</body>
</html>
fomrdata.php
<?php
$email = $_POST['user_email'];
$uploaded_file = //don't know what to do here
$output = shell_exec("/usr/bin/python /var/www/Create.py $uploaded_file $email);
echo "<pre>$output</pre>";
?>
Create.py
# def moveMetadataFile():
# import shutil
# shutil.copyfile('/var/www/**uploaded_file**', '/var/www/temp/**uploaded_file**')
# some more code
upload = sys.argv[1]
email = sys.argv[2]
This works fine and I get the email and everything from the user and the code works if I specify a file name myself but now I want the python script to use the file that the user uploads. And I have absolutely no idea where to start?
If it's easier to answer this ignoring the commented out part then that'll work too.
You can get the filename using
$uploaded_file = $_FILES['file']['tmp_name'];
From the PHP documentation for tmp_name:
The temporary filename of the file in which the uploaded file was stored on the server.
See the PHP documentation | POST method uploads for more information. You would probably also want to use some of the other fields to make sure an error did not occur prior to calling the Python script.
Does that help?
I'd like if someone could give me some advice on creating the php file, i know to php language.but where to write it.i have followed some tutorial to run php file in netbeans but its pathethic to download xamp server,apache http server.can u give me the direct of how make configuration and all.i have window7 ultimate and will file i hav to download i don't know. i have netbeans all bundle feature and wamp server.how should i write my php program successfully.plz help me to resolve this.
i m editing the question becoz in comment it is than wordspace given there thatwhy not accepting
thanks its working..
can u tell me why this code doesnot work properly
my php code(or content is this)
<html>
<head>
<title>Binary Search</title>
<style type="text/css">
h1 {color: blue}
</style>
</head>
<body>
<h1 align="center">Computer guess number by using binary search</h1>
<form method="GET">
<?
if (empty($flag_num))
{
$flag_num = -1;
}
if ($flag_num==-1)
{
if (empty($max_num)) $max_num = -1;
if (empty($min_num)) $min_num = -1;
$flag_num = 1;
print <<<Here
<input type="hidden" name="flag_num" value="$flag_num">
<input type="hidden" name="max_num" value="$max_num">
<input type="hidden" name="min_num" value="$min_num">
Input your hidden number: <input type="text" name="hid_num" value="$hid_num"> (1-99)
<br>
<input type="submit" value="Now let's computer guess">
Here;
}
else
{
if ($max_num==-1 && $min_num==-1)
{
$max_num = 100;
$min_num = 0;
$result_num = $hid_num;
}
else
{
if ($comparision == "bigger")
{
$min_num = $guess_num;
}
else if ($comparision == "smaller")
{
$max_num = $guess_num;
}
}
$guess_num = ($max_num + $min_num)/2;
setType($guess_num,"integer");
print "Computer guess <h3> $guess_num </h3>";
if ($guess_num == $result_num)
{
$flag_num = -1;
}
if ($flag_num == -1)
{
print <<<Here
<input type="hidden" name="flag_num" value="$flag_num">
<h1> Congratulation, Computer win </h1>
<input type="submit" value="Next>>>" >
Here;
}
else
{
print <<<Here
<input type="hidden" name="flag_num" value="$flag_num">
<input type="hidden" name="max_num" value="$max_num">
<input type="hidden" name="min_num" value="$min_num">
<input type="hidden" name="guess_num" value="$guess_num">
<input type="hidden" name="result_num" value="$result_num">
<br>
Your intruction: <input type="radio" name="comparision" value="bigger"> Bigger
<input type="radio" name="comparision" value="smaller"> Smaller
<br>
<input type="submit" value="Submit">
Here;
}
}
?>
</form>
</body>
</html>
it doesnot giving the output properly as required
wherever wamp is installed go to the folder "www", put the the php file there
then go to localhost:8080/yourfile.php
that's all there is to it
You could write PHP code in almost anything, and the file will be created as long as you add the .php extension to the new file. I wrote a few simple programs using Notepad and Notepad++ before going onto Eclipse PDT.
To run your file, one out of many ways, you can start up your WAMPserver, browse to http://localhost and drag and drop your file into the browser.
You could also put the .php file inside the www folder (IIRC thats the name of the folder, my memory is a bit hazy) and then browse to http://localhost/name_of_file.php. In either case, if successful, your PHP code should execute on the page.