Php upload image to directory not working - php

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.

Related

HTML not posting file to PHP

Here is my code:
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_POST["myFile"];
echo("hello");
}
?>
</body>
</html>
when I run it and input a file it says: Undefined array key "myFile"
what am I doing wrong?
Uploaded files don't get added to the $_POST array but they are loaded into the $_FILES array. Try check that out.
You can then use the move_uploaded_file() to save it wherever you want.
Important: as soon as the script execution is over, the temporary uploaded file is removed if it was not saved somewhere else with move_uploaded_file().
The request transfers the data using the names of the form fields and not the IDs, the file data can be found in the global variable $_FILES.
Use $file = $_FILES['filename']['name']; for the name of the uploaded file
or $file = $_FILES['filename']['tmp_name']; for the temporary file path of the uploaded file.
Add on form enctype="multipart/form-data"
This value is necessary if the user will upload a file through the form.
Your file type name is "filename".
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_FILES['filename']['tmp_name'];
echo("hello");
}
?>
</body>
</html>

Saving image with PHP on localhost uploaded with a form

i'm trying to save an image uploaded with a HTML form on my localhost server (working with Xampp), but although there are no errors, the file isn't saved anywhere.
This is the form, really simple:
<form id="form1" action="result.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="imguploaded" id="imguploaded" accept=".png, .jpg, .jpeg"></br>
<input class="btn btn-outline-danger btn-lg" id="inputbtn" type="submit" value="Upload Image" name="submit">
</form>
and this is the PHP code (on result.php):
<?php
$result=false;
$error=false;
if (isset($_FILES['imguploaded'])){
$nomefile = strtolower($_FILES['imguploaded']['name']);
$path = "caricamenti/$nomefile";
move_uploaded_file($_FILES['imguploaded']['name'], $path);
echo($path);
}
?>
the path exists and it is in the same folder of the .PHP files.
In move_uploaded_file($_FILES['imguploaded']['name'], $path); the first parameter isn't correct. It should be the temporary path where php stored it intermediatly, which you find in $_FILE['imguploaded']['tmp_name'].
So change that line to
move_uploaded_file($_FILES['imguploaded']['tmp_name'], $path);
relevant docs
Be sure to:
sanitize filename & extention first
check for allowed mimetypes, size, ..
Right now I could easily upload a php script and execute that.
Try this:
$file_name= $_FILES['imguploaded']['name'];
$file_tmp_name= $_FILES['imguploaded']['tmp_name'];
$div= explode('.',$file_name );
$file_txt= strtolower(end($div));
$unique_image= substr(md5(time()),0,10).".".$file_txt;
$uploaded_image= "caricamenti/".$unique_image;
move_uploaded_file($file_tmp_name,$uploaded_image);

Upload file to server and return name of file

I'm trying to upload a file to my php server, then return the name of the file to display in the html document. But I get the following
`error: Objektet wasn't found! The requested address was not found on this server. The link on the previous page appears to be incorrect or out of date Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.8`
My html Doc
<html>
<body>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
My php doc
<?php
header('Content-type: text/plain');
if(isset($_POST["fileToUpload"])){
$file = $_FILES["fileToUpload"];
echo("File: ".$file);
}
?>
You have many errors in PHP
<?php
if(isset($_FILES["fileToUpload"])){
$file = $_FILES["fileToUpload"]["name"];
echo "File: ".$file;
}
?>
HTML
<html>
<body>
<form method="post" enctype="multipart/form-data" action="server.php">
<input type="file" name="fileToUpload" id="fileToUpload" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
Errors
1.if(isset($_POST["file"])){ its not post it should be $_FILES["fileToUpload"]) since its a file upload
$file = $_FILES["file"]; and in your html you have defined file name as fileToUpload but your accessign unknown name so it should be $file = $_FILES["fileToUpload"]["name"];
In your PHP script you are asking for a form name that does not exist. In your form, the variable is called fileToUpload but in your script you are checking for $_POST['file'].
Also, the global $_FILES is an array with information about the file, so you cannot use echo to show its content. Use echo $_FILES['fileToUpload']['name'] since $_FILES['formFieldName']['name'] will display the original name of the file on the client machine.

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

Upload image to database Undefined index

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>

Categories