Undefined variable error when uploading image to wampserver - php

I would need some advice/assistance here. I'm trying to upload image but wampserver keeping showing me this error (Notice: Undefined variable: name in C:\wamp\www\Shopaholic\upload_file.php on line 32), would appreciate if anyone can assist here. Thanks
here is my code
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = '..Shopaholic/upload/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
}
} else {
echo 'please uploaded';
}
}
$sql1= mysql_query("INSERT INTO dumimage (name)values('$name')");
?>
this is the error line 32: $sql1= mysql_query("INSERT INTO dumimage (name)values('$name')");

You need to wrap your query in your IF statement. If it doesn't exist you can't use it. I'd put it after your file is uploaded and confirmed as succeeding. (Might want to check your IF statements, too. Not sure all of them are necessary).
<?php
$conn = mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("shopaholic",$conn) or die (mysql_error());
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = '..Shopaholic/upload/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
$sql1= mysql_query("INSERT INTO dumimage (name)values('$name')");
}
}
} else {
echo 'please uploaded';
}
}
?>

Related

Could not find the file and upload

I trying to do multiple image upload to the system but it won't able to found the images after upload.
When i try to var_dump the total number of item uploaded it keep showing 0 item uploaded.
Need some help here, Thank You.
This is my HTML code:
<form id="login-form" action="" method="post" enctype="multipart/form-data" >
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
This is my PHP code:
<?php
//Check Error
error_reporting(E_ALL);
//Start Session
session_start();
//Include Database
include_once('dbConnect.php');
if (isset($_POST['submit-scam'])){
echo "button clicked";
if(isset($_FILES['file'])){
/*This Function is to loop multiple upload file into DB*/
$total = count($_FILES['file']['name']);
for ($i=0; $i<$total; $i++) {
$file = rand(1000,100000)."-".$_FILES['file']['name'][$i];
$file_loc = $_FILES['file']['tmp_name'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_type = $_FILES['file']['type'][$i];
$folder="uploads/";
$today = date('Y-m-d');
// 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))
{
//To Insert Latest base to SQL Latest submit ec_claim id
$sql="INSERT INTO db_evidence(db_evidence_name,db_evidence_type,db_evidence_scam_id,db_evidence_users_id)
VALUES('$final_file','$file_type','1','1')";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
echo "success upload";
// header("Location:index.php");
}
else
{
echo "failed to upload";
}
}
} else {
echo "error no file is found";
}
}
?>
Add enctype="multipart/form-data" to the form
<form id="login-form" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" multiple="multiple" />
<input type="submit" name="submit-scam" value="Submit">
</form>
That should let you read from type="file"

Image not being Uploaded in MySQL

This all started with me getting an unidentified index error: image. Then I put in the check before so I would not have to see the error again. However I know the images are not being uploaded. I have used legit images to test, and nothing is working. I have been struggling on this for hours. Please Help.
<html>
<body>
<form action="images-add.php" method="post" enctyp="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="sum" value="upload" />
</form>
</body>
</html>
<?php
include('connection.php');
if(isset($_POST['image'])) {
if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
$image= addslashes($_FILES(['image']['tmp_name']));
$name= addslashes($_FILES(['image']['name']));
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
}
function saveimage($name, $image){
$con= mysql_connect("localhost", "root", "");
mysql_select_db("hw2", $con);
$qry= "insert into images (name, image) values ('$name', '$image')";
$result= mysql_query($qry,$con);
if($result) {
echo "<br/> Image Uploaded.";
}else{
echo "Not Uploaded";
}
}
?>
Please change enctyp to enctype in your HTML form.
Also update code
From
$image= addslashes($_FILES(['image']['tmp_name']));
To
$image= addslashes($_FILES['image']['tmp_name']);
And
This line of code, From
$name= addslashes($_FILES(['image']['name']));
To
$name= addslashes($_FILES['image']['name']);
Complete code is given below...
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="sum" value="upload" />
</form>
</body>
</html>
<?php
include('connection.php');
if($_POST){
if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name'])){
$image= addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
}
function saveimage($name, $image){
$con= mysql_connect("localhost", "root", "");
mysql_select_db("hw2", $con);
$qry= "insert into images (name, image) values ('$name', '$image')";
$result= mysql_query($qry,$con);
if($result) {
echo "<br/> Image Uploaded.";
}else{
echo "Not Uploaded";
}
}
?>
I hope this code will works for you.

How to upload image in a specific folder php?

I have xampp installed and I have a program that you choose a file and it gets uploaded to my server. I have a folder in htdocs called uploads thats meant for storing the pictures. When I upload them, it goes in the htdocs, but not the folder in htdocs i want. I did specify that i needed it to go there. Can someone help?
Heres the code:
<?php
#$name = $_FILES['file']['name'];
#$size = $_FILES['file']['size'];
#$type = $_FILES['file']['type'];
#$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name))
{
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location. $name));
echo 'Uploaded';
}
else
{
echo 'Please choose a file';
}
}
?>
<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>
Try below code:
Note: Make sure your uploads folder have write permission.
<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name))
{
if (!empty($name))
{
if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
echo 'Uploaded';
else
echo "Not Uploaded";
}
else
{
echo 'Please choose a file';
}
}
?>

Undefined index: on uploading an image into database from php

Sorry for that question, but I tried everything and i can't solve my problem.
I'm trying to add an image to the database from a form in php and then show the image on the page.
However I am with an error I can't solve.
The error is this: Notice: Undefined index: image in C: \ xampp \ htdocs \ test \ index2.php on line 18
Does anyone could help me please?
Thanks everyone.
-----index.php-----------
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="index2.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
</form>
<?php
if (isset($_FILES['image'])) {
mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('database') or die (mysql_error());
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select an image";
else{
echo $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['tmp_name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image";
else{
if(!$insert = mysql_query("INSERT INTO table_image(name_image, image) VALUES ('Test','$image' )"))
echo"Problem uploading image.";
else
{
$lastid = mysql_insert_id();
echo"Image uploaded.<p />Your Image: <p /><img src=get.php?id=$lastid>";
}
}
}
}
?>
------get.php-----
<?php
mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('database') or die (mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("select * from table_image where id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
Line 18: $file = $_FILES['image']['tmp_name'];
--------------EDIT----------------
thanks everyone for all sugestions.
I tried everything you guys said. And the result was this.
What's going on?
You forgot the 't' here.
enctype="mulipart/form-data"
so
enctype="multipart/form-data"
You're also trying to check for isset($_POST['image']) which should be isset($_FILES['image'])
apart from typo fix of mulipart/form-data to multipart/form-data You need to validate/check it before accessing
if (!empty($_FILES['image']['name'])) {
$file = $_FILES['image']['tmp_name'];
}

Notice: Undefined index: image - unable to find the error

here is my error.i dont know why it is not working.i checked all the parts but i was unable to find the error.
Notice: Undefined index: image in C:\xampp\htdocs\Final\places\Ressave.php on line 27
here is my html code that pass the name of input:
<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on">
<div class="well">
<legend>Photos</legend>
<div class="control-group">
<label class="control-label">Upload Photo: </label>
<div class="controls">
<input name="image" type="file" />
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn">Cancel</button>
</div>
</form>
Ressave.php is here and can not recieve the name here,so the error occure.....
<?php
{ // Secure Connection Script
include('../Secure/dbConfig.php');
$dbSuccess = false;
$dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);
if ($dbConnected) {
$dbSelected = mysql_select_db($db['database'],$dbConnected);
if ($dbSelected) {
$dbSuccess = true;
} else {
echo "DB Selection FAILed";
}
} else {
echo "MySQL Connection FAILed";
}
// END Secure Connection Script
}
if(! $dbConnected )
{
die('Could not connect: ' . mysql_error());
}
{ // File Properties
$file = $_FILES['image']['tmp_name']; //Error comes from here(here is the prob!)
if(!isset($file))
echo "Please Choose an Image.";
else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
echo "That is not an image.";
else
{
$lastid = mysql_insert_id();
echo "Image Uploaded.";
}
}
}
{ //join the post values into comma separated
$features = mysql_real_escape_string(implode(',', $_POST['features']));
$parking = mysql_real_escape_string(implode(',', $_POST['parking']));
$noise = mysql_real_escape_string(implode(',', $_POST['noise']));
$good_for = mysql_real_escape_string(implode(',', $_POST['good_for']));
$ambience = mysql_real_escape_string(implode(',', $_POST['ambience']));
$alcohol = mysql_real_escape_string(implode(',', $_POST['alcohol']));
}
$sql = "INSERT INTO prestaurant ( ResName, Rating, Food_serve, Features, Parking, noise, Good_For, Ambience, Alcohol, Addition_info, Name, Address1, Zipcode1, Address2, Zipcode2, Address3, Zipcode3, City, Mobile, phone1, phone2, phone3, phone4, Email1, Email2, Fax, Website, image)".
"VALUES ('$_POST[restaurant_name]','$_POST[star_rating]','$_POST[food_served]','$features','$parking','$noise','$good_for','$ambience','$alcohol','$_POST[description]','$_POST[name]','$_POST[address1]','$_POST[zipcode1]','$_POST[address2]','$_POST[zipcode2]','$_POST[address3]','$_POST[zipcode3]','$_POST[city]','$_POST[mobile]','$_POST[phone1]','$_POST[phone2]','$_POST[phone3]','$_POST[phone4]','$_POST[email1]','$_POST[email2]','$_POST[fax]','$_POST[url]','$image')";
mysql_select_db('place');
$retval = mysql_query( $sql );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($dbConnected);
?>
If a file was not uploaded the $_FILES array will be empty. Specifically, if the file image was not uploaded, $_FILES['image'] will not be set.
So
$file = $_FILES['image']['tmp_name']; //Error comes from here(here is the prob!)
should be:
if(empty($_FILES) || !isset($_FILES['image']))
update
You will also have issues because you're missing the enctype attribute on your form:
<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on" enctype="multipart/form-data">
In order to be able to process files in your form you need to add the enctype attribute.
<form method='POST' enctype='multipart/form-data' >
Hey i guess you have forgotten one important setting in form
enctype="multipart/form-data" this option is used when used with files eg image file etc
<form name="image" method="post" enctype="multipart/form-data">
Apart from that to extract the contents of the file you can use following options
$tmp_img_path = $_FILES['image']['tmp_name'];
$img_name = $_FILES['image']['name'];
to print all the content of a file use:
print_r($_POST['image']);
this is the code i have which successfully insert the data into mysql and retrieve from the DB.
"Index.PHP"
<html>
<head>
<title>PHP & MySQL: Upload an image</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File: <input type="file" name="image" /><input type="submit" value="Upload" />
</form>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("registrations") or die(mysql_error());
if(!isset($_FILES['image']))
{
echo 'Please select an image.';
}
else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
echo $_FILES['image']['tmp_name'];
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE){
echo "That's not an image.";
} else {
if(!$insert = mysql_query("INSERT INTO test_image VALUES ('','$image_name','$image')"))
{
echo "Problem uploading image.";
} else {
$lastid = mysql_insert_id();
echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
this is get.PHP
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("registrations") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM test_image WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
NOTE:- please changes your DB and table name accordingly..
Thanks,
Santanu

Categories