Undefined Index while uploading file
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 !!';
}
}
?>
Notice: Undefined index: file uploading video
You're trying to process the file upload before a file is uploaded. You have to check if the form was posted first.
<?php
if (isset($_FILES['file'])) {
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
// $_FILES = $_FILES['file']; // <-- remove this line
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$unique = date('Y-m-d_H-i-s');
if ((null !==($_FILES["file"]["type"] == "video/mp4")
|| (null !==($_FILES["file"]["type"] == "audio/mp3"))
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
echo 'File uploaded successfully';
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
$datetime = date('Y-m-d_H-i-s');
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"] . $datetime . md5($_FILES["file"]["name"]));
}
}
} else {
echo "Invalid file";
}
}
?>
<form action="profile.php" id="videoupload" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
undefined index in php while uploading an image file
When you first load the page, $_FILES['myimage']['name']
and $_FILES['myimage']['tmp_name'];
will be undefined because you haven't uploaded anything yet.
The solution is:
First add a
name
attribute your submit button, like this:<button type="submit" name="submit" class="btn btn-info">upload</button>
And wrap your form processing code inside an
if
block, like this:// your code
if(isset($_POST['submit'])){
$location="profilepics/";
$name=$_FILES['myimage']['name'];
$temp_name=$_FILES['myimage']['tmp_name'];
if(isset($name)){
move_uploaded_file($temp_name,$location.$name);
}
}
Undefined index error while trying to upload images
Change the following:
<form action="" method="POST">
to
<form action="" method="POST" enctype="multipart/form-data">
and try again.
To upload image using form
you have to send form-data
encoded as "multipart/form-data"
Notice: Undefined index: file when uploading file
For the next one looking for it :)
To upload a file you will need enctype="multipart/form-data"
:
<form enctype="multipart/form-data">
<input type="file" name="file"/>
</form>
PHP Undefined index error $_FILES?
first: try to strict programming
error_reporting(E_ALL | E_STRICT);
also you must use isset for check is index for array available or not
if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) {
// ...
}
also check php configuraion
file_uploads "1"
upload_max_filesize "2M"
post_max_size "8M"
max_file_uploads 20
post max size must be larger than upload max file size.
also as guys said check form enctype
Related Topics
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It
Jquery Mobile: Sending Data from One Page to the Another
Good Error Handling with File_Get_Contents
Order by Before Group by Using Eloquent (Laravel)
Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript
What Does New Self(); Mean in PHP
How to Get Exact Browser Name and Version
PHP Apple Enhanced Push Notification Read Error Response
PHP - Csrf - How to Make It Works in All Tabs
Split a Text into Single Words
In PHP, How to Check If a Function Exists
PHP Short Circuit Lazy Evaluation, Where Is It in the PHP.Net Manual
Xml Parse Error - Extra Content at the End of the Document