I have a simple statement below:
if(file_exists('/images/alum/'.$profile['pic']))
echo '/images/alum/'.$profile['pic'];
else echo '/images/user_default.jpg';
The file is there but it's always going to the default image. What have I got wrong?
You are saying on the server that the file at the root of the file system exists. You will have to probably add some . or ..
Try:
if(file_exists('./images/alum/'.$profile['pic']))
echo '/images/alum/'.$profile['pic'];
else echo '/images/user_default.jpg';
i.e. changing the "/images" to "./images" but only in the file_exists call.
Change it to this.
if(file_exists('./images/alum/'.$profile['pic']))
echo './images/alum/'.$profile['pic'];
else
echo './images/user_default.jpg';
make sure that $profile['pic'] has valid file name, contraction with path is valid, and current directory with parth point to file...
temporary negate condition to see file from profile...
Related
I have to include a file stored in a database and retrieved as a variable.
how can I do it? I have tried this line but it doesn't work
include_once("News/$post->newspage");
the fild newspage contains the names of the files stored in News folder. for example his line works.
include_once("News/labourday2014.php");
you can try like
include_once("News/".$post->newspage);
include_once("News/{$post->newspage}"); should do the trick :)
Verify if the file exist then include it
if(is_file("News/".$post->newspage)) {
include_once("News/".$post->newspage);
} else {
// File dosent't exist
echo "File dosent't exist";
}
You can try this -
$fileName="News/".$post->newspage;
include_once($fileName);
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'm just having some trouble working with $_FILES and file_get_contents.
$nomFichier = $_FILES['pieceJointe']['name'];
echo is_readable($_FILES['pieceJointe']['tmp_name']);
echo $_FILES['pieceJointe']['error'];
if(file_get_contents($_FILES['pieceJointe']['tmp_name']) == false)
{
echo "impossible de lire le fichier.<br/>";
}
else
{
// store the file into a BLOB field in my database
}
is_readable display "1", $_FILES['pieceJointe']['error'] says 0.
But file_get_contents return false.
I notice that happen only with files which the name contains accents.
Everything is working fine with only letters/numbers file names.
Do I missed something ?
ps: I'm french student, not professional, sorry for my english :o
Thx!
Use var_dump to see the actual value of is_readable(). The "1" that you see is actually the output of $_FILES['pieceJointe']['error'] which means the file you are trying to upload is too large. See http://www.php.net/manual/en/features.file-upload.errors.php.
I guess that there is some conflict of file_get_contents function and open_basedir directive of your php.ini file:
http://www.php.net/manual/en/ini.core.php#ini.open-basedir
Try to use move_uploaded_file function before you read the file.
It will move uploaded file from temporary directory of your server to specified directory, (open_basedir directive only effects the destination parameter of move_uploaded_file)
Isn't $_FILES['pieceJointe']['tmp_name'] just a file name name, instead of a valid full path?
Maybe move the file first with move_uploaded_file before opening it.
<?php
if(file_exists(dirname(__FILE__).'\<<<<<<<'))
echo "YES";
else
echo "No";
?>
My server is Windows 2003
When I test on my server , the response is YES.
But we all know the file name : "<<<<<<<" is invalid ,SO the file named "<<<<<<<" does not exist
So what is the reason ?
when i echo dirname(FILE).'\<<<<<<<';
The output is C:\website\<<<<<<<
i think you should check your server file manager manual or windows server guidelines (forum)..
because in any Windows XP/7/8 try to make folder with name "con" or "LPT1",this will not allow to generate...so its called "EASTER Egg" problem... and in your case i think this problem is something like easter egg.
are you sure your file name is Correct "<<<<<<<"
if yes then try to echo the whole path first
- $filename = dirname(FILE) . '\<<<<<<<';
echo $filename; // see is it returning you the correct path
$myfilepath = SITEROOT."/uploads/vaibhav_photo/thumbnail/".$user_avatar['thumbnail'];
if(file_exists($myfilepath))
{
echo "file exist";
}
else
{
echo "file does not exist";
}
It always goes to else part even though file is present.
if anybody have an alternate option for this in PHP please reply as fast as possible,
file_exists works on file paths only. http:// URLs are not supported.
$myfilepath = SITEROOT.DS.'uploads'.DS.'vaibhav_photo'.DS.'thumbnail'.DS.$user_avatar['thumbnail'];
NOTE::SITEROOT have actual root path and DS is constant DIRECTORY_SEPARATOR.
Pekka is correct that file_exists does not support http protocol.
You can however use file_get_contents
if(file_get_contents($myfilepath)) {
echo "file exist";
}
By default this pulls back the entire contents of the file. If this is not what you want you can optimise this by adding some flags:
if(file_get_contents($myfilepath, false, null, 0, 1)) {
echo "file exist";
}
This syntax will return the first character if it exists.
Check what your working directory is with getcwd().
Your path
example.com/uploads/etc/etc.jpg
is interpreted relative to the current directory. That's likely to be something like
/var/www/example.com
so you ask file_exists() about a file named
/var/www/example.com/example.com/uploads/etc/etc.jpg
You need to either figure out the correct absolute path (add the path containing all site roots in front of SITEROOT) or the correct relative path (relative to the directory your script is in, without a leading /).
tl;dr: try
$myfilepath = 'uploads/etc/etc.jpg';