Why is my upload path invalid? (Codeigniter-Library) - php

I'm creating a library as part of a project and one of the methods is a wrapper for the upload helper.
The method:
public function upload(){
echo "Doing upload";
$config['upload_path']= RESOURCE_PATH . "Downloads";
$config['allowed_types']='pdf|doc';
$config['max_size']='10000';
//echo $config['upload_path'];
$this->CI->load->library('upload',$config);
if(!$this->CI->upload->do_upload()){
echo "Couldn't do the upload";
echo $this->CI->upload->display_errors();
echo $config['upload_path'];
}
else{
echo "Could do the upload";
}
}
I've checked the directory permissions of the Downloads folder and that it exists but I'm getting the following error: "The upload path does not appear to be valid."
How do I resolve this issue?
EDIT:: I created a symlink so my directory strucutre really looks like:
www -> /home/user/Dropbox/www/appname
Note about variables used:
$this->CI = &get_instance(); // Defined in custom library class
define('RESOURCE_PATH', APPPATH . 'views/resources/'); // Defined in constants.php

Figured out the idiotic problem.
I was autoloading the library and some how when I was trying to
initialize the configuration by $this->load->library('upload',
$config); it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been
$this->upload->initialize($config);
See this answer.

Add this line
$this->CI->upload->initialize($config);
after
$this->CI->load->library('upload',$config);

Related

Upload a file to specific folder using WordPress

I need help for how to upload a file in WordPress, I don't want to upload files into the default path of media files i.e. into uploads/../.., I want to upload my files into wp-content/uploads/my_folder, I just created one file in wp-admin folder and added some functionality there, from that form I want to upload my file (not creating plugin).
Is it possible to do like this? If yes then how? If no then what I want to do for uploading file?
I tried the following solution for it:
$path_array = wp_upload_dir();
$upload_path = $path_array['baseurl'].'/myfoldername/';
$target_path = $upload_path."/".$file_name;
$file_name = $_FILES['fieldname']['name'];
$tmp_name = $_FILES["fieldname"]["tmp_name"];
upload_user_file($_FILES,$upload_path); // Called this function
In functions.php of my theme, I defined the above called function upload_user_file() like as follows:
function upload_user_file( $file = array(),$path) {
if(!empty($file))
{
$uploaded=move_uploaded_file($file['fieldname']['tmp_name'],$path.$file['fieldname']['name']);
if($uploaded)
{
echo "Uploaded successfully ";
}
else
{
echo "Some error in upload ";
print_r($file['error']);
}
}
}
Please help me for this issue.
Thanks.
It's not working because of FILES array loses its values very soon..
Upload it in the same code where you called function it will work...
Thanks cale_b its really helpful.

CodeIgniter doesn't load upload library

I'm having a problem with loading a CodeIgniter library,
I've already built three (3) websites with CodeIgniter, so I consider myself familiar with this framework.
The problem is only with the upload lib. I've tried with multiple functions and it still doesn't work.
I've even tried with the CodeIgniter example
In google I can't find any answers, does anyone have an idea what it could be?
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->library('v');
$this->load->model('admin_model');
}
public function upload_a_thing($set = null){
$this->load->helper(array('form', 'url'));
if(!$set){
$this->load->view('admin/upload_a_thing');
}else{
$this->load->library('Upload');
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
} else {
$file = $this->upload->data();
// $product = $this->admin_model->get_products($id);
$newFilePath = $config['upload_path'].'try.jpg';
$file['file_name'] = str_replace(" ","_",$file['file_name']);
rename($config['upload_path'].$file['file_name'], $newFilePath);
}
}
}
CodeIgniter Error
Undefined property: Admin::$upload
PHP error
Fatal error: Call to a member function do_upload() on a non-object
Edited for Spary
$config['upload_path'] = './media/imgs/products/';
$this->load->library('upload',$config);
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
If your form looks like this:
<input type="file" class="input" name="logo" />
then call do_upload() like this:
do_upload('logo');
If it looks like this:
<input type="file" class="input" name="userfile" />
then call it like this:
do_upload();
do something like this
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
Your code...
$this->load->library('Upload');
As per all examples in the documentation, upload should be in all lower-case and most likely the reason the library is not loading.
Additionally, if your $config array is missing, you're not following the example code in the documentation:
$config['upload_path'] = './uploads/'; // <- preferences
$this->load->library('upload', $config); // <- load library and set configuration
OR
$this->load->library('upload'); // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
OR
// 'upload' library is "auto-loaded" // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
From the chart of preferences, they all seem optional, except for upload_path...
Default Value: None
Description: The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.
I'm not sure how CodeIgniter could know where to upload the file when there's no upload_path because there is no default and your entire configuration array is missing.

$_FILES empty when uploading Magento package

I am trying to install a Magento package, but I get No file was uploaded
Its coming from this code because $_FILES is an empty array in /downloader/Maged/Controller.php
/**
* Install uploaded package
*/
public function connectInstallPackageUploadAction()
{
if (!$_FILES) {
echo "No file was uploaded";
return;
}
if(empty($_FILES['file'])) {
echo "No file was uploaded";
return;
}
$info =& $_FILES['file'];
if(0 !== intval($info['error'])) {
echo "File upload problem";
return;
}
$target = $this->_mageDir . DS . "var/" . uniqid() . $info['name'];
$res = move_uploaded_file($info['tmp_name'], $target);
if(false === $res) {
echo "Error moving uploaded file";
return;
}
$this->model('connect', true)->installUploadedPackage($target);
#unlink($target);
}
It might be worth noting that product uploads work fine.
The only log output I get is
2014-07-03T18:44:15+00:00 ERR (3): Warning: array_key_exists() expects parameter 2 to be array, null given in /var/www/vhosts/example.com/httpdocs/app/code/core/Mage/Captcha/Model/Observer.php on line 166
exception.log was empty
Make sure that your var folder in magento installation is fully writable. 777 permission. All folders and files.
You can try uploading a small dummy file first to check if the error stays the same.
There is a file upload limit which might be reached.
File upload often fails due to upload_max_filesize or post_max_size being too small as mentioned in Common Pitfalls section of the PHP documentation.
Use firebug in firefox to check if your form does has enctype="multipart/form-data".
Check the user group it was created with,
To explain, recently I had some file saving issues. Turned out I had created the folder using the Root user for the server, and the CPanel user ( the one php was running under ) didn't have permission to write in folders owned by the Root account, even when setting the permissions to 777.
Just a thought.
First check if your installation is configured properly
see#http://php.net/manual/en/features.file-upload.common-pitfalls.php
Also, if you upload with PUT/xhr the file is on the input stream
$in = fopen('php://input','r');
see#http://php.net/manual/en/features.file-upload.put-method.php and https://stackoverflow.com/a/11771857/2645347,
this would explain the empty $FILES array, in case all else is ok and the upload works via xhr/PUT.
$_FILES is an associative array of items uploaded to the current script via the HTTP POST method. All uploaded files are stored in $HTTP_POST_FILES contains the same initial information, but is not a superglobal. So, ... be sure that method is POST
Always check that your form contains correct enctype:
<form ... enctype="multipart/form-data"> ... </form>
Sometimes happens that when someone upload multiples file, $_FILES return empty. This could happen when I select files that exceed some size. The problem can be in the POST_MAX_SIZE configuration.
On
app/code/core/mage/captcha/model/observer.php
change
public function checkUserLoginBackend($observer)
{
$formId = 'backend_login';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
$loginParams = Mage::app()->getRequest()->getPost('login');
$login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
if ($captchaModel->isRequired($login)) {
if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
$captchaModel->logAttempt($login);
Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
}
}
$captchaModel->logAttempt($login);
return $this;
}
to
public function checkUserLoginBackend($observer)
{
$formId = 'backend_login';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
$login = Mage::app()->getRequest()->getPost('username');
if ($captchaModel->isRequired($login)) {
if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
$captchaModel->logAttempt($login);
Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
}
}
$captchaModel->logAttempt($login);
return $this;
}
Your issue is:
"Captcha Observer throws an error if login in RSS feed" issue #208
or if you wish you could only replace the variable $login to be like this:
$login = array_key_exists('username', array($loginParams)) ? $loginParams['username'] : null;
You may try out below points.
Use Magento Varien File Uploaded Classes to Upload the files.
Magento File Uploader
1) Check enctype="multipart/form-data" in your form.
2) Use Magento Form Key in your form.
3) Use Varien file uploader to upload your files using below links answers.

Code Igniter file upload saying path is invalid

I am creating some file upload functionality utilizing the upload library within the Code Igniter framework and I am having an issue where I keep on getting an error back stating that my file upload path is incorrect. I have checked many times and there is no issue with the file structure and the files definitely exist.
Here is my model upload code (I am dieing to check where I am within the process):
class Gallery_model extends CI_Model {
var $gallery_path;
function __construct() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images/profileImages');
//$this->gallery_path = realpath($this->config->base_url() . 'images/profileImages');
}
public function doUpload() {
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
die(print_R($data));
} else {
$error = array('error' => $this->upload->display_errors());
die(print_R($error));
}
}
}
My images folder is outside of the application and is called 'images', which contains a folder called 'profileImage' where I want this files to save. I get the following error:
'The upload path does not appear to be valid.'
I can't for the life of me figure out why this path is not working when it definitely exists. Does anyone have any ideas?
Thanks
Codeigniter will only return that error message if $config['upload_path'] is blank or the php command is_dir($config['upload_path']) returns false. Check to make sure the 'uploads' folder is in the same directory as main index.php file and that the directory name is the same case
Is your images folder in the web root? If is this should work
$this->gallery_path = './images/profileImages/';

Codeigniter -> File Upload Path | Folder Create

Currently I have the following:
$config['upload_path'] = 'this/path/location/';
What I would like to do is create the following controller but I am not sure how to attack it!!
$data['folderName'] = $this->model->functionName()->tableName;
$config['upload_path'] = 'this/'.$folderName.'/';
How would I create the $folderName? dictionary on the sever?
Jamie:
Could I do the following?
if(!file_exists($folderName))
{
$folder = mkdir('/location/'$folderName);
return $folder;
}
else
{
$config['upload_path'] = $folder;
}
am not sure what they are talking about by using the file_exist function since you need to check if its the directory ..
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
if(!is_dir($folderName))
{
mkdir($folderName,0777);
}
please note that :
i have added the permission to the folder so that you can upload files to it.
i have removed the else since its not useful here ( as #mischa noted )..
This is not correct:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Nothing will be uploaded if the folder does not exist! You have to get rid of the else clause.
$path = "this/$folderName/";
if(!file_exists($path))
{
mkdir($path);
}
// Carry on with upload
$config['upload_path'] = $path;
Not quite sure what you mean by 'dictionary', but if you're asking about how to create a variable:
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
To add/check the existence of a directory:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Not 100% sure what you want from your description so here are a few suggestions:
If you need to programmatically create a folder using PHP you can use the mkdir() function, see: http://php.net/manual/en/function.mkdir.php
Also, check out the Codeignitor file helper for reading and writing files:
http://codeigniter.com/user_guide/helpers/file_helper.html
If the folder already exists, you need to make sure the permissions are write-able. On windows you can right click on the folder, properties, security and edit there. On Linux you'll want the chmod command.

Categories