Using HTML POST to upload file via PHP - php

I am trying to upload a local file to webserver using HTML POST method and PHP.
this is my php code:
<?php
if (isset($_POST["submit"])) {
$updir = "/var/tmp/";
$upfile = $updir.basename($_FILES['rawexcel']['name']);
if(is_uploaded_file ($_FILES ["rawexcel"]["tmp_name"]))
{
move_uploaded_file ($_FILES["rawexcel"]["tmp_name"], $upfile);
} else {echo "error uploading file ".$upfile;}
} else {echo "not isset post method";}
?>
and HTML code is:
<div class="container" id="upl">
<h4> Upload files</h4>
<form action="upl.php" enctype="mutipart/form-data" method="post">
<p> upload your files to DB</p>
<p><input type="file" name="rawexcel" id ="rawexcel">
<input type ="submit" value="Upload" name ="submit"></p>
</form>
</div>
$_FILES["rawexcel"]["error"] shows 0 and from running this peice of code i get
error uploading file /var/tmp
I guess file name was not retrieved from html?

Error is in enctype:
enctype="multipart/form-data"
not:
enctype="mutipart/form-data"

You have typo mistake in enctype="multipart/form-data" , instead of this you typed enctype="mutipart/form-data" . So "mutipart" spelling is need to correct.

Related

Php Uploader cannot be uploaded on xampp

I have just started to code in PHP and i have a problem with the code of the uploader: I use xampp and when I upload the file the result is
File uploaded in C:xampp\htdocs\myfile.php
but the file does not exist.
Code Snippet below:
<form action="sys.php" method="POST" ectype="multipart/form-data">
<c size=6>FILE TO UPLOAD</c><br>
<input type="file" name="sys"><br><br>
<input type="submit" value="UPLOAD" name="upload"><br><br>
<?php
$path = "C:xampp\htdocs";
if (isset($_POST["upload"])){
move_uploaded_file($_FILES["sys"]["tmp_name"], $path);
echo "File Uploaded in $path\YOURFILE";
}
else{
echo "[-]ERROR the uploader doesn't work";
}
?>
</html>
change double quote to single and also set name for uploaded image.example code are added below . Please put this inside if (isset($_POST["upload"])) condition
$path = 'C:\xampp\htdocs\\'.$_FILES["sys"]["name"];

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.

move_uploaded_file not moving file with no error - searched other questions

I have searched all the questions of a similar nature yet have not been able to find the solution. I have checked my php.ini file and all permission and sizes are appropriate. The file is being created in mysql but I can not get a copy moved to my local folder. I have tried copy as well as setting permission with the commented out code seen below. Again, no errors are thrown that I can find.
Thank you. If you see the answer in another post feel free to supply a link.
<?php require_once("include/DB.php");?>
<?php require_once("include/Sessions.php");?>
<?php require_once("include/Functions.php");?>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_file = ($_FILES["Image"]["name"]);
$Target =__DIR__."\Uploads\\".basename($_FILES["Image"]["name"]);
$Query="INSERT INTO admin_panel (image)
VALUES('$target_file')";
$Execute=mysqli_query($Connection, $Query);
chmod($target_dir,0755);
if(move_uploaded_file($_FILES['Image'],['tmp_name'], $Target)){
$_SESSION["SuccessMessage"]="uploaded ". $Target;
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="not uploaded ". $Target;
Redirect_to("testImage2.php");
}
if($Execute){
$_SESSION["SuccessMessage"]="Post Added Successfully";
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="Something went wrong failed to add";
Redirect_to("testImage2.php");
}
}
?>
<!DOCTYPE html>
<html>
<body>
<?php echo Message();
echo SuccessMessage();
?>
<form action="testImage2.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="Image" id="Image">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

php - Upload .dat file to be stored in json

I am trying to upload .dat file, I want to get the content inside the file and have it in json.
I have this code:
HTML:
<from action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload">
<button type="submit" name"btnSubmit">Upload</button>
</form>
PHP:
If(isset($_POST["btnSubmit"])) {
$file = file_get_contents($_FILES["fileUpload"]["name"], true);
$r = json_encode($file);
}
The error I get is file_get_contents("fileName.dat"): failed to open stream
I am not trying to upload the file to my server or a folder, I am trying to get the data inside it and store it into json.
A file upload will save the file in the php's defined temporary directory. From there you can either move the file to a desired location, or get the content from it. In your case you can simply get the content by using "tmp_name" instead of "name".
Future more, you have to make sure you have the enctype set in your form.
<?php
// check if file is given
if(!empty($_FILES)) {
// get file from temporary upload dir
$file = file_get_contents($_FILES["fileUpload"]["tmp_name"], true);
$r = json_encode($file);
// show restult
var_dump($r);
}
?>
<!-- add multipart -->
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload">
<button type="submit" name"btnSubmit">Upload</button>
</form>

Image upload failing in PHP

So I'm trying to set up an image upload from a form via php on my localhost and after running the code and connecting to the database okay, I'm getting the error for the upload. Since all of the other parts of the form are working after a section by section check, I'll just add the html for the upload input and its relevant php script.
<input type="file" name="image" id="image-select" />
And the portion of the php that has to deal with the image upload and verification after upload:
$image = $_FILES ['image']['name'];
$type = #getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
So error appears once the code hits the upload process. I was attempting to upload a small PNG file The relevant code I added is also in their respective orders when they appear in the script.
can you please put error here. If error means that after submit page file is not being upload. then please check your form enctype. see below an example
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>
Just a wild stab in the dark here as you haven't provided your form tag but you have remembered the enctype attribute haven't you?
Sorry I don't have 50 reputation otherwise I would ask this as a comment.
<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>
Also you should check if the file uploaded OK before calling getimagesize() (not that this would be the source of your issue)
if you are using PHP5 and Imagick, then you can try to use something like the following:
$image->readImageFile($f);

Categories