PHP - accessing a form-submitted file for validation - php

I've been doing form validation using $_POST for text entries. Now I have to validate uploaded files through the HTML type='file' forms. I'm having trouble accessing them through $_POST or $_FILES. Is there a different way to do this? I need to get the file so that I can check the MIME type using getimagesize() or mime_content_type()
Here are the relevant bits from my form:
<form action='submission.php' method='POST'>
<p>Upload photo of professor</p>
<input type="file" name="pic" accept="image/*>
<p>Upload video of professor</p>
<input type="file" name="video" accept="video/*">
<p><input type="submit" onclick="validate()"></p>
</form>
submission.php is supposed to call on a validation function to check that the MIME type is image, if I can get the file.
function validateimage(&$errors, $field_list, $field_name, $pattern) {
if (isset($field_list[$field_name])) {
if (!preg_match($pattern, mime_content_type($field_list[$field_name]))) {
$errors[$field_name] = 'Please upload an image file';
}
} else {
$errors[$field_name] = 'Required field';
}
}
After adding enctype="multipart/form-data" to my field and setting $field_list to $_FILES it almost works. However, it appears isset($_FILES['pic']) is always true, as when I try to submit with no upload, I get this warning:
validateimage($errors, $_FILES, 'pic', '/^image/i');
Warning: mime_content_type(): Empty filename or path in /Applications/XAMPP/xamppfiles/htdocs/cs4ww3/Assignment 3/validate.inc.php on line 36

In order for $_FILES to be populated with the submitted files, in your html you need to set enctype attr on your form to multipart/form-data:
<form action='submission.php' method='POST' enctype="multipart/form-data">
...
</form>

Related

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>

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.

Codeigniter form validation for file

I'm using codeigniter for my project and I need to read contents of files. So,I need validation to check whether the file is selected or not.
Here is my code in controller
$this->form_validation->set_rules(
'estimation_file',
'Project Estimation File',
'required'
);
But while choosing a file it shows error saying - The Project
Estimation File field is required
In codeigniter you cannot check the validation of two dimension array or file field with form_validation, instead you can check it after posting the data.
$this->form_validation->set_rules('validation_check','any required field','required');
if($this->form_validation->run()==FALSE)
{
// your code before posting...
}
else
{
// check the file posting
if($_FILES['estimation_file']['name']!='')
{
// if file selected or not empty
}
else
{
// if file not selected | empty | redirect
}
}
do not forget to write enctype="multipart/form-data" within the form field, otherwise your file field will not pass the value of two dimension array.
<form method="post" enctype="multipart/form-data" name="upload_form" action="">
<input type="hidden" name="validation_check" value="TRUE" />
<input type="file" name="estimation_file" value="" />
<input type="submit" value="Post" />
</form>

checking if the file upload is chosen or not in codeigniter

I have a form with input type file and its not compulsory to upload . But after the submit action occurs I want to check in the controller section if the file input is empty or not. I have a following view page and controller but the method is not working for me .
This is the form fillup page
<form enctype="multipart/form-data" method="post"
action="<?php echo site_url('welcome/do_upload');?>">
username:<input type="text" name="username">
<p>upload file</p>
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>
and this is the controller where am trying to check if the file is selected to upload or not .
if(isset($_FILES['image'])){
echo( "image selected");
} else {
echo("image not selected");
}
the output of the result is always the "image selected" though I don't select any image
The error is when you submit the FORM
$_FILES['image'] will always be created no matter what thus making your conditions true.
You are checking for the wrong conditions here, what you want to do is when a user uploads a file. You might want to do is to check if the $_FILES['image']['name'] length is > 0 or the $_FILES['image']['size'] is > 0.
example, code not for production, just for testing.
if ( strlen($_FILES['image']['name']) > 0){
//yeahhhh!
}

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