I have a web page form that needs to select image and upload to a file, but for some reasons i dont know the image is not being detected. I might be missing something. The image folder is was created in the same folder as the application and system. Find the code below
View
<div id="gallery">
<div id="upload">
<?php echo $error;?>
<?php
echo form_open_multipart('index.php/favoriteMemories/pleasurable');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</div>
Controller
public function pleasurable() {
$config = array(
'upload_path' => '../images/',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '1000000',
'overwrite' => TRUE,
'remove_spaces' => TRUE,
'encrypt_name' => FALSE
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('pleasurable', $error);
} else {
$this->upload->do_upload('userfile');
$this->load->view('pleasurable');
}
}
Make sure the following,
In your controller function you don't need to use the upload code twice.
$this->upload->do_upload('userfile');
its already in the if condition you don't need to call it in else part.
Make sure you have loaded the upload library because I didn't see that in controler function, you might load it in constructor or autoload but make sure.
$this->load->library('upload', $config);
Make sure you image folder path is correct and writable
Check the image type and size you provided in the $config array.
if you see any error message please post it for more help.all the best.
i have gone through your code. there is no error in ur code. accept the upload path.
if you change it to 'upload_path' => 'image/',
it will save the image in image folder located in root. just make sure the image folder is writable. you can print the upload data and change the folder accordingly.
use print_r($this->upload->data());
to check the uploaded data
Related
I'm writing an app that lets the user upload an Excel file. It checks the file for errors, then, if no errors are found, it uploads the contents to a database. If it does find errors, the cells containing errors are colored red, then the file is saved. I then want to create a download link to this file so the user can check where they made mistakes.
The problem is that I am not sure how to create this link and where to store the file. I modify the file like this:
foreach ($badCells as $bcell) {
$sheet->getStyle($bcell)->applyFromArray(array(
'fill' => array(
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'FF4444')
)
));
}
And then save it with
$objWriter->save($dldir . $formData['upload']['name']);
$dldir is created with
$dldir = "/download/";
if (file_exists($dldir)) {
if (!is_dir($dldir)) {
unlink($dldir);
mkdir($dldir);
}
} else {
if(!is_dir($dldir)) {
mkdir($dldir);
}
}
Is this even the right way to do this? Can I store the files in any old folder or do they have go somewhere specific? How do I create the link to the specific file in the view for the user and make it accessible so they can download it?
I may be wrong .
For file upload i use this library Gargron/fileupload
The function bellow help to upload file to a specific folder and return full link of the file. You can save the link in DB
function uploadFile ($file){
$validator = new \FileUpload\Validator\Simple('5M');
$pathresolver = new \FileUpload\PathResolver\Simple($_SERVER['DOCUMENT_ROOT'].'/upload_folder');
$filesystem = new \FileUpload\FileSystem\Simple();
$fileupload = new \FileUpload\FileUpload(file, $_SERVER);
$fileupload->setPathResolver($pathresolver);
$fileupload->setFileSystem($filesystem);
$fileupload->addValidator($validator);
$md5Generator = new \FileUpload\FileNameGenerator\MD5 (true);
$fileupload->setFileNameGenerator($md5Generator);
list($files) = $fileupload->processAll();
foreach($files as $file){
if ($file->completed) {
return $_SERVER['DOCUMENT_ROOT'].'/upload_folder/'.$file->getFileName()) ;
}
}
}
Thanks for the help, I managed to figure it out over the weekend. Here's how I did
it:
$filename = join(DIRECTORY_SEPARATOR, array($dldir, $formData['upload']['name']));
$objWriter->save(str_replace(__FILE__,$filename,__FILE__));
This is how I save the file, using DIRECTORY_SEPARATOR so it will work correctly on both Windows and Linux.
return $this->redirect()->toRoute('import', ['action' => 'reject'],['query' => ['q' => 'file', 'name' => $filename]]);
In the controller, I redirect the route to the correct action and pass the file name on to it via a query URL.
public function rejectAction()
{
$filename = $this->getRequest()->getQuery('name', null);
return new ViewModel(array('filename' => $filename));
}
There, I obtain said file name through getRequest()->getQuery() and pass it on to the viewmodel.
Right-click here and choose 'Save As' to download your file.
And finally, this is how it shows the link in reject.phtml.
Downloading only works with right click and save as, I suspect I will have to write some sort of file handler to make ZF produce the correct headers for a normal left click download.
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/';
I'm trying to upload a css file user codigniter's upload class. Here is the controller:
public function uploadcssfile()
{
$config['upload_path'] = './stylesheets/stores/'.$memberid.'/';
$config['file_name'] = 'main.css';
$config['allowed_types'] = 'css';
$config['overwrite'] = true;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('filefieldname'))
{
die($this->upload->display_errors());
}
}
I just want to upload a file of type css, yet codeigniter always gives this error:
The filetype you are attempting to upload is not allowed.
I know, this is really weird but it worked for me:
in application/config/mimes.php, line 77
'css' => 'text/css',
change to:
'css' => array('text/css','text/x-c'),
To see, if your development environment has its own sense of humour and can add new mime types to any text files (my case), check your mimes like this:
if ( ! $this->upload->do_upload(-your-form-input-name-) ){
print_r($_FILES['-your-form-input-name-']['type']);
exit($this->upload->display_errors(). 'File Type: ' . $this->upload->file_type);
}
If an upload fails it will show you what mime type your server is actually getting to deal with.
PS. Now that there is an answer to your question, go here and help me :)
function upload($path){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
if($path =='profile'){
$config['upload_path'] = '../assets/uploads/avatars';
$config['file_name'] = $this->id;
}
if($path =='company'){
$config['upload_path'] = '../assets/uploads/company';
$config['file_name'] = $this->id.'_company';
}
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
if($path == 'profile'){
$this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
}
if($path == 'company'){
$this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
}
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path,
'maintain_ratio' => true,
'width' => 500,
'height' => 500
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
This is my upload function, and it works fine when $path = 'profile', but when it is company, it won't upload to the "company" folder...
Is there any reason it should be so?! I'm at a loss here...This function works when it goes to the avatar folder, but not if it goes to the company folder...
I had a similar problem with multiple folders. The way I fixed was to use to use the initialize function instead of passing the config as an argument to the load library function.
$this->upload->initialize($config);
You could load the library then set your config and call the initialize method.
Hope this helps.
I think all suggestions are really good, you can also make sure you are posting the right data in your form:
$this->upload->do_upload(); is by default expecting the form name to be 'userfile'
also I find sometimes really usefull to have some errors displayed...so instead of just $this->upload->do_upload(); try something like
if (!$this->upload->do_upload($userfile)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_error', $error);
}
and then add a file in your views called upload_error.php with the following code in it:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
</body>
</html>
good luck!
Try to unset config before setting another config like this.
The following code unset config and clear image library.
You need to clear your lib.
//Unset config for the next one
unset($config);
$this->image_lib->clear();
From CI manual...
$this->image_lib->clear()
The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.
Hmmm, Here are two suggestions:
Make sure your company folder is writable
What $this->id returns? if it's returning the file name WITH the extension then this is what causing your upload process to fail because then you'll have a file name my_image.png_company which is not an allowed type.
Please refer to the documentation page for more info.
$config = array(
'upload_path' './assets/upload/profilepic/',
'allowed_types' => 'JPG|JPEG|PNG',
'overwrite' => TRUE,
'file_name' => date('YmdHis').''.rand(0,9999)
);
its not works for JPEG file type
also i had written in small latter but it didn't works.
please Share if you have something
JPEG files are not upload Others file are inserted easely.
I've got an image uploader that doesn't upload images. I'm not receiving any errors and i've checked the folder permissions and even on 777 no images are getting uploaded.
You can see the code here: http://pastebin.com/gvH1dKh9
The value for gallery_path is /home/domain/public_html/assets/images/gallery
The value for gallery_path_url is http://domain.com/assets/images/gallery/
I have used this same code on another site with zero problems. I'm not sure what the problem is with this site?
Use
$this->load->library('upload', $config);
instead of
$this->upload->initialize($config);
In my case it was helpful
Models are intended for interaction with databases. Try moving your upload code into the controller, then if needed take the returned data ($this->upload->data();) and pass that to a model for insertion to a database.
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => '/uploads',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$this->Gallery_model->insertImageData($image_data);
}
}
Try doing some error checking on your upload:
if(! $this->upload->do_upload('Filedata')){
echo $this->upload->display_errors();
}
$upload_info = $this->upload->data();
echo var_dump($upload_info);