I wrote a php script to upload files to a webserver. There I am able to download it as well. I want to shift the files from the server to a client, so everytime a file is uploaded, it gets pushed to the client as well. Is it even possible to handle that with php scripts, where do I define these operations besides the php scripts, or can it be integrated?
My php scripts for index and upload file:
index:
$sql = "SELECT * FROM documents";
$res = mysqli_query($con, $sql);
?>
<html>
<head>
<title>Documents</title>
</head>
<body>
<nav id="nav">
<ul>
<li>Upload</li>
<li>Download</li>
</ul>
</nav>
</body>
</html>
upload:
<?php
include("db.php");
if(isset($_POST['submit'])){
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if($name){
$location = "/storage/ssd2/932/2536932/uploads/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query($con, "INSERT INTO documents (name, path) VALUES('$name', '$location')");
header('Location: index.php');
}
else
die("Please select a file.");
}
?>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Browse File:</label>
<input type="file" name="myfile">
<input type="submit" name="submit" value="Upload" onclick="index.php">
</form>
</body>
</html>
Related
This code working fine in localhost but working in online webserver
whenever I try to upload the files using online hosting, this code does not work for me:
<?php
if (isset($_POST['submit'])) {
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = 'images/';
if (move_uploaded_file($temp_name, $location . $name)) {
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="save.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
</body>
</html>
What am I doing wrong?
With this code I'm trying to upload a file to mysql database and then try and display it using another code (which I don't show here). The problem I'm having is that though this script executes, the uploaded file does not show.
<!DOCTYPE html>
<html lang="en">
<body>
<div class="container" >
<form action="insert.php" method="post" class="form-horizontal" role="form" enctype="multipart/form-data">
<div class="col-sm-offset-3">
<h2>Application Results</h2>
<label for="fileSelect">Please upload your application results here:</label>
<input type="file" name="application_results" ><br>
</div>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$file = rand(1000,100000)."-".$_FILES['application_results']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$sql="INSERT INTO applications(application_results) VALUES('$file')";
mysql_query($sql);
}
?>
Please give permission to that uploads folder. Also it is better to have if condition before database insert. Adding if condition there will help you to identify if file is successfully uploaded or not.
if(move_uploaded_file($file_loc,$folder.$file)) {
$sql="INSERT INTO applications(application_results) VALUES('$file')";
mysql_query($sql) or die(mysql_error());
}
If you have not solved this yet. Please try with tmp name.
move_uploaded_file($_FILES['application_results']['tmp_name'],$folder.$file)
Try this:
if(isset($_POST['submit']))
{
$uploads = $_FILES['application_results'];
$file = rand(1000,100000)."-".$_FILES['application_results']['name'];
$folder = 'uploads/' . $file;
if(move_uploaded_file($uploads['tmp_name'], $folder))
{
$sql="INSERT INTO applications(application_results) VALUES('$file')";
mysql_query($sql);
}
}
I have the following code to upload files to a database in mysql:
Index.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
<?php
if(isset($_GET['success']))
{
?>
<label>File Uploaded Successfully... click here to view file.</label>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<?php
}
?>
Upload.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
which works well, but only for individual file. I saw many tutorials, but I can not modify the code to allow upload multiple files at once. Any suggestions?
To allow multiple file uplodes you need to set attribute multiple="multiple" on the file input with a name file[]. Or you can use separate inputs with a name file[] as well. Than to store values in database make a
foreach($_FILES['filename']['name'] as $key=>$name){
// store each file and write data to DB for each file
//for example to get the name of file
$name = $_FILES['filename']['name'][$key];
}
Still a newb to coding . have almost no idea what im doing . i tried to make a php page which would let me upload and view an image . do not know what is wrong . i tried to do it as correctly as possible . could someone please help me out ?
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected ";
$imagename= mysqli_real_escape_string($_FILES['image'] ['name']);
$imagefile =mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$qry = "INSERT INTO image (name,file) VALUES ('$imagename','$imagefile')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo "$currimage <br/>";
}
?>
</body>
</html>
To be able to catch a post variable, you need to submit the form and handle the action. The first problem with your code is that your form is not complete - it's missing a closing tag. Second thing, to be able to send a file through the post, you'll need multipart form. You should add enctype="multipart/form-data" as an attribute of the form.
So, instead of
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
You'll need
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
</form>
you must move the uploaded image to server
try this code -- create directory /uploads/
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected";
$uploads_dir = '/uploads';
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$qry = "INSERT INTO image (name,file) VALUES ('$name','$tmp_name')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo '<img src="'.$currimage.'" /> <br/>';
}
?>
</body>
</html>
I'm stuck with a simple php code, which must move a uploaded file from tmp to a desired location. Please find my code below. I'm not sure where i'm going wrong.
Any help is appreciated.
php code:
<?php
define("UPLOAD_DIR", "/srv/www/wordpress/mywebpage/uploads/");
$scr = $_FILES["jb_cv"]["tmp_name"];
$desttmp = $_FILES["jb_cv"]["name"];
$dest = UPLOAD_DIR .$desttmp;
echo " source file : ";
echo $scr."\t";
echo " destination file : ";
echo $dest;
$success = move_uploaded_file($scr,$dest);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
?>
HTML code:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="move.php" id="contactform" name="contactform" enctype="multipart/form-data">
<label> Upload your CV</label> <input name="jb_cv" type="file" />
<input type="submit" value="SUBMIT" />
</form>
</body>
</html