php - Upload .dat file to be stored in json - php

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>

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);

Using HTML POST to upload file via 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.

I have a form where I can submit text files, and i want it to add those text files to another existing form

I have an html form that is used to upload text files:
<div class="form">
<h3>Upload File:</h3>
<form action="networkSelector.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="UploadFileField"></label>
<input type="file" name="UploadFileField" id="UploadFileField" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
</div>
The php portion of the code for the form:
<?php
require('db.php');
include("auth.php");
if(isset($_FILES['UploadFileField'])){
// Creates the Variables needed to upload the file
$UploadName = $_FILES['UploadFileField']['name'];
$UploadName = mt_rand(100000, 999999).$UploadName;
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadType = $_FILES['UploadFileField']['type'];
$FileSize = $_FILES['UploadFileField']['size'];
// Removes Unwanted Spaces and characters from the files names of the files being uploaded
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
// Upload File Size Limit
if(($FileSize > 125000)){
die("Error - File too Big");
}
// Checks a File has been Selected and Uploads them into a Directory on your Server
if(!$UploadTmp){
die("No File Selected, Please Upload Again");
}else{
move_uploaded_file($UploadTmp, "C:/xampp/htdocs/meg/$UploadName");
}
}
?>
It works great and as seen in the 'move_upload_file command it puts them directly into that directory.
However what I am trying to achieve is to upload these files with this form and then to add it to another form that is on the same page.
Here is an example of my other form:
<form action="networkCompiler.php" method="POST">
<h3>Choose Network/Function:</h3>
<select id ="network" name="network" />
<option value="networkA">A</option>
<option value="networkB">B</option>
</select>
So ideally if I upload networkC on the first form, I want it to then display on the second form. I am using PHP primarily on this project and was attempting to find a solution in that language. So far I have tried saving the file upload as a variable and then adding that to the bottom of the form.
<?php
if (isset($_POST['UploadButton'])) {
if (is_uploaded_file($_FILES['UploadFileField']['tmp_name'])) {
$trying = $_POST['FileUploadForm'];
}
}
?>
Any input would be appreciated. Thank you.
Use file_get_contents():
<?php
if(your_condition)
echo '<option value="the_value_you_want">' . file_get_contents('path_of_uploaded_file') . '</option>';
?>

Using PHPExcelReader to parse file and store values in array - File not found

I am using PHPExcelReader to parse an excel file (chosen by the user from their local machine). PHP will take those values and store them in an array and then reorganize it and display it on the screen in the form of a table.
However, after I browse for a file, once I click "Submit", I get the error: File not found
Here is the HTML:
<div class="row-fluid" style="margin-top:15px">
<h2>Step 1: Import the file</h2>
<p>Once uploaded, the window will display a preview of the document. Please check to make sure the column
headers match the data before uploading.</p>
<form action="../scripts/previewFile.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" />
<input type="submit" value="Preview File" name="submit">
<div id="filePreview" style="height: 500px; overflow: scroll;">
</div>
</form>
</div>
The previewFile.php file that is called:
function previewFile()
{
$filePath = $_FILES['file']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}
UPDATE
The error was a 404 for the PHP file "previewFile.php" that is called as an action.
Not sure what's wrong with my URL "../scripts/previewFile.php"
Here's my schema:
root
|_views
|_default
|_assets
|_scripts
|_previewFile.php
|_templates
|_step1.php <- the html file with the input tags
Your input name is fileToUpload, and you are looking in the $_FILES array for file, so it should be as follows:
function previewFile()
{
$filePath = $_FILES['fileToUpload']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}

Categories