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.
Related
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
pls i need help am trying to use the mkdir for user profile pic upload and its not working ... but when i use it normally i.e ordinarilly for example when i created a new php page and used " mkdir("newdir") "it worked
<?php
if (isset($_FILES['profilepic'])) {
if (((#$_FILES["profilepic"]["type"]=="image/jpeg") ||
(#$_FILES["profilepic"]["type"]=="image/png") || (#$_FILES["profilepic"]
["type"]=="image/gif"))&&(#$_FILES["profilepic"]["size"] < 1048576)) {
//1 Megabyte
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("userdata/profile_pics/$rand_dir_name");
}
else{
}
}
?>
<p>UPLOAD YOUR PROFILE PHOTO:</p> <br />
<form action="" method="POST" enctype="multipart/formdata">
<img src="./img/default_pic.png" width="70" />
<input type="file" name="profilepic" /><br /><br />
<input type="submit" name="uploadpic" value="Upload Photo"><br />
</form>
its not because of php, the php code is written well the problem is in the html where the html form enctype is written wrong
this
<form action="" method="POST" enctype="multipart/formdata">
should be
<form action="" method="POST" enctype="multipart/form-data">
Edit some code for your code with this:
Notice that it is best practise to use the variable outside of the string. Also you place the code in an if statement to check it executed correctly.
edit
You also need to start the mkdir call with a / or even better start it with $_SERVER['document_root'] so you know you always get an absolute path to the directory you want to generate.
if(mkdir("/userdata/profile_pics/".$rand_dir_name)){
print "this worked!";
}
else {
print "This failed";
}
Also you need to check that the permissions for the parent directory are correct, that the PHP usergroup is allowed to create directories in the /profile_pics/ folder.
You can also check that PHP is in the correct working directory, you can check this with print gwtcwd();
May be you should try like this (variable name along with a single-inverted comma)
mkdir("userdata/profile_pics/'$rand_dir_name'");
I hope it will work.
First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.
I am using the following script to edit text based files on my server (TXT,HTML,PHP,etc..)
<?php
$filename = "test.php";
function make_content_file($filename,$content,$opentype="w")
{
$fp_file = fopen($filename, $opentype);
fputs($fp_file, $content);
fclose($fp_file);
}
if($_POST)
{
$newcontents=$_POST[newcontents];
make_content_file($filename,$newcontents);
}
$filecontents = file_get_contents($filename);
?>
<?php
if($_POST)
{
echo '<p><span style="font-weight: 700; background-color: #CCFFCC">You have successfully posted to your txt file!</span></p>
Download';
}
?>
<form method="post">
<textarea name="newcontents" cols="70" rows="25"><?=$filecontents?></textarea>
<br>
<input type="submit" value="Save">
</form>
The script works fine with most of PHP files Example:
But if a file includes some form with textarea the code just get messed up
Example:
Another Example: http://i.imgur.com/P9O34Y8.png
Would like to know why this happen and how to fix it, Thanks
The first comment sums it all
Fun fact: I have a similar tool myself, with the ability to edit files from a bookmark, an emptying files feature, and sha1 password protection. (insecure crc32 is still used in the original code)
Hope it's actually useful
if (sha1($_POST['pass']) != $pass) {
echo "<style>input{padding:0.1em; font-size:1.1em}body{background:$bg; color:$fg; font-family:corbel;font-size:1.4vw;padding:0.4em}a{color:$fg}</style><body><h2>$nick<mark>:)</mark></h2>";
echo '
<form action="writer.php" method="post">
Code: <input type="password" name="pass" autofocus>
<input type="hidden" name="f" value="'.$_GET['f'].'"><br><br>';
if ($_POST['pass'] != ""){
echo 'Incorrect code.';
}
}
https://pastebin.com/krMrA783
To empty a file, type "empty".
To set the password before using, go through the first 5 lines of code, and modify $pass.
Make sure that the password is secure and unique (14+ characters)
I'm trying to build a tiny skeleton framework for a friend, where each time a button is pressed a certain animation is played. He wants a way to count the number of times the button is clicked, as well, but I can't seem to get that part working. What am I doing wrong?
<?php
if( isset($_POST['mushu']) )
{
echo "Working.";
playAnimation();
clickInc();
}
function playAnimation()
{
/* ... */;
}
function clickInc()
{
$count = ("clickcount.txt");
$clicks = file($count);
$clicks[0]++;
$fp = fopen($count, "w") or die("Can't open file");
fputs($fp, "$clicks[0]");
fclose($fp);
echo $clicks[0];
}
?>
<html>
<head>
<title>Adobe Kitten</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="button"
value="Let's see what Mushu is up to."
name="mushu">
</form>
</body>
</html>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit"
value="Let's see what Mushu is up to."
name="mushu">
</form>
First of all use the form with method="post", or change $_POST[] to $_GET[] in your Script.
And If your Button is not a Submit button, then you are not submitting the form. So I've changed type="button" to type="submit".
Should work
The code looks fine, I tested it and it worked for me.
I suggest:
Make sure the file isn't read-only.
Make sure the file is called "clickcount.txt"
Make sure it's in the same folder as your script.
It would be helpful to know the error but a shot in the dark - it could be a problem with Write permissions?
also, change to:
<input type="submit" value="Let's see what Mushu is up to." name="mushu" />