How to Get the File Path in HTML <Input Type="File"> in PHP

How to get the file path in html input type=file in PHP?

You shouldn't just use the $_GET you've got now. Your file is based in $_FILES["csv_file"]["tmp_name"].

Best you review this tutorial, that basically says you need to do something like this:

<?php
if ($_FILES["csv_file"]["error"] > 0)
{
echo "Error: " . $_FILES["csv_file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["csv_file"]["name"] . "<br />";
echo "Type: " . $_FILES["csv_file"]["type"] . "<br />";
echo "Size: " . ($_FILES["csv_file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["csv_file"]["tmp_name"];
}
?>

And you can go from there. Use move_uploaded_file if you want to move the file from the temp location, also explained in the tutorial :)

how to get file names from input type file in html 5 using javascript or jquery

well so here i am with the solution after lot of research, in case of input type file the value is stored in array as files with key name.

var files = $('input#files')[0].files;
var names = "";
$.each(files,function(i, file){
names += file.name + " ";
});
alert(names);

fiddle : http://jsfiddle.net/raj_er04/nze2B/1/

pure javascript

function getFileNames(){
var files = document.getElementById("files").files;
var names = "";
for(var i = 0; i < files.length; i++)
names += files[i].name + " ";
alert(names);
}

fiddle : http://jsfiddle.net/raj_er04/nze2B/2/

How to get the path of a selected file in input type=file using php?

$tmp_path = $_FILES['txtImage']['tmp_name'];

$dest_path = path_where_in_your_server_you_want_this_image_to_be_moved.$_FILES['textImage']['name']; (eg: 'images/'.$_FILES['name'])

if(move_uploaded_file($tmp_path,$dest_path)){ //this will move the file from tmp location in server to the destination you provide in the second parameter

$sql = "INSERT INTO tblquestions (q_category, q_question, q_image, q_correct, q_answer2, q_answer3, q_answer4) VALUES ('$_POST[txtCategory]','$_POST[txtQuestion]','$dest_path','$_POST[txtCorrect]','$_POST[txtChoice2]','$_POST[txtChoice3]','$_POST[txtChoice4]')";

}else{

echo "Image could not be uploaded"

}

Also keep in mind that there can be permission issues (with the directory that you want the image to be uploaded to) while uploading the file.

Good Luck!

Angular File Upload

Here is a working example for file upload to api:

Step 1: HTML Template (file-upload.component.html)

Define simple input tag of type file. Add a function to (change)-event for handling choosing files.

<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>

Step 2: Upload Handling in TypeScript (file-upload.component.ts)

Define a default variable for selected file.

fileToUpload: File | null = null;

Create function which you use in (change)-event of your file input tag:

handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
}

If you want to handle multifile selection, than you can iterate through this files array.

Now create file upload function by calling you file-upload.service:

uploadFileToActivity() {
this.fileUploadService.postFile(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}

Step 3: File-Upload Service (file-upload.service.ts)

By uploading a file via POST-method you should use FormData, because so you can add file to http request.

postFile(fileToUpload: File): Observable<boolean> {
const endpoint = 'your-destination-url';
const formData: FormData = new FormData();
formData.append('fileKey', fileToUpload, fileToUpload.name);
return this.httpClient
.post(endpoint, formData, { headers: yourHeadersConfig })
.map(() => { return true; })
.catch((e) => this.handleError(e));
}

So, This is very simple working example, which I use everyday in my work.

How to insert file name, title in table and upload file on server

Enable PDO Exceptions with

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION`); 

after your connect

How to allow input type=file to accept only image files?

Use the accept attribute of the input tag. To accept only PNG's, JPEG's and GIF's you can use the following code:

<label>Your Image File
<input type="file" name="myImage" accept="image/png, image/gif, image/jpeg" />
</label>

Get filename from input [type='file'] using jQuery

You have to do this on the change event of the input type file this way:

$('#select_file').click(function() {
$('#image_file').show();
$('.btn').prop('disabled', false);
$('#image_file').change(function() {
var filename = $('#image_file').val();
$('#select_file').html(filename);
});
});​


Related Topics



Leave a reply



Submit