Yii: CUploadFile save as not working properly - php

I am trying to upload pdf file using Yii's CUploadFile. In my Controller:
$model->recipe_file_url = CUploadedFile::getInstance($model, 'recipe_file_url');
$file = $model->recipe_file_url;
$model->recipe_file_url->saveAs('images\uploads\\' . $file);
It is supposed to save the file in uploads directory under images directory, but it uploads it in the root directory with this name images\uploads\$file. How to solve this? any help?

Well, i got it myself. It was simple, I tried:
$model->recipe_file_url->saveAs('images/uploads/' . $file);
and it worked!

Related

wordpress remove uploaded file

I have created a simple script to upload file in my WordPress plugin using
wp_handle_upload
In database only link to this image is stored. I would like to delete this uploaded file when i delete the post which it is linked to, however using
unlink()
does not work due to link structure which looks like this:
http://localhost/wp-content/uploads/2016/10/image.jpg
Does Anyone know the way to remove "http://[ip]/" from path or any WordPress method to remove uploaded file
I would be grateful for help.
You can use get_home_path() to get the root directory. Then your code would be:
$url = 'http://localhost/wp-content/uploads/2016/10/image.jpg';
$path = parse_url($url, PHP_URL_PATH); // Remove "http://localhost"
$fullPath = get_home_path() . $path;
unlink($fullPath);

php move_uploaded_file not creating file

I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}

Codeigniter cannot unlink file created by codeigniter

I hope someone can help me with this.
I am creating a text file from the query results from a mysql DB. I then set the file to auto download. Once that is done I am trying to unlink the file. It fails to remove the file from the server. When I go to the location and manually try to delete the file it states that it is write protected.
I do not have root access to this system so I can not change the permissions on said file.
Here is the code, is there a way to not create a write protected file?
$leagueinfo = $this->livedraft_win_model->get_leagueinfo($sport, $leagueid);
$export = $this->my_model->get_export($sport, $leagueid);
$file_name = $leagueinfo['strat_id'] . '.IOD';
$export=strip_quotes($export);
$export = str_replace(", ",",",$export);
write_file('/tmp/' . $file_name, $export,'x+');
$data = file_get_contents('/tmp/' . $file_name, FILE_BINARY);
ob_clean();
force_download($file_name, $data);
array_map('unlink', glob("/tmp/*.IOD"));
I use the same unlink format to remove files that are uploaded to the same location and that works just fine. It is only when I try to remove the files that are created by codeigniter.
Thanks
Please try with below code.
$this->load->helper("url");
unlink(base_url('/tmp/' . $file_name));
Or try this
delete_files('/tmp/' . $file_name);
For more information please read this doc.
https://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

Cannot unlink file in Codeigniter

In my project I have a folder secure in root. The project package looks like:
application
secure
system
...........
Inside secure folder I am uploading some images on form submit using
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'];
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('thumb_image');
and it is working properly. Now while on editing the details, using another form, if I am uploading a new image instead of the current image file, I want to unlink the current one and then upload new file.
For this I am using the code:
unlink(base_url("secure/".$data['row']->videothumbnail));
I also tried with
unlink('/secure/'.$data['row']->videothumbnail);
where $data['row']->videothumbnail) is the current image file from database. New file is successfully uploaded. But old file is not getting unlinked. I have set the permission of secure folder to 777. But the images are uploaded with read only permission. Is it because of this, it is not getting unlinked?
Can anyone help me to solve this?
Thanks in advance.
Try this:
Set the permission dynamically using:
#chmod('./secure/'.$data['row']->videothumbnail, 0777);
then try unlink:
#unlink('./secure/'.$data['row']->videothumbnail);
Try echoing the path that you are providing to unlink function.
It should be something like this:
base_url()."secure/".$data['row']->videothumbnail;
I also had this issue even after setting the right permission on the folder. But the following code worked for me.
unlink(realpath(APPPATH . '../uploads').'/'.$ImageName);
Try to use $_SERVER['DOCUMENT_ROOT'] instead of base_url
$this->load->helper("file")
unlink(base_url('folder/file.ext'));
location:
\app\controller
\system\libraries
**folder\file.ext**
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
unlink($unlinkUrl);
}
else{
echo $unlinkUrl." is not available";
}
I think you are just making a stupid mistake.
Firstly, the first param of unlink should be a relative path or absolute path, but base_url function will return you a path contains domain name, HOW CAN YOU DELETE A FILE ON REMOTE SERVER ?
Secondly, '/secure/'.$data['row']->videothumbnail here is not a relative path but a absolute path
YOU MUST change it into /the/absolute/path/to/secure/ or ./the/relative/path/to/secure/ (DO NOT MISS THE DOT)
use this to unlink
$oldthumb = "secure/".$data['row']->videothumbnail;
#unlink($oldthumb);
First Load the $this->load->helper("file") and then unlink it
unlink("secure/".$data['row']->videothumbnail);
if ($rowAffected > 0) {
if ($isMediaUpload)
if (file_exists('./uploads/' . $this->input->post('img_url')))
unlink('./uploads/' . $this->input->post('img_url'));
redirect('/admin/configration', 'location');
}
Though i came in late but someone might need this .
unlink(FCPATH."secure/".$data['row']->videothumbnail)
**FCPATH** - path to front controller, usually index.php
**APPPATH** - path to application folder
**BASEPATH** - path to system folder.

Create destination folder on file upload with same name as filename

I've been looking at various PHP/AJAX file upload plugins online, but I'm having some trouble finding one feature I really need. For the purposes of this upload, each file that I upload must go into a directory with the same name as that file (minus the extension). Naturally, the nicest way would be to create that folder during the upload and then send the file to it. I know this involves mkdir() in some way, and I've found a number of scripts that even perform basic folder creation, but I'm not clear on how to do this dynamically using the filename. Any ideas?
Thanks!
When you upload a file in php it is stored in the $_FILES array, its name is stored in $_FILES['inputfield']['name'] where 'inputfield' is the name in the file input like:
<input type='file' name='inputfield' />
So then you would do:
$exp = explode(".",$_FILES['inputfield']['name']);
$filename = $exp[0];
$path = "/path/to/base/folder/" . $filename . "/" . $_FILES['inputfield']['name'];
move_uploaded_file($_FILES['inputfield']['tmp_name'], $path);
$fileName = $_FILES['fieldname']['name']
$foldername = substr($fileName, 0, strrpos($fileName, '.'));

Categories