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).
Related
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>
Why move_uploaded_file function does not work I have created a form in which I will upload some audio to the server folder but "move_uploaded_file" does not move the file. I don't know where I am wrong can please anyone help me.
<html>
<head>
</head>
<body>
<form action="uploading.php" method="post">
<input type="file" name= "audioFile"/><br>
<input type="Submit" value="Upload" name="Save_audio"/>
</form>
</body>
</html>
My HTML Code
uploading.php code
<?php
if(isset($_POST['Save_audio']) && $_POST['Save_audio']=="Upload")
{
$dir='Uploads/';
$audio_path=$dir.basename($_FILES['audioFile']['name']);
if (move_uploaded_file($_FILES['audioFile']['tmp_name'], $audio_path))
{
echo 'Uploaded';
}
}
?>
Don't assume a PHP function "doesn't work". Debug. In this case your browser isn't sending the file to the server at all.
Your form element is missing the encoding:
<form action="uploading.php" method="post" enctype="multipart/form-data">
Without the enctype the default encoding is application/x-www-form-urlencoded which can't hold files.
Because you are missed enctype="multipart/form-data" in form
<form action="uploading.php" method="post" enctype="multipart/form-data">
I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>
In my cake view, I have the following form:
<form enctype="multipart/form-data" action="<?php echo $this->base; ?>/reviews/upload/" method="post" target="_blank">
<input type="file" id="file" name="file" />
<input type="submit" />
</form>
In the ReviewsController, in the upload() method there is:
public function upload() {
var_dump($this->request->data);
}
but it's always empty! I already tried "this->request", "this->data" but it's always empty.
What am I doing wrong?
Try doing it cake way,
echo $this->Form->create("Review", array("action" => "upload", "type" => "file"));
echo $this->Form->input("my-file", array("type" => "file"));
echo $this->Form->end();
In your reviews controller
public function upload() {
debug($this->request->data);
}
Using debug over var_dump and print_r as advantage that debug can be turned off in production mode application wide by setting lowering the debug level to 0 in core.php
Try changing the name of the input to somethinge else then file. Had problems with that before!
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
}
?>