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
}
}
}
}
?>
Related
I'm a php/mysql newbie and need your help!
I have an upload formula to upload multiple files to a database, selected one by one.
The files upload works fine, but I want to add a title field for each selected file. I have no idea how to implement this in my code.
Here is my code:
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file) VALUES ('$doc')";
mysqli_query($db, $sql_f);
}
}
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div>
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
<input type="file" name="sel_doc[]">
</div>
<div id="upload_button">
<button type="submit" name="upload_docs">upload</button>
</div>
</form>
This is how 1 upload would looks like:
| id_doc | doc_file | doc_title|upload_id|
++++++++++++++++++++++++++++++++++++++++++++++++
|.....1......|...a.pdf...|....vvv....|....1....|
|.....2......|...b.pdf...|....www....|....1....|
|.....3......|...c.pdf...|....xxx....|....1....|
|.....4......|...d.pdf...|....yyy....|....1....|
|.....5......|...e.pdf...|....zzz....|....1....|
In my orginal code I insert in 2 tables for 1:n realationship (because of the "upload_id").
HTML
<div>
<?php for ($i=0; $i<5; $i++){ ?>
<b><?php echo $i ?></b><br>
File: <input type="file" name="sel_doc[<?php echo $i ?>]"><br>
Title: <input name="title[<?php echo $i ?>]"><br>
<?php } ?>
</div>
Will print this:
<div>
<b>0</b><br>
File: <input type="file" name="sel_doc[0]"><br>
Title: <input name="title[0]"><br>
<b>1</b><br>
File: <input type="file" name="sel_doc[1]"><br>
Title: <input name="title[1]"><br>
<b>2</b><br>
File: <input type="file" name="sel_doc[2]"><br>
Title: <input name="title[2]"><br>
<b>3</b><br>
File: <input type="file" name="sel_doc[3]"><br>
Title: <input name="title[3]"><br>
<b>4</b><br>
File: <input type="file" name="sel_doc[4]"><br>
Title: <input name="title[4]"><br>
</div>
PHP code
$db = mysqli_connect("localhost", "root", "", "xy");
if (isset($_POST['upload_docs'])) {
for($mi = 0; $mi < count($_FILES['sel_doc']['tmp_name']); $mi++) {
$doc = $_FILES['sel_doc']['name'][$mi];
$tmp_doc = $_FILES['sel_doc']['tmp_name'][$mi];
$target = "doc_uploads/". basename($doc);
$title = (isset($_POST['title'][$mi])?$_POST['title'][$mi]:"");
if (move_uploaded_file($tmp_doc, $target)) {
echo "Upload successfully!";
}
else {
echo "Upload failed!";
}
$sql_f = "INSERT INTO docs (doc_file, title) VALUES ('$doc', '$title')";
mysqli_query($db, $sql_f);
}
}
Btw; This example code is not safe for sql injection.
I'm building a website that has a form that allows users to upload files, but the form seems to be going to the wrong place. I told it to go to "upload.php", but instead it tries to go to "“upload.php", which I did not include in the form action. It doesn't look like I added any unicode characters in the script. Any reason why this might be happening?
Here's my form in index.html:
https://pastebin.com/7PETk5Sy
<!DOCTYPE html>
<html>
<head>
<title>Deeper</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="titlebar">
<img src="DeeperNetIcon.jpg"></img>
<h1>DeeperNet</h1>
Back to Deeper Homepage
<br/><br/>
<form action="view.php" method="post">Search for World by ID: <input type="text" name="world"> <input type="submit" value="Search"></form>
<br/>
</div>
<p color="white">Welcome to DeeperNet!</p>
<div id="upload">
<h1>Upload World to DeeperNet</h1>
<form action=“upload.php” method="post" enctype="multipart/form-data">
World File:
<br/><br/>
<input type="file" name="world">
<br/><br/>
World Name:
<br/>
<input type="text" name="name">
<br/>
Description:
<br/>
<input type="textbox" name="desc">
<br/><br/>
<input type="submit" value="Upload World">
</form>
<br/>
</div>
</body>
The index.html File has 2 Forms, the second form is the one I'm talking about.
Here's my upload.php Script:
https://pastebin.com/0FyuAX3Q
<?php
include("index.html");
if(!empty($_POST)) {
if(isset($_FILES["world"])) {
$world = $_FILES["world"];
$name = $world["name"];
$tmp_name = $world["tmp_name"];
$size = $world["size"];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$allowed = "deep";
if ($ext == $allowed) {
if ($size <= 300000) {
$id = uniqid('', true);
$destination = 'worlds/' . $id . '.' . $ext;
if(move_uploaded_file($tmp_name, $destination)) {
echo '<br/>World: "' . $name . '" uploaded to DeeperNet!';
echo “<br/>World ID: ” . $id;
$info = fopen('worlds/' . $id . '.txt', 'w');
fwrite($info, $_POST['name'] . "\r\n");
fwrite($info, $_POST['desc']);
fclose($info);
}
}
}
MAMP says I'm using PHP 7.0.8 if you're wondering.
Replace your code with this as form accepts this double quotes (") not this one (“)
<form action="upload.php" method="post" enctype="multipart/form-data">
I'm trying to upload a file in php and unable to do it. The file I'm trying to upload is a csv file, but it should not be a concern. I'm using php to upload my file. I'm also trying to process the form in the same page. Below is my code for file upload and it is not working...
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['csv_file'])) {
echo "<p>".$_POST['csv_file']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
update your form code with enctype attribute
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype = "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
use enctype="multipart/form-data"
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
I have try below code and it's work perfect.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
echo "<p>" . $_POST['csv_file'] . " => file input successfull</p>";
$target_dir = "images ";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir . $file_name)) {
echo "<h1>File Upload Success</h1>";
} else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
</html>
It should be something like this
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if ($_FILES['csv_file']['size'] > 0)
{
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
?>
</body>
Below are the changes you need to make
Add enctype = "multipart/form-data" in form tag as new attribute
Change from this if(isset($_POST['csv_file'])) { to this if($_FILES['csv_file']['size'] > 0) { as you need to check size
whether its uploaded or not
Change from this echo "<p>".$_POST['csv_file']." => file input
successfull</p>"; to this echo "<p>".$_FILES['csv_file']['name']."
=> file input successfull</p>"; as you need to use $_FILES to get file name instead of $_POST
Last but not the least, complete </body> tag if you have not yet.
Upload code in PHP[without checking its extension]
<?php
if(isset($_POST['save'])){
$path="var/import/";
$name = $_FILES['csv_file']['name'];//Name of the File
$temp = $_FILES['csv_file']['tmp_name'];
if(move_uploaded_file($temp, $path . $name)){
echo "success";
}else{
echo "failed";
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="save" value="submit">
</form>
First Give permission to folder where you going to upload Ex: "var/import/" folder.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype= "multipart/form-data">
<input type="file" name="csv_file">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(isset($_FILES['csv_file']) && $_FILES['csv_file']['size'] > 0) {
echo "<p>".$_FILES['csv_file']['name']." => file input successfull</p>";
fileUpload();
}
}
function fileUpload () {
$target_dir = "var/import/";
$file_name = $_FILES['csv_file']['name'];
$file_tmp = $_FILES['csv_file']['tmp_name'];
if (move_uploaded_file($file_tmp, $target_dir.$file_name)) {
echo "<h1>File Upload Success</h1>";
}
else {
echo "<h1>File Upload not successfull</h1>";
}
}
for uploading files, the trick is enctype="multipart/form-data"
use this form definition
I want to save my excel file as table in database using PHP. Is it possible?
I tried like below. But we need to give table name.
<html>
<head>
<?php
if(isset($_POST["Import"]))
{
include 'database.php';
$filename=$_FILES["file"]["tmp_name"];
$heading=true;
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000)) !== FALSE)
{
$num = count($emapData);
echo $num;
echo $emapData[0];
//check if the heading row
if($heading) {
// unset the heading flag
$heading = false;
// skip the loop
continue;
}
$sql = "INSERT into sheet(name,first_name,last_name) values('$emapData[0]','$emapData[1]','$emapData[2]')";
$row=mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
</head>
<body>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">
Only Excel/CSV File Import.
</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
</body>
</html>
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. .