Please help me to solve the problem. i'm trying to to upload the image with extension jpg using move_uploaded_file() function. my image upload code is below:
my condition is always become false please help me to solve the issue.
$file_path = '3499738f724b2ae08a1871b6a0a7d175aaaaaaaaaa.jpg';
$fileURL = 'http://demo.deftbit.com/umeedtv/prog_image/';
if(move_uploaded_file($_FILES['vdoImg']['tmp_name'], $fileURL . $file_path)){
echo 'Image Uploading Done.';
}else{
echo 'Image Uploading fail try again later.';
}
Your code does not work, because move_uploaded_file('file tmp path','local root dir path') 2nd para. is wrong. So give root path in file name like
$file_path = '$_SERVER['DOCUMENT_ROOT']'.'your folders/3499738f724b2ae08a1871b6a0a7d175aaaaaaaaaa.jpg';
Please provide directory path not the url path you have provided url path in second parameter that is wrong.
To get current directory path you can use dirname(__FILE__).
I don't think your file path is in the default directory where it is searching for. Try appending the root path to it and see.
Related
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.');
}
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.
I try to unlink an image in CodeIgniter, but the unlink function shows:
notice Undefined index: userfile
Here is my code
<?php
function get_from_post(){
$data['logo_name'] = $this->input->post('logo_name',TRUE);
$data['logo_thumb'] = $_FILES['userfile']['name'];
return $data;
}
function deleteconf($data){
$data= $this->get_from_post();
$update_id=$this->uri->segment(3);
#unlink(base_url.'image/logo_thumb'.$logo_thumb);
$query= $this->_delete($update_id);
}
?>
the unlink function shows notice Undefined index: userfile
The upload form, using multipart/form-data
Make sure you've use enctype="multipart/form-data" attribute/value for your upload form.
<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
From the MDN:
enctype
multipart/form-data: Use this value if you are using an <input>
element with the type attribute set to "file".
If you're going to use CodeIgniter form helper, you could use form_open_multipart() function:
This function is absolutely identical to the form_open() tag above
except that it adds a multipart attribute, which is necessary if you
would like to use the form to upload files with.
Deleting files, File path vs URL address
PHP unlink() function accepts the Path of the file as the first argument. Not the URL Address.
The base_url() helper function returns the URL address of the site, which you've set in the config.php file.
You'll have to use the path of the file on your server, as follows:
unlink('/path/to/image/image_name.jpg'); // This is an absolute path to the file
You could use an Absolute or Relative path, but note that the relative path is relative to the index.php file. i.e. If the image/ folder is placed beside index.php file, you should use image/image_name.jpg as the file path:
unlink('image/image_name.jpg'); // This is a relative path to the file
If you want to upload a photo for user profile and also
at the time the old user photo should be deleted by
using unlink method in codeignitor
$query = $this->db->where('profile_id',$profile_id)->get('users')->row();
$result = $query->photo;
$result = http://localhost/abc/user_photo/FE12563.jpg
if ($result) {
$dd = substr($result, strlen(base_url()));
unlink($dd);
return $this->db->set('photo',$photo)->where('profile_id',$profile_id)->update('users');
}
First check your file input name. Is it "userfile" or not?
if not then add it and then run it once again.
function get_from_post(){
$filename = $_POST['logo_name'];
$path = $_SERVER['DOCUMENT_ROOT'].'/projectname/uploads/'.$filename ;
if(is_file($path)){
unlink($path);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
base_url() function returns url of you project but here you have to use directory path of file which you want to delete.
$path = BASEPATH.'/assets/upload/employees/contracts/';
$get_file = $path.$con_id.'.jpg';
if(file_exists($get_file)){
unlink($get_file);
}
instead of unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));
First unlink function work only with path (without url), so please remove base_url() function.
Then in your first function $_FILES array doesn't contain userfile index, that's why getting error.
NOTE:- Before using unlink i would like to use also file_exists() function, first it will check if file is exist on same path then use unlink function (for error handling).
Like that -
<?php
if(file_exists(filePath))
{
unlink(filePath);
}
?>
Please fix both issue.
Thanks
Suppose you have the file name in the database and your file is abc.jpg. Then to unlink that in Code Igniter just do -
$file = "abc.jpg";
$prev_file_path = "./assets/uploads/files/".$file;
if(file_exists($prev_file_path ))
unlink($prev_file_path );
My file upload path is "public_html/assets/uploads/files/". I am writing the above code in my controller which is "public_html/application/controllers/MyController.php". So the path will be same which I used in my CI file upload section. i.e.
...
$config['upload_path'] = './assets/uploads/files';
...
So we used relative path in both the upload and unlink section. So if the upload works perfectly then this will also work perfectly.
You can do like this, you need to get first the path of directory from where you have uploaded your image.
Exemple:
//this is your url
$mainPath='https://google.com/uploads/image/16.jpeg';
$uploadedDirectory=str_replace("https://google.com/","./", $mainPath);
//now you get path like this ./uploads/image/16.jpeg
if(unlink($uploadedDirectory)) {
$this->db->where('prodId',$id);
$this->db->delete('products');
}
$this->load->helper("file");
just add above line before unlink
unlink($path);
Can you please use this code to remove image on folder.
unlink(realpath('uploads/' . $_image));
"uploads" : Image exist in uploads folder.
"$_image" : Image file name
PHP functions:
1 : unlink()
2 : realpath()
The above code successfully working on my site to delete image on folder.
try this code
unlink(base_url().'/image/logo_thumb'.$logo_thumb);
Note: You didn't assign / declare $logo_thumb in your deleteconf().
Please check your code.
I am trying to upload my php project into public server.
I made the image upload file when I create product or edit product.
It works in localhost, but when I move to public server, it is not working.
I think move_uploaded_file part does not working.
How can I change the link? or do I have to change anything?
When I see Filzilla, I can see remote site that it is '/www/eshopProject/inventory_images'.
And index file is '/www/eshopProject/storeAdmin'.
Do I have to change link like this?
I don't know how can I change the link.
Could you help me? uploading the image into public server is not working..
Is it any security issue? or something?
Please help me. Thanks.
--index.php--
$pid = mysql_insert_id();
//Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/product_$newname");
First of all check the permissions of the directory as mentioned in come of the comments.
If you have shell access "chmod 777 target_dir" or "chmod 707 target_dir" should be sufficient.
Second try to debug it using if's and the file_exists function(http://php.net/manual/en/function.file-exists.php).
Something like this.
$uploadedFile = $_FILES['fileField']['tmp_name'];
$destination = "../inventory_images/product_$newname";
if(file_exists($uploadedFile))
{
echo "file uploaded to temp dir";
}
else
{
echo "file upload failed";
exit();
}
if(move_uploaded_file($uploadedFile, $destination))
{
echo "upload complete";
}
else
{
echo "move_uploaded_file failed";
exit();
}
You can also check your current working directory by using the FILE or DIR constants(http://php.net/manual/en/language.constants.predefined.php).
Try this.
echo __FILE__;
echo dirname(__FILE__);
echo __DIR__;
Use the copy() method. For me it worked.
copy($tmp_file, Destination) or
copy($tmp_image, IMAGE_DIRECTORY . SAM . $product_image);
Make sure you have write file permissions set to the folder you are trying to upload too.
I recommend setting the folders to "755" permissions and retry. This would make the permissions a little tighter.
This question is a bit old but i recently faced a similar issue where even with permission 777 on the upload folder it wouldn't work.
The issue was that the SELinux (https://wiki.centos.org/HowTos/SELinux) was on enforcing mode so i had to change it to permissive mode and then the upload works perfectly.
I hope this can help someone facing this issue.
my code keeps on saying that the folder does not exist though it should be checked by the mkdir function..it creates the folder but does not go through the uploading process.. and displays the error on the couldnt find the folder.. is the algorithm correct? please help.. Your advice will help! :)
here is the code..
if(!(file_exists($target_path)))
{
if(!mkdir($target_path, 0777, TRUE))
{
die ("could not create the folder on mkdir");
}
//in this line the error occurs..printing what is below..//
die ("could not find folder on file exists");
}
else
{
umask($target_path);
...
}
file_exists() routine requires the complete path to file like
/var/www/uploads/file1.c
so the
file_exists($target_path);
call is ok . but second call to make directory, ie,
mkdir()
is requiring an directory , not an path to an file ie, it require /var/www/upload part only .
so you can remove the basename from path name and apply it to mkdir function()
try..
if(file_exists($target_path) && is_dir($target_path)){ //rest of the code...
}
instead of...
if(!(file_exists($target_path))){
}
Hope this will do something for you...
...............................
one thing more...
i think the problem is with if(!(file_exists($target_path))){} Statement,
THIS SHOULD BE...
if(!file_exists($target_path)){}
during the file upload process, your file saving path in move_uploaded_file()
function may be creating problem. I am saying may be because your given code is not clear enough to me. Second parameter of move_uploaded_file() is the destination where first parameter is the file name. please check the value of $target_path, it may solve your problem. thank you.