Uploaded file not available for processing? - php

Problem:
When I choose the file and submit the form, it goes to the processing page, but apparently doesn't send the file array, just the name of the file.
Goal:
Accept and upload file to appropriate directory.
Environment:
Local
OSX / MAMP Pro
PHP / Laravel 4
Upload Controller:
public function uploadLogo() {
$locals['route'] = route('saveProfileLogo');
return View::make('upload_file', $locals);
}
Upload View:
<form method="get" action="{{{$route}}}" enctype="multipart/form-data" class="grid-form">
<label>File</label>
<input type="file" name="fresh_file" />
<input type="submit" class="btn btn-primary" value="Start Upload" />
</form>
Save Controller:
public function saveLogo() {
// Always fails
// if (!Input::hasFile('fresh_file'))
// return 'No file provided.';
// Always shows: array(1) { ["fresh_file"]=> string(21) "Angular Mountains.jpg" }
// dd(Input::all());
Input::file('fresh_file')->move(public_path() . '/files');
return View::make('upload_file_response');
}

You should use the method="post" in the form, not get, that's why you see the file as a string.

Related

Trying to upload a file in laravel but it doesn't seem to exist

Hi I'm new to laravel and was trying to upload a .csv file.
My code looks like this:
CLIENT_SIDE (BLADE)
<form role='form' action="{{route('webscrape.upload')}}" method ="post" enctype="multipart/form-data" data-parsley-validate="">
{{ csrf_field() }}
<input type='file' name="data" required="">
<br>
<input type='submit' name='submit' value='Import' class="btn btn-info btn-lg" onclick="return confirm('Are you sure you want to submit these information?')">
</form>
SERVER_SIDE (CONTROLLER)
public function uploadFile(Request $request){
if ($request->input('submit') != null ){
$file = $request->file('data');
if (file_exists('data')) {
dd('hello');
} else {
dd('hi');
}
}
}
I keep getting hi even when I upload a file. I already checked the routes to make sure the names and links are fine. Any advice would be great. Thank You
From what i perceive, you are using file_exists() to check a string instead of the actual file path and in this case it will never work.
My advice for you:
You should pass the actual path of the file that you want to check for. In order to do that, you can write something like this:
$file = $request->file('data');
$path = $file->getPathname();
if (file_exists($path)) {
echo 'hello';
} else {
echo 'hi';
}

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!
}

Can't upload files with CodeIgniter 2.1.0

I'm having problems uploading files with CodeIgniter 2.1.0, as I recieve the $_FILES array empty.
This is the form:
<form enctype="multipart/form-data" action="<?= base_url()?>nicUpload/test" method="POST">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
The action in the rendered form takes the value: http://localhost/nicUpload/test.
This is the controller:
<?php
class NicUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function test() {
echo count($_FILES);
}
}
?>
The result is 0, I would expect 1.
I tried doing the same without CodeIgniter:
index.php:
<!doctype html>
<html>
<head></head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
</html>
upload.php:
<?php
echo count($_FILES);
?>
and I get the expected result (1). So it's not a php configuration problem.
** UPDATE **
I should have said it earlier, but if I use CodeIgniter's Upload class it fails in this lines of CI's system/libraries/Upload.php:
// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]))
{
$this->set_error('upload_no_file_selected');
return FALSE;
}
as $_FILES is empty.
Ok, thanks to Sena I found the problem. I was using the method described here to use i18n, so when I was uploading to http://localhost/nicUpload/test I was being redirected to http://localhost/spa/nicUpload/test, in that redirections the information in $_FILES was getting lost. So I just had to add nicUpload to the $special array in MY_Lang.php:
private $special = array (
"admin",
"auth",
"nicUpload"
);
That fixed it, once $_FILES had the proper information I could use the proper method to upload files (the method that Sena mentioned).

html forms - sending file to php with method post results in blank page

I have the following form:
<form name="uploadForm" action="proxy.php" method="POST" enctype="multipart/form-data">
<input id="fileToUpload" name="fileInput" type="file"/>
<input type="submit" name="uploadButton" value="Upload"/>
</form>
The php works as a proxy and is OK (i do have to change the POST method to PUT in the proxy).
When upload is finished, the page turns blank and the path i see in the browser is the path to the php.
What am i doing wrong?
after uploading the files in proxy.php redirect to the form page
//add single line at last
header("Location:form.php");
Another way that i like is to post data in the same file:
<form name="uploadForm" action="?action=upload" method="POST" enctype="multipart/form-data">
<input id="fileToUpload" name="fileInput" type="file"/>
<input type="submit" name="uploadButton" value="Upload"/>
</form>
at the beginning of your file that contains upload form, add this:
<?php
$uploadComplete = false;
if(isset($_GET["action"]) && $_GET["action"]=="upload")
{
// put upload codes that you have in proxy.php
$uploadComplete = true; // you can even set this variable to check if upload is done or not
}
?>

Categories