Load php response to div on the same page after uploading files - php

i am uploading the files to PHP script. it process and give response. i want to load that response in the div or frame on same web page. now the following script giving response in the blank web page. please help me to solve this.
php:
<html>
<head>
<script type="text/javascript">
function init() {
if(top.uploadDone) top.uploadDone();
}
window.onload=init;
</script>
<body>
<?php
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
if ($fn) {
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
else {
// form submit
$files = $_FILES['fileselect'];
//print $files['name'];
foreach ($files['error'] as $id => $err) {
if ($err == UPLOAD_ERR_OK) {
$fn = $files['name'][$id];
move_uploaded_file(
$files['tmp_name'][$id],
'uploads/' . $fn
);
if($id==1)
{
$filename = 'uploads/' . $fn;
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
$line = fgets($fp);
fclose($fp);
$arr = split(",",$line);
for($i=2;$i< count($arr);$i++)
{
print '<input type="checkbox" name="traits" value="1">'.$arr[$i]."<br>";
}
}
}
}
}
?>
</body>
</html>
Jquery:
function init() {
document.getElementById("upload").onsubmit=function() {
document.getElementById("upload").target = "Analysis";
document.getElementById("Analysis").onload = uploadDone; }
}
function uploadDone() { //Function will be called when iframe is loaded
var ret = frames['Analysis'].document.getElementsByTagName("body")[0].innerHTML;
}
HTML script:
<div id="content_column">
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<div id="Geno" style="display:none">
<strong>Select Genotypic File</strong>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<div>
<input type="file" id="fileselect" name="fileselect[]" />
</div>
</div>
<P>
<div id="Peno" style="display:none">
<strong>Select Penotypic File</strong>
<div>
<input type="file" id="fileselect1" name="fileselect[]" />
</div>
</div>
<P>
<div id="Other" style="display:none">
<strong>Select Other Files*</strong>
<div>
<input type="file" id="fileselect2" name="fileselect[]" multiple="multiple" />
</div>
</div>
<div id="submitbutton" style="display:none">
<INPUT TYPE="Submit" VALUE="Upload" />
</div>
</fieldset>
</form>
<iframe id="Analysis" name="Analysis" src="" ></iframe>
</div>
Thank you in adavance. .

Related

Form not sending any data when submitting form : $_POST is empty

I'm trying to submit a simple form with video files in it, a title and a description. My code works on my laptop that has a 8.1.9 php version and not working on my desktop that has 8.1.0 php version.
I checked this page : Form submit is not sending any POST data but I didn't find any solutions.
I checked php.ini but everything was set normaly... I checked for overlappings tags but didn't find any...
I hope that this is not to obvious...:/
Here is my code :
add-videos.php
<?php
require_once './partials/header.php';
include_once './fetch-videos.php';
?>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button id="myBtn">Ajouter une vidéo</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form action="add-videos-logic.php" method="POST" enctype="multipart/form-data">
<label for="video">Fichier vidéo</label>
<input type="file" name="video" /> <br><br>
<label for="title">Titre de la vidéo</label>
<input type="text" name="title" /> <br><br>
<label for="description">Description</label>
<input type="text" name="description" /> <br><br>
<input type="submit" name="submit" value="envoyer">
</form>
</div>
</div>
<?php endif ?>
<?php while ($fetch = mysqli_fetch_array($query)) { ?>
<h2> <?= $fetch['title'] ?> </h2>
<p><?= $fetch['description'] ?></p>
<video width="320" height="240" cont rols>
<source src="<?= $fetch['source'] ?>" data-id="<?= $fetch['id'] ?>">
</video>
<?php if (isset($_SESSION['user_is_admin'])) : ?>
<button><a href="delete-video.php?video_id=<?= $fetch['id'] ?>&source=<?= $fetch['source'] ?>">Supprimer</button>
<button type="submit" name="update" value="modifier"> </button>
<?php endif ?>
<?php } ?>
<?php
require_once './partials/footer.php';
?>
add-videos-logic.php
<?php
require './config/database.php';
date_default_timezone_set('Asia/Manila');
var_dump($_POST);
var_dump(isset($_POST["submit"]));
if (isset($_POST["submit"])) {
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
extract($post);
$file_name = $_FILES['video']['name'];
$file_temp = $_FILES['video']['tmp_name'];
$file_size = $_FILES['video']['size'];
if ($file_size < 50000000) {
$file = explode('.', $file_name);
$end = end($file);
$allowed_ext = array('avi', 'flv', 'wmv', 'mov', 'mp4');
if (in_array($end, $allowed_ext)) {
$name = date("Ymd") . time();
$location = 'video/' . $name . "." . $end;
if (move_uploaded_file($file_temp, $location)) {
mysqli_query($connection, "INSERT INTO `videos` VALUES('', '$name', '$location', '$description', '$title')");
header('location: ' . ROOT_URL . '/admin/add-videos.php');
}
} else {
echo "<script>alert('Wrong video format')</script>";
echo "<script>window.location = 'index.php'</script>";
}
} else {
echo "<script>alert('File too large to upload')</script>";
echo "<script>window.location = 'index.php'</script>";
}
}
file size configuration to modify in php.ini post_max_size was too small.
Sorry for this question.
Adios stackoverflow

FILE INPUT INSERT INTO BD WITH MOVE_TO_FILE

This is more of a question to gain a more clear understanding. If I insert from a form like:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
into the DB for a link and it's successful:
$file = "INSERT INTO ('foo') VALUES ('FOO', NOW())";
just an example:
yet in the php script:
$_FILE['fileToUpload']['name'];
$_FILE['fileToUpload']['tmp_name'];
since the tmp_name folder only holds upload files in an array, which than I would have to use either a foreach or for loop search the loop of files, this than makes the INSERT INTO db difficult.
Question is how can I separate the search results from the array and than insert each one into the database?
HERE"S THE CODE:
<?php
$con = mysqli_connect("localhost","root","","acc1");
if(mysqli_connect_errno()){
echo 'Failed to connect to MySQL:' . mysqli_connect_error();
}else{
echo 'Connected!';
}
if(isset($_POST['submit']) && !empty($_FILES['fileBC']['name'] && !empty($_FILES['fileB']['name'] && !empty($_FILES['fileBR']['name']) ))){
$file = "image/";
$name = $_FILES['fileBC']['name'];
$data = $_FILES['fileBC']['tmp_name'];
$fileV = "video/";
$nameV = $_FILES['fileBR']['name'];
$dataV = $_FILES['fileBR']['tmp_name'];
$fileB = "book/";
$nameB = $_FILES['fileB']['name'];
$dataB = $_FILES['fileB']['tmp_name'];
if(move_uploaded_file($data,$file.$name)){
$ins_name = $con->query("INSERT INTO fileimages (fileBC, fileBR, fileB) VALUES ('$name','$nameB', '$nameV')");
}if($ins_name){
echo 'success!';
}else{
echo 'error';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/fontawesome.css">
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
</head>
<script>
function mymodal(){
$('#myModal').modal('show');
}
</script>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<div class="form-group">
<label class="text-primary">Book Cover:</label>
<input class="form-control" type="file" name="fileBC" accept="image/*" >
</div>
<div class="form-group">
<label class="text-primary">Book:</label>
<input class="form-control" type="file" name="fileB" accept=".epub, .mobi, .pdf, .prc, .azw, .bbeb" >
</div>
<div class="form-group">
<label class="text-primary">Book Reading:</label>
<input class="form-control" type="file" name="fileBR" accept="video/*" >
</div>
<button name="submit">upload</button>
</form>
<p></p>
<ol>
</ol>
</body>
</html>
This is an example you can adept it as per your need.
<form action="file_reciever.php" enctype="multipart/form-data" method="post">
<input type="file" name="files[]" multiple/>
<input type="submit" name="submission" value="Upload"/>
</form>
PHP code is like
<?php
if (isset($_POST['submission'] && $_POST['submission'] != null) {
for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
//Your insert statement goes here
}
}
}
}
?>

Show file link after uploading

I've downloaded an image uploading script with Ajax from this Link
The codes are:
index.php
<script language="javascript" type="text/javascript">
<!--
function startUpload(){
document.getElementById('f1_upload_process').style.visibility = 'visible';
document.getElementById('f1_upload_form').style.visibility = 'hidden';
return true;
}
function stopUpload(success, str){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.visibility = 'hidden';
document.getElementById('f1_upload_form').innerHTML = result + str+'<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('f1_upload_form').style.visibility = 'visible';
return true;
}
//-->
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
<div id="content">
<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
<p id="f1_upload_form" align="center"><br/>
<label>File:
<input name="myfile" type="file" size="30" />
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</p>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
</div>
<div id="footer">Powered by AJAX F1</div>
</div>
</body>
Uploader.php
<?php
// Edit upload location here
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
// echo $target_path;
}
sleep(1);
?>
<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result.",".$target_path; ?>);</script>
I want to show the url of uploaded image. So that it can be use instantly.
I've a little knowledge about ajex. I tried by writing two three of codes but it's not working.
Anyone can help to show the file url.
Thanks in advance.
Hope this helps...
index.php
<html>
<head>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
function startUpload(){
document.getElementById('f1_upload_process').style.visibility = 'visible';
document.getElementById('f1_upload_form').style.visibility = 'hidden';
return true;
}
function stopUpload(success, str, filename){
var result = '';
var file_link = '';
var location = window.location.href;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
file_link = "<br/><a href='"+directoryPath+"/"+filename +"'>"+filename+"</a><br/>";
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.visibility = 'hidden';
document.getElementById('f1_upload_form').innerHTML = result + file_link +'<br/><br/><label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('f1_upload_form').style.visibility = 'visible';
return true;
}
//-->
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
<div id="content">
<form action="ajax-uploader.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
<p id="f1_upload_form" align="center"><br/>
<label>File:
<input name="myfile" type="file" size="30" />
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</p>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
</div>
<div id="footer">Powered by AJAX F1</div>
</div>
</body>
</body>
</html>
ajax-uploader.php
<?php
// Edit upload location here
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
$actual_name = basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
// echo $target_path;
}
sleep(1);
?>
<script language="javascript" type="text/javascript">window.top.stopUpload(<?php echo "$result, '$target_path','$actual_name' "; ?>);</script>
change Uploader.php to upload.php.
Add php code after sleep as follows..
sleep(1);
$target_path = str_replace('\\', '/', $target_path);
$spath = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/".$_FILES['myfile']['name'];
?>
<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo "'".$result."','".$target_path.',img url :'.$spath."'";?>);</script>

i tried to upload a file to upload folder but getting failure message

my directory is looks like this
->source files
--css
--upload
add_file.php
upload.php
my code is as follows
upload.php
<form role="form" method="post" enctype="multipart/form-data" action="add_file.php">
<div class="form-group">
<label for="filecaption">
Caption :
</label>
<input type="text" name="f_caption" class="form-control"/>
</div>
<div class="form-group">
<label for="Choose a File">
Caption :
</label>
<input type="file" name="uploaded_file" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" name="upload" value="Upload" class="form-control btn btn-warning"/>
</div>
</form>
add_file.php
<?php
if(isset($_FILES['uploaded_file']))
{
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($f_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is from the add.php.
Try this
<?php
if(isset($_FILES['uploaded_file'])) {
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($temp_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is that you were moving from the f_name instead of the temp_location.

Upload image php

I'm trying to upload an image in a specific folder, but I'm getting nothing, not even an error message, when I click on upload button I get a blank page, and that's it, What am I missing?
<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form2")) {
$nome_arquivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "../legendofgames/documentos/games/".$nome_arquivo);
?>
<script>
opener.document.form2.strImage.value="<?php echo $nome_arquivo; ?>";
self.close();
</script>
<?php
}
else
{?>
.
<form method="post" enctype="multipart/form-data" id="form2">
<p>
<input name="userfile" type="file" />
</p>
<p>
<input type="submit" name="button" id="button" value="Upload Image" />
</p>
<input type="hidden" name="enviado" value="form2" />
</form>
<?php }?>
1.turn on error reporting in php.ini file or add this line at first:
<?php error_reporting(E_ALL); ?>
2.It seems that u have an syntax error on line 4: forget to close php code by ?>
<?php if ((isset($_POST["enviado"])) && ($_POST["enviado"] == "form2")) {
$nome_arquivo = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "../legendofgames/documentos/games/".$nome_arquivo);
?>
<script>
opener.document.form2.strImage.value="<?php echo $nome_arquivo; ?>";
self.close();
</script>
<?php
}
else
{?>

Categories