Uploaded file not found in database - php

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);
}
}

Related

Image doesn't appear on MYSQL database

My problem is that if i try to post a image trought my website to my database called photos they don't appear there. Images do appear inside the folder i created upload/img. My database photos table is called images with 2 rows. id int(11) and image MEDIUMBLOB. Any ideas why doesn't my images appear on my MYSQL database?
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<?php if(isset($_SESSION['err'])){ ?>
<h2><?php echo $_SESSION['err']; ?></h2>
<?php session_unset(); } ?>
<form method="post" action="upload.php" enctype="multipart/form-data" >
<input type="file" name="image" />
<input type="submit" value="Submit" name="save">
</form>
</body>
</html>
My upload.php
<?php
require_once('config.php');
session_start();
if(isset($_POST['save']))
{
$target_dir = "upload/img/";
$filename = explode('.',$_FILES['image']['name']);
$ext = $filename[1];
$imgname = time().'.'.$ext;
$target_file = $target_dir . $imgname ;
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$path=$imgname;
$conn->query("INSERT INTO images (id, image)VALUES ('$path')");
$_SESSION["Success"]='Image Is Upload Success...';
header("Location:view.php"); /* Redirect browser */
exit();
} else {
$_SESSION["err"]=$text;
header("Location:index.php"); /* Redirect browser */
exit();
}
}
}
?>
Here is picture of my "images" table.
it has to be
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$path=$imgname;
$conn->query("INSERT INTO images (image) VALUES ('$path')");
$_SESSION["Success"]='Image Is Upload Success...';
header("Location:view.php"); /* Redirect browser */
exit();
} else {
$_SESSION["err"]=$text;
header("Location:index.php"); /* Redirect browser */
exit();
}
because you are telling Mysql to insert id but you didn't provide it's value
So i figured it out why those pictures wont upload to my MYSQL database.
Like some people here suggested i didn't need "id" written in my INSERT part.
My table id row had to be set to primary and AUTO_INCREMENT(see image below).
Now things seem to be working fine. You can set image row type from varchar to blob.

network programming - send file from webserver to client

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>

php can't move file from tmp

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

php form send empty data online but works fine on localhost

i got some forms in php so i can upload data into a mysql database, i already do the trials on a localhost server with xampp, but when i upload my files into my host ftp the forms doesnt work,so, i was thinking it has something to do with the fact that i put this file and the "connect.php" one too under password restriction, and that has something to do with the file permissions too but i already grant permission to the user of the database and permission to the file and i also had tried without the password protected thing, and nothing, always had the same result, so i start to experiment a little and i figured out that the image uploads fine into the database but nothing else did it, knowing that i try to "echo out" the other input results but non of it show something, and i came into the conclusion that my form is sending empty data, SOMEONE CAN TELLME IF IM RIGHT?, WHY THIS IS HAPPENING! PLEASE HELP ,heres my code...
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div id="slider_upload">
<h1>Slider principal</h1>
<div class="forma">
<form action="sliderPrincipal.php" method="post" enctype="multipart/form-data">
<p><label for="encabezado">Encabezado</label>
<input type="text" id="encabezado" name="encabezado" /></p><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<p><label for="fileupload">File to upload</label>
<input type="file" id="fileupload" name="fileupload" /></p><br>
<p><label for="articulo">Articulo</label>
<textarea id="articulo" name="articulo" rows="26" style="width: 100%;" >Escribir aqui</textarea></p><br>
<button type="submit" name="submit" value"send">Upload File</button>
</form><br><br>
</div>
</div>
<div class="message">
<?php
$file_dir = "../uploaded";
if (isset($_POST['encabezado'])){
$encabezado = mysql_real_escape_string($_POST['encabezado']);
$articulo = mysql_real_escape_string($_POST['articulo']);
}
foreach($_FILES as $file_name => $file_array){
//echo "path: ".$file_array['tmp_name']."<br />\n";
//echo "name: ".$file_array['name']."<br/>\n";
//echo "type: ".$file_array['type']."<br/>\n";
//echo "size: ".$file_array['size']."<br/><br/>\n";
//echo "encabezado ".$encabezado;
if(is_uploaded_file($file_array['tmp_name'])){
move_uploaded_file($file_array['tmp_name'], "$file_dir/".$file_array['name']) or die ("Your image couldnt be uploaded <br>");
$newpath = addslashes("uploaded/".$file_array['name']);
include "connect.php";
$addfile = "INSERT INTO slider (encabezado, image, articulo) VALUES ('$encabezado','$newpath', '$articulo')";
$result = mysql_query($addfile);
if ( $result === FALSE ) {
echo "your post could not be uploaded but we already got your image in our files ";
} else{
echo '<p style="padding: 20px;"><h1>Your post was successfully uploaded <h2></p><br><br>';
}
mysql_close();
} else "No file found";
}
?>
</div>
</div>
</body>
</html>
$file_dir = "../uploaded";
if($_REQUEST['submit']) {
$encabezado = mysql_real_escape_string($_POST['encabezado']);
$articulo = mysql_real_escape_string($_POST['articulo']);
foreach($_FILES as $file_name => $file_array){
if(is_uploaded_file($file_array['tmp_name'])){
move_uploaded_file($file_array['tmp_name'], "$file_dir/".$file_array['name']) or die ("Your image couldnt be uploaded <br>");
$newpath = addslashes("uploaded/".$file_array['name']);
include "connect.php";
$addfile = "INSERT INTO slider (`encabezado`, `image`, `articulo`) VALUES ('$encabezado','$newpath', '$articulo')";
$result = mysql_query($addfile);
if ( $result === FALSE ) {
echo "your post could not be uploaded but we already got your image in our files ";
} else{
echo '<p style="padding: 20px;"><h1>Your post was successfully uploaded <h2></p><br><br>';
}
mysql_close();
} else "No file found";
}
}
Try above code.
Note: not tested.

Outputting an image from an SQL BLOB

I have a php file which stores an image in a BLOB in a Database. The uploading part works fine. However it won't display the image and I don't know why. Help appreciated.
Two files:
index.php
<!DOCTYPE HTML>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<h1>Upload an Image</h1>
<form action="index.php" method="POST" enctype="multipart/form-data">
<label for="image">File:</label>
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php
//connect
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("up") or die(mysql_error());
//file stuff
$file= $_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select an image";
else {
$image=mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$imageName=mysql_real_escape_string($_FILES['image']['name']);
$imageSize=getimagesize($_FILES['image']['tmp_name']);
if(!$imageSize)
echo "Thats not an image";
else {
//upload
$query="INSERT INTO store VALUES('','$imageName','$image')";
$sendQuery=mysql_query($query);
if(!$sendQuery)
echo "This is embarressing. It didn't work";
else {
$lastid=mysql_insert_id();
echo "Image was uploaded. <br>Your image:";
echo "<img src=get.php?id=$lastid/>";
}
}
}
?>
</body>
</html>
and get.php:
<?php
//connect
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("up") or die(mysql_error());
$id=mysql_real_escape_string($_REQUEST(['id']));
$imageQuery="SELECT * FROM store WHERE id=$id;";
$sendImageQuery=mysql_query($imageQuery);
$image=mysql_fetch_assoc($sendImageQuery);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
PHP uploads, as stored in the ['tmp_name'] are TEMPORARY files, which are automatically deleted by PHP when the script ends/exits, unless you've taken measures to move it elsewhere. You'd need to have something more like
if ($_FILES['image']['error'] != UPLOAD_ERR_OK) {
... handle upload ...
move_uploaded_file($_FILES['image']['tmp_name'], "/path/in/your/document/root/$id.jpg");
} else {
die("Upload failed with error code: " . $_FILES['image']['error']);
}
first. Note the addition of the error check - NEVER assume that an upload succeeded.

Categories