Undefined array key error while uploading images on php [duplicate] - php

This is my error:
Notice: Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 21 Notice:
Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 23 please
uploaded
How to get rid of it?
Html Code:
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>
Php Code:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['temp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = '../uploads/';
}
if (move_uploaded_file($temp_name, $location.$name)) {
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>

Make sure you have set form attribute enctype="multipart/form-data".
This attribute help you to get files from user.
<form action="PATH" method="post" enctype="multipart/form-data"></form>

Change your PHP script as below and try
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name) and !empty($name)){
$location = '../uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
} else {
echo 'You should select a file to upload !!';
}
}
?>

this happens due to the size of the file :
max_execution_time = 300
max_input_time = 240
post_max_size = 128M
upload_max_filesize = 128M
in your php.ini file you should change above codes according to your requirement...

Do a check around your PHP code block checking if either the submit button has been pressed or if isset($_FILES['file']). This should remove your errors. They pop up because the $_FILES['file'] isn't populated before the submit button is pressed.

Usually, the problem is forgetting to add this line as a form tag attribute.
enctype="multipart/form-data"
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only if method="post".

Spelling mistake:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name']; // tmp_name
if(isset($name)){
if(!empty($name)){
$location = '../uploads/';
}
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>

$upload_dir="../uploads";
$target_file="";
$tmp_file="";
if(isset($_POST['submit']))
{
$tmp_file=$_FILES['file']['tmp_name'];
$target_file=basename($_FILES['file']['name']);
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
{
echo "File uploaded <br />";
}
else {
echo "Something went Wrong !!<br/>";
}
}

if you are getting Notice: Undefined index: zip_file in error message most of the time, While uploading any file to server using php, Then here is the solution for this. only you need to mention enctype type in form tag.
<form method="post" action="" name="login" enctype="multipart/form-data">

Check if file_uploads is enabled on your php.ini
file_uploads = On

the solid solution to this problem is to use
if(isset($_POST['submit-button'])){
$option="";
$option=$_POST["anbieterin_geburtstag_month"];
echo $option;
}
always use isset function

Related

How to upload image to database using php and mysql? [duplicate]

This is my error:
Notice: Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 21 Notice:
Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 23 please
uploaded
How to get rid of it?
Html Code:
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>
Php Code:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['temp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = '../uploads/';
}
if (move_uploaded_file($temp_name, $location.$name)) {
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>
Make sure you have set form attribute enctype="multipart/form-data".
This attribute help you to get files from user.
<form action="PATH" method="post" enctype="multipart/form-data"></form>
Change your PHP script as below and try
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name) and !empty($name)){
$location = '../uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
this happens due to the size of the file :
max_execution_time = 300
max_input_time = 240
post_max_size = 128M
upload_max_filesize = 128M
in your php.ini file you should change above codes according to your requirement...
Do a check around your PHP code block checking if either the submit button has been pressed or if isset($_FILES['file']). This should remove your errors. They pop up because the $_FILES['file'] isn't populated before the submit button is pressed.
Usually, the problem is forgetting to add this line as a form tag attribute.
enctype="multipart/form-data"
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only if method="post".
Spelling mistake:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name']; // tmp_name
if(isset($name)){
if(!empty($name)){
$location = '../uploads/';
}
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>
$upload_dir="../uploads";
$target_file="";
$tmp_file="";
if(isset($_POST['submit']))
{
$tmp_file=$_FILES['file']['tmp_name'];
$target_file=basename($_FILES['file']['name']);
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
{
echo "File uploaded <br />";
}
else {
echo "Something went Wrong !!<br/>";
}
}
if you are getting Notice: Undefined index: zip_file in error message most of the time, While uploading any file to server using php, Then here is the solution for this. only you need to mention enctype type in form tag.
<form method="post" action="" name="login" enctype="multipart/form-data">
Check if file_uploads is enabled on your php.ini
file_uploads = On
the solid solution to this problem is to use
if(isset($_POST['submit-button'])){
$option="";
$option=$_POST["anbieterin_geburtstag_month"];
echo $option;
}
always use isset function

move_upload_file() is not working

<?php
if (isset($_FILES['file']['name'])){
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$location = 'uploads/';
if (!empty($name)){
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded successful';
}
}else
echo 'Please select a file.';
}?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
I am not getting any error but the move_uploaded_file() is not working. It displayed no result on the browser. I have the folder 'uploads/' inside my directory.
The problem is solved, the maximum file size for the file to be uploaded is 2MB. The file that I tried to upload exceed 0.1MB. Thank all of you.

Undefined variable error when uploading image to wampserver

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';
}
}
?>

Undefined Index while uploading file

This is my error:
Notice: Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 21 Notice:
Undefined index: file in
C:\xampp\htdocs\Project\Template1\users\index.php on line 23 please
uploaded
How to get rid of it?
Html Code:
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>
Php Code:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['temp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = '../uploads/';
}
if (move_uploaded_file($temp_name, $location.$name)) {
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>
Make sure you have set form attribute enctype="multipart/form-data".
This attribute help you to get files from user.
<form action="PATH" method="post" enctype="multipart/form-data"></form>
Change your PHP script as below and try
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name) and !empty($name)){
$location = '../uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
this happens due to the size of the file :
max_execution_time = 300
max_input_time = 240
post_max_size = 128M
upload_max_filesize = 128M
in your php.ini file you should change above codes according to your requirement...
Do a check around your PHP code block checking if either the submit button has been pressed or if isset($_FILES['file']). This should remove your errors. They pop up because the $_FILES['file'] isn't populated before the submit button is pressed.
Usually, the problem is forgetting to add this line as a form tag attribute.
enctype="multipart/form-data"
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only if method="post".
Spelling mistake:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name']; // tmp_name
if(isset($name)){
if(!empty($name)){
$location = '../uploads/';
}
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
} else {
echo 'please uploaded';
}
?>
$upload_dir="../uploads";
$target_file="";
$tmp_file="";
if(isset($_POST['submit']))
{
$tmp_file=$_FILES['file']['tmp_name'];
$target_file=basename($_FILES['file']['name']);
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
{
echo "File uploaded <br />";
}
else {
echo "Something went Wrong !!<br/>";
}
}
if you are getting Notice: Undefined index: zip_file in error message most of the time, While uploading any file to server using php, Then here is the solution for this. only you need to mention enctype type in form tag.
<form method="post" action="" name="login" enctype="multipart/form-data">
Check if file_uploads is enabled on your php.ini
file_uploads = On
the solid solution to this problem is to use
if(isset($_POST['submit-button'])){
$option="";
$option=$_POST["anbieterin_geburtstag_month"];
echo $option;
}
always use isset function

move_uploaded_file help

<?php
if (count($_POST)) {
$userfile = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_temp = $_FILES['userfile']['tmp_name'];
$path = 'uploads/';
$randomizer = rand(0000, 9999);
$file_name = $randomizer.$userfile;
if($file_size > 25600) {
echo 'FILE SIZE TO LARGE<BR />';
exit();
}
if (move_uploaded_file($file_temp, $path.$file_name)) {
echo '<h3>Upload Successful!</h3>';
} else {
echo '<h3>ERROR</h3>';
}
}
?>
<form enctype="multipart/form-data" method="post">
Upload File: <input name="userfile" type="file" /> <br />
<input type="submit" value="Upload File" />
</form>
I do have a folder named uploads in my root but still this wont work, i dont even get the error. Getting tired..Whats wrong?
You might be better off doing something like:
if (isset($_FILES['userfile'))
In your current code, do you know whether the lines within the "if (count($_POST))" block actually execute? The first thing I would've done is shoved a print_r call (or even just echoed some text) into both the if block and created an else block with an echo statement.
It just means you know what it's trying to do, and where it's failing, rather than trying to make guesses at it.

Categories