I'm having 2 codeigniter applications one of them in the root and the other is in a sub directory. I'm trying to upload files from the application in the sub sirectory the the images/artists folder in the root (in the main application) but I get
ERROR 404 Page Not Found: Images/artists
The upload destination folder does not appear to be writable.
I run this code in the controller which is used to upload in the sub directory application
if (file_exists('../images/artists')) {
echo 'directory exists - ';
if (!is_dir('../images/artists')) {
echo 'not a dir';
}
else{
echo 'it is a dir';
}
}
And I get "directory exists - it is a dir" and then run the upload code
$gallery_path = realpath(APPPATH . '../images/artists/');
$config['upload_path'] = $gallery_path;
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '20000';
$config['file_name'] = round(microtime(true) * 1000).'.jpg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('img')) {
$phot_data = $this->upload->data();
$photo_name = $phot_data['file_name'];
$this->resizeImage($photo_name);
$artist_data['image'] = $config['file_name'];
}
//not used here but dows it override and trigger the error?
function do_upload() {
if ($_FILES['photos']['name'][0] != '') {
$name_array = array();
foreach ($_FILES as $key => $value)
$_FILES['userfile']['name'] = $value['name'];
$_FILES['userfile']['type'] = $value['type'];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'];
$_FILES['userfile']['error'] = $value['error'];
$_FILES['userfile']['size'] = $value['size'];
$config['upload_path'] = realpath(APPPATH . 'images/artists/');
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
redirect('admin/gallery');
}
I checked images and artists permissions and they are set to 777 Where's the issue then?
The first snippet only checks if the directory exists, but you want to confirm that it is writable (i.e. that you can put new files into it).
Check is_writable for more details. To fix the problem you'll need to run chmod 600 /path/to/images/artists on the server.
Related
In CodeIgniter when I upload a file to a particular folder it uploads two copies of the file
To folder. I mean, it repeats the file, I do not know why.
How do I solve this problem?
thank you.
Controller:
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|svg|rar|zip' ;
$config['max_size'] = 220048;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config, 'catalogupload3');
// Create custom object for catalog upload
$this->catalogupload3->initialize($config);
$this->catalogupload3->do_upload('userfile5');
if (!$this->catalogupload3->do_upload('userfile5')){
$url_file = 'nofile' ;
$file_name = 'false';
$file_size = 'false';
}else {
$this->catalogupload3->data();
$url_file = $this->catalogupload3->data('file_name');
$file_name = $_FILES['userfile5']['name'];
$file_size = $_FILES['userfile5']['size'];
}
////
$config['upload_path'] = './files/32/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|svg|rar|zip' ;
$config['max_size'] = 220048;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config, 'catalogupload5');
// Create custom object for catalog upload
$this->catalogupload5->initialize($config);
$this->catalogupload5->do_upload('userfile7');
if (!$this->catalogupload5->do_upload('userfile7')){
$url_file = 'nofile' ;
}else {
$this->catalogupload5->data();
$url_file_32 = $this->catalogupload5->data('file_name');
}
Of course it will upload two copies because you ran the function twice:
// first run
$this->catalogupload3->do_upload('userfile5');
// second run
if (!$this->catalogupload3->do_upload('userfile5'))
If you wanna a check do it once:
// this is enough it will run it and return a bool
if (!$this->catalogupload3->do_upload('userfile5'))
This has been asked a lot but after trying a number of things, I still can't find a solution to my cause of this issue.
I'm autoloading the upload library.
My folder structure is:
/application/
/assets/
css/
images/
hero/
js/
/system/
My controller:
$fileName = $this->input->post('title') . '_hero';
$filePath = './assets/images/hero/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = true;
$this->upload->initialize($config);
if (!empty($_FILES['heroimage'])) {
$config['file_name'] = $fileName;
$config['upload_path'] = $filePath;
if (!$this->upload->do_upload('heroimage')) {
$error = array('error' => $this->upload->display_errors());
var_dump($error);
} else {
$data = array('upload_data' => $this->upload->data());
var_dump($data);
}
}
I have tried initializing both how it is now and using $this->load->library('upload', $config); but neither have worked.
I have also checked if the directory exists and if it is writable and both returned true.
Another thing I tried was setting the upload path to $config['upload_path'] = FCPATH.'assets\images\hero';
Any help pointing me in the right direction would be appreciated.
You should have config data like because the file path was not initialized
https://www.codeigniter.com/user_guide/libraries/file_uploading.html#setting-preferences
$fileName = $this->input->post('title') . '_hero';
$filePath = './assets/images/hero/';
$config['upload_path'] = $filePath;
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = true;
$this->upload->initialize($config);
if (!empty($_FILES['heroimage'])) {
if (!$this->upload->do_upload('heroimage')) {
// errors
} else {
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
}
}
I've been trying to do a multi file upload with codeigniter 1.7. I'm having issues to say the least!
I know its an old version but that's what I've got to work with.
Can I loop the $_FILES array and upload each one? It doesn't seem to work.
Heres what I've got?
$dir_name = $this->input->post('dir');
$file_path = "./system/application/views/courses/course/";
$full_file_path = $file_path . $dir_name;
if (!file_exists($full_file_path)) {
mkdir($full_file_path, 0777, true);
}
$config['upload_path'] = $full_file_path;
$config['allowed_types'] = 'php|js|html';
$this->load->library('upload');
foreach ($_FILES as $files => $fileObject) //
{
if (!empty($fileObject['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($files))
{
$errors = $this->upload->display_errors();
}
else
{
echo "It worked";
}
}
}
Is this possible?
This code runs but doesn't upload the files?!
Hope This will help
$name_array=array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
{
for ($s = 0; $s < $count; $s++)
{
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = "uploads/item_images/itemimage/original/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '8000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ($this->upload->do_upload("image"))
{
$data = $this->upload->data();
if ($this->image_moo->errors)
{
print $this->upload->display_errors();
}
else
{
$name_array[] = $data['file_name'];
} } } }
Yes, can loop through $_FILES... although you probably shouldn't name an array an "object", it's a little confusing.
Here's a user manual I found, specifically for the uploads in 1.7, it appears to be substantially similar to 3.0.3. http://www.standoffsystems.com/ci/user_guide/libraries/file_uploading.html
First, make sure your destination folder is set to 777
your config is outside the foreach() and the $this->upload->initialize($config); is inside, you don't need to initialize it each time since the $config items are, essentially, static. This isn't likely to fix your issue though.
Try this instead:
$config['upload_path'] = $full_file_path;
$config['allowed_types'] = 'php|js|html';
$this->load->library('upload', $config);
foreach (){
$this->upload->initialize($config); <-- remove this
...
}
I see you're not echoing out any errors, what happens if you do?
echo $this->upload->display_errors();
I am uploading multiple dynamic images with codeigniter with the following code.
foreach ($_FILES as $key => $value) {
$imagePrefix = time();
if (!$this->upload->do_upload($key))
{
echo $errors = $this->upload->display_errors();
return '';
}
else
{
$imagename = $imagePrefix.$value['name'];
$insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')");
}
}
When I upload an image to the specific folder this works fine; the issue is if a user uploads an image having same name, then it will automatically rename the image and upload to the folder, but what I want is to rename it by adding $imagePrefix that I have added in else part of the code and then want to upload the image with this name. But this is not working with me..
Any suggestion please?
you need to provide configuration preferences to your upload function like so:
$imagePrefix = time();
$imagename = $imagePrefix.$value['name'];
$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename; // set the name here
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)){
...
}else{
...
}
Source: http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences
Just alternative, you can use CI's rename function to rename after file uploaded.
$image = $this->upload->data();
$file_path = $image ['file_path'];
$file = $image ['full_path'];
$file_ext = $image['file_ext'];
$final_file_name = time() . $image['file_name'] . $file_ext;
only add below code and it run successfully and also set encrypt_name to false
$path = $_FILES['userfile']['name'];
$newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION);
$config['file_name'] = $newName;
$config['encrypt_name'] = false;
Now i've tried most of the fixes that i've read, most of them mention about APPPATH, base_url(), real path and etc. but i really don't know why all of them didn't work, what worked for me is that i've used the actual path, not a url but the one with the C:\xampp\htdocs.. blah blah blah.. now i've read one thread that url and directory aren't the same thing.. and the upload_path accepts only directory path and i mean the actual location of the uploads folder on the server not the URL.. now my question is how come APPPATH don't work. as what i know it the actual directory path. but when i tried to display is it return only "../applicaiton/" what really is the best path to be used on the $config['upload_path'] on the upload.php specially when deploying it to an actual server it is really a nuisance finding the directory path of your upload folder, NOTE im not using the initialize() method i'm putting my configs on config/upload.php
EDITS:
i have this on a separate file... upload.php
<?php
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
and this is my controller
if($this->upload->do_upload('image'))
{
//Did something here
}
else
{
//Did something for errors like display_errors()
}
and end result is it displays "the upload path doesn't seem to be valid"
and i've tried these line of code also
Trial 1:
$config['upload_path'] ='./uploads/';
Trial 2:
$config['upload_path'] ='uploads/';
Trial 3:
$config['upload_path'] ='/admin/assets/uploads/';
Trial 4:
$config['upload_path'] ='./admin/assets/uploads/';
Trial 5:
$config['upload_path'] ='admin/assets/uploads/';
the only thing that works is this
$config['upload_path'] ='C:\xampp\htdocs\practice\admin.abcgencon\admin\assets\uploads'';
and using the last part as a path is kinda messy so i've tried also APPPATHbut it doesn't work also it also display "../application"..
as #cryptic said i've posted this code snippet.
Question, you tried realpath and APPPATH seperately?
In Codeigniter APPPATH points to the application folder.
Example: (place your folder outside the application folder, just saying if you did not do that way) let's say the folder where we want to place the files called images.
So what you need to do is to combine realpath() and APPPATH
$image_path = realpath(APPPATH . '../images');
and pass it to your config
$config['upload_path'] = $image_path;
Create your file upload directory say uploads outside of application directory or at the root directory of CI, and set the upload path as follows:-
$config['upload_path'] = realpath(FCPATH.'uploads');
FCPATH: Path to the front controller where index.php exists (root of CI)
The above code runs on both server and local.
This is how file uploading is done in CI
$cat_image_name = $_FILES["cat_image"]["name"] ;
//file uploading params
$config['upload_path'] = './uploaded_files/categories';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $image_id."_ipad";
$config['remove_spaces'] = TRUE;
//Loading Library - File Uploading
$this->load->library('upload', $config);
//Upload the image(Ipad)
if (!empty($cat_image_name))
{
$this->upload->do_upload('cat_image');
$data = array('upload_data' => $this->upload->data());
$category_image_ipad = $data['upload_data']['file_name'];
$img_extension = $data['upload_data']['file_ext'];
}
try this
$config['upload_path'] = '../uploads/;
It's working very well for me
use it:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
file upload requires a multipart form. For this, you must have included a form helper.
goto config folder, click autoload and find the $autoload['helper'] = array(); and put 'form' :
$autoload['helper'] = array('form');
for controller:
$config = array(
'upload_path' => "./assets/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->user_model->create_photo($post_image);
redirect('');
}
for model:
public function create_photo($post_image){
$data = array(
'pri' => $this->input->post('price'),
'descrip' => $this->input->post('title'),
'post_image' => $post_image
);
return $this->db->insert('table_name', $data);
}
Tested on Windows & Linux:
$path = realpath(".");
$path .= "/uploads/profile_images";
Here uploads is the uploads directory on root.
FCPATH: front controller path where index.php exists or root folder
APPPATH: application folder
Create a uploads folder under root folder:
$config['upload_path'] = realpath(FCPATH.'uploads');
Create a uploads folder under application folder:
$config['upload_path'] = realpath(APPPATH.'uploads');
In case: If you have created the uploads folder outside of root folder:
$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads');
It is very simple:
Just copy your all $config in new php file called upload.php and put it in cofig folder.
You path will work ok.
Here is upload.php file contents:
<?php
$config['upload_path'] = site_url().'uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_ext_tolower'] = TRUE;
$config['overwrite'] = FALSE;
$config['max_filename'] = 0;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['detect_mime'] = TRUE;
$config['mod_mime_fix'] = TRUE;
?>
But please keep $config data in upload_file controller as well.For example:
public function do_upload(){
$config['upload_path'] = 'uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_ext_tolower'] = TRUE;
$config['overwrite'] = FALSE;
$config['max_filename'] = 0;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['detect_mime'] = TRUE;
$config['mod_mime_fix'] = TRUE;
//Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
It is simple...