Upload video Using PHP - javascript

I'm Trying this code to upload video.
but the code below is not working for me, can someone help?
Plese correct the code if required.
I dont know php well..
<?php
mysql_connect("localhost","root","123456789");
mysql_select_db("vid");
if(isset($_POST['submit']))
{
$name = $_FILES['file']['name'];
$name = $_FILES['file']['temp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url = "http://localhost/video/uploaded/$name";
mysql_query("INSERT INTO `videos` VALUE ('','$name','$url') ");
}
?>
<body>
Videos
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<br><br>
<input type="submit" name="submit" value="Upload" />
</form>
<?php
if(isset($_POST['submit']))
{
echo "<br/>".$name." has been Uploaded";
}
?>
</body>
Thanks

Related

Changing site title server-side

How can I change a web page title for everyone on that site and not to be changed with refreshing, using javascript or PHP?
I've tried HTML DOM title but it gets back to its first title after refreshing.
I've tried giving variable in PHP and changing it by after a button pressed but the title didn't change!
These were my codes:
<?php
$title = "Chat";
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div id="chat_messages"></div>
<form enctype="multipart/form-data" method="post">
<input onClick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
<?php
if(isset($_POST['send'])){
$title = "new message";
}
?>
</body>
</html>
Any idea what should I do?
Glad to help.
Of course the title won't change, because php reads your code from 1st line to the end, and you have already added $title = "Chat";
what to do is to change the code like this:
<?php
if(isset($_POST['send'])) $title = "new message";
else $title = "Chat";
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<div id="chat_messages"></div>
<form enctype="multipart/form-data" method="post">
<input onClick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
</body>
</html>
This will change the title.
Also, if you want to change the title without refreshing the page, you need JS.
Just use
document.title = '***'; //use your title to replace "***"!
Have a good day~
There are some mistakes in your code like you have used onClick instead you should use onclick
you have not putted a semicolon ; while you are typing
You have not assigned action to your form
Action will be echo htmlspecialchars($_SERVER['PHP_SELF'])
And we will use if else
This will work
<?php
if (isset($_POST['send'])) {
$title = "new message";
} else {
$title = "Chat";
}
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<div id="chat_messages"></div>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" enctype="multipart/form-data" method="post">
<input onclick="dokme()" class="button2" type="submit" name="send" value="Send">
</form>
</div>
</body>
</html>
I hope this will Help you

How to delete values from JSON object array in php?

I have a PHP code and JSON as shown below:
PHP Code:
<?php if (!empty($_POST) && isset($_POST['savechanges']) && $_POST['savechanges'] == 1 && isset($_SESSION['pageadmin'])) {
$output = array();
$output['en_desc']=$_POST['en_desc'];
$output['code']=$_POST['code'];
$fp = fopen('../feeds/ptp-ess_landing_scommittees.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
}
if(file_exists('../feeds/ptp-ess_landing_scommittees.json')){
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
}
?>
<?php if($data) { ?>
<form method="post" id="myform" style="text-align:left;">
<input type="hidden" id="savechanges" name="savechanges" value="1">
<div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
<button type="submit">Save</button>
</div>
<?php foreach ($data->code as $key => $value) { ?>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
<input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
</div>
<?php } ?>
</form>
<?php } else { echo 'Cannot read JSON settings file'; }?>
JSON:
{"code":["AEFA","AGFO"], "en_desc":["Foreign Affairs and International Trade","Agriculture and Forestry"]}
The following DOM is generated through the PHP/JSON code above:
DOM (HTML):
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
<input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
<input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>
The following JS code deletes a row from the DOM on click of a delete button. On refreshing the page,
the deleted row comes back again as everything is rendered through JSON.
JS code:
<script>
function removeRow(el) {
el.parentNode.remove();
}
</script>
Problem Statement:
The above JS code is deleting the row (on click of a delete button) from the DOM but on refresing the page, everything is rendered again.
I am wondering what PHP code I need to add so that it delete the values from the JSON on saving the form when row is deleted from DOM through JS.
Step 1: User delete the row from the DOM on click of a delete button.
Step 2: On saving the form and rendering the page, that deleted row should not be present.
I know I have to use unset function in order to remove the values from the JSON but I am not sure how I can integrate it in the form.
unset($data->code);
unset($data->en_desc);
You have a typo here:
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
it should be
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
Look at the "s" :)
Edit: you also were saving the new file without actually checking if there is a post happening, here is the full code:
<?php
if (isset($_POST['submit'])) {
$output = array();
$output['en_desc'] = $_POST['en_desc'];
$output['code'] = $_POST['code'];
$fp = fopen('../feeds/ptp-ess_landing_committees.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
}
if (file_exists('../feeds/ptp-ess_landing_committees.json')) {
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
}
?>
<?php if ($data) { ?>
<form method="post" id="myform" style="text-align:left;">
<div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
<button type="submit" name="submit">Save</button>
</div>
<?php foreach ($data->code as $key => $value) { ?>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
<input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
</div>
<?php } ?>
</form>
<?php } else {
echo 'Cannot read JSON settings file';
} ?>
<script>
function removeRow(el) {
el.parentNode.remove();
}
</script>

How to merge two php page into one php

I have index.php and upload.php. index.php gives option of selecting an image and once clicked on upload image button, upload.php is called which then creates a directory and saves the image.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />
<input type="submit" value="Upload Images" />
</form>
</body>
</html>
upload.php
<?php
$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;
echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile);
?>
When upload.php is called, the page redirects to upload.php page and shows the upload file path. I dont want to redirect to upload.php. I want to keep everything in single index.php file. So that after uploading the images, it shows the upload file path on the same index.php. How can we achieve it.?
Put both in one PHP page and set the action of your form to the page itself. it would do the trick :)
<!DOCTYPE html>
<html>
<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />
<input type="submit" value="Upload Images" />
</form>
</body>
</html>
<?php
if(!empty($_FILES['userfile']))
{
$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;
echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}
?>
You can achieve that by using the functionality of PHP_SELF environment variable.
You have to make the action attribute as form action = <?php echo $_SERVER['PHP_SELF']; ?> and then add the code of upload.php into the same index.php.
For reference : Refer this link

submitting content to a .txt using php and staying on the same page and not go to the php page

I wanna submit/write content to a .txt file and i have to used php to do it, but i don't wanna open the php page and wanna stay on the same page.
How can I do this?
Index.html
<form action="writer.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
writer.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo " written to file";
}
}
else {
die('no post data to process');
}
?>`
You could simply use iframes, easier alternative to AJAX.
<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
...and as everyone here is yelling, consider learning AJAX.
create a php file with following in it.
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php if(isset($message)) echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>

When I write page name in action attribute the file image does not upload

When I write page name in action attribute the file image does not upload. If i write "#" in action page the image upload to target folder.
Please check the coding and mention mistake. Thanks in advance.
<form method="post" enctype="multipart/form-data" action="#">
<input type="file" name="imageupload" align="center">
<input type="submit" name="frmsubmit" value="Submit">
</form>
<?php
include 'includes/dbconnection.php';
if(isset($_POST['frmsubmit'])){
$image_name = $_FILES['imageupload']['name'];
$image_type = $_FILES['imageupload']['type'];
$image_size = $_FILES['imageupload']['size'];
$image_tmp = $_FILES['imageupload']['tmp_name'];
$target_dir = "dp/";
$path = $target_dir . $image_name;
move_uploaded_file($image_tmp, $path);
echo "The file ". basename( $_FILES["imageupload"]["name"]). " has been uploaded."; }?>
In form's action you can use $_SERVER['PHP_SELF']. Then it will send the files to same script. If you are using same php script to upload the file then it is good. I have modified you code please have a look at that
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="file" name="imageupload" align="center">
<input type="submit" name="frmsubmit" value="Submit">
</form>
<?php
include 'includes/dbconnection.php';
if(isset($_POST['frmsubmit'])){
$image_name = $_FILES['imageupload']['name'];
$image_type = $_FILES['imageupload']['type'];
$image_size = $_FILES['imageupload']['size'];
$image_tmp = $_FILES['imageupload']['tmp_name'];
$target_dir = "dp/";
$path = $target_dir . $image_name;
move_uploaded_file($image_tmp, $path);
echo "The file ". basename( $_FILES["imageupload"]["name"]). " has been uploaded."; }?>
Then after successful file upload you can use header("Location") to transfer user to any page.

Categories