Upload image to database Undefined index - php

I made theform working with storing the images directly on database and now, I want to learn how to store them in a folder, and store in database just the path.
At this moment I get this error Notice: Undefined index: uploaded_file in and I really don't understand why. Please, some F1 :)
Html form:
<form action="ad_cont.php" method="POST" class="add_contact" name="add_contact" enctype="multipart/form-data">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
Php script:
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$img_path."')");

You have to add enctype='multipart/form-data' to your form for file uploads to work.
<form action="ad_cont.php" method="POST" class="add_contact" enctype="multipart/form-data" name="add_contact">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
The change php code like this.
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
$img_name= $_FILES['uploaded_file']['tmp_name'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$img_name."','".$img_path."')");
Try changing the code of your form like this and hope it will work then.

To upload file to server, you need to mention enctype='multipart/form-data' in the <form>
<form enctype="multipart/form-data" action="ad_cont.php" method="POST" class="add_contact" name="add_contact" >
..
..
</form>

Related

PHP input file error, echo file name with Post method

I'm trying to take posted input file name, I'm not uploading anywhere.
I just need the name of posted filename so I'm trying this code;
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES['file'];
}
?>
If I change enctype="multipart/form-data" into form tag, it's ok, but I need this tag.
You still need the enctype attribute, as the files will not be available without it.
if (isset($_POST['submit'])) {
echo $_FILES['file']['name'];
}
use
echo $_FILES['file']['name'];
instead of
echo $_FILES['file'];
$_FILES['file'] contains array of properties of uploaded file. use print_r instead. It will work fine.
you can get file name like that
$name = $_FILES['file']['name'];
this code is working fine
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_FILES['file']) ;
}
?>

PHP 'Upload File' code not working

HTML in 'index.php':
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf, .pdf" required><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
PHP in 'index.php':
<?php
if (isset($_POST['submit'])) {
$target_dir = 'docs/';
$temp_loc = $_FILES['fileToUpload']['tmp_name'];
$file = $_FILES['fileToUpload']["name"];
if(move_uploaded_file($temp_loc,$target_dir.$file)) {
?><script>alert('successfully uploaded');</script><?php
} else {
?><script>alert('error while uploading file');</script><?php
}
}
?>
The following code doesn't seem to upload any of the documents I select to the target dircetory, that is, '/docs'.
I'm using wampserver with localhost, if that is needed at all. The 'file-upload' is allowed in the 'php.ini'
I've seen many tutorials and have even done troubleshooting on Stackoverflow itself, but I can't seem to make it work.
Thanks in advance

Php upload image to directory not working

I know there's questions on this issue but I'm trying to get images uploaded to a directory using move_uploaded_file. I'm using the example from the php manual http://www.php.net/manual/en/function.move-uploaded-file.php but I'm getting an Undefined index: file. File is the name of my file input type so I'm not sure why it's undefined.
Here is my form:
<form action="MoveImages.php" class="dropzone" id="my-awesome-dropzone" method="post">
<input type="file" name="file" />
<input TYPE="submit" name="upload" title="Add Images" value="Add Photo"/>
</form>
Here's my PHP file:
<?php
$uploads_dir = '/Users/Jane/Desktop/NE';
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
Can anyone see where I am going wrong?
Add enctype="multipart/form-data" in your form tag.
That attribute is required when you are uploading file.

PHP uploaded file name doesnt show up in browser

All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>

Save image as session and display it

I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable $ _FILES
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>

Categories