php file_exists with invalid filename - php

<?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

Related

file_get_contents returning false instead of is_readable true

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.

Where does file_put_contents save the files to?

I have read the documentation and it doesn't seem to indicate where can I expect the file to be created. I assumed that If I used file_put_contents on a server then a txt file would be created in the same place where the php file running it is. What am I missing here? I'm trying to save the url's in a text file as well.I just need them on my computer really not on the server.
while ($blekr<=$blekko_count)
{
echo '<a href='.$Blekko[$blekr]['url'].'><h4>'.$Blekko[$blekr]['url_title'].'</h4></a>';
echo '<p>'.$Blekko[$blekr]['snippet'].'<p>';
echo '<b>'.$Blekko[$blekr]['engine'].'</b>';
$file = 'Blekko.txt';
file_put_contents($file, $Blekko[$blekr]['url'], FILE_APPEND);
echo '<hr>';
$blekr++;
}
Unless you specify a different directory (using one or more slashes), the file is saved in the current working directory. getcwd() returns the current working directory; chdir() changes it.

Unlink() not working

I am trying to delete a file from the Server.
The files of my application is in a folder name "/public_html/app/";
All the images associated with the application is located in the following path: "/public_html/app/images/tryimg/"
The file in which I am writing the below code spec is in "/public_html/app/".
Here is my code snipet:
<?php
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
if (file_exists($m_img_path))
{
unlink($m_img_path);
}
// See if it exists again to be sure it was removed
if (file_exists($m_img))
{
echo "Problem deleting " . $m_img_path;
}
else
{
echo "Successfully deleted " . $m_img_path;
}
?>
When the above script is executed the message "Successfully deleted try.jpg" is displayed.
But when I navigate to the folder, the file is not deleted.
Apache: 2.2.17
PHP version: 5.3.5
What am I doing wrong?
Do I have to give a relative or absolute path to the image?
you're missing a directory separator:
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
// You end up with this..
$m_img_path == 'images/tryimgtry.jpg';
You need to add a slash:
$m_img_path = "images/tryimg". DIRECTORY_SEPARATOR . $m_img;
You also need to change your second file_exists call as you're using the image name and not the path:
if (file_exists($m_img_path))
You check the wrong path:
if (file_exists($m_img))
while you (tried to) delete(d) $m_img_path, so replace your check with
if (file_exists($m_img_path))
unlink() returns a boolean value to indicate whether the deletion succeeded or not, so it is easier/better to use this value:
if (file_exists($m_img_path))
{
if(unlink($m_img_path))
{
echo "Successfully deleted " . $m_img_path;
}
else
{
echo "Problem deleting " . $m_img_path;
}
}
Furthermore, the current directory is at the location where the script is executed, so you need to keep this in mind when using a relative path. In most situations it is probably better/easier to use absolute paths if possible.
If you need paths to a lot of files on your server, you might want to put the absolute path in a variable and use that, so it is easy to change the absolute location if your server configuration changes.

file_exists in php code

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...

file_exists() does not run

$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';

Categories