How to check a folder contains .csv files? - php

I need to check whether a folder contains csv files or not. Using PHP
I need to show an error message if someone try to upload any csv into a specific folder if there is already existing a csv.

Use glob() to find all files matching a wildcard. It returns an array of the matches, so you can check if the array is empty.
$files = glob("$dirname/*.csv");
if (!empty($files)) {
die("Error: The directory already has a .csv file");
}

<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "Die Datei $filename existiert";
} else {
echo "Die Datei $filename existiert nicht";
}
?>
From: http://php.net/manual/de/function.file-exists.php

Loop over the files of directory to check whether it contains csv or not.
$files=scandir("/path/to/dir");
foreach($files as $fileName)
{
if(preg_match("/\.csv$/", $fileName))
{
throw new Exception("/path/to/dir contains csv file with filename: $fileName");
}
}

$dir = 'folder';
echo (count(glob("$dir/*.csv")) === 0) ? 'Empty' : 'Not empty';
You can use glob() function. http://php.net/manual/tr/function.glob.php
Also #Barmar answer is look like doing job well.

<?php
$directory = new DirectoryIterator(__DIR__);
if ($fileinfo->isFile()) {
$fileExt = $fileinfo->getExtension();
if($fileExt=="csv")
{
echo "File found";
}
else
{
echo "File not found";
}
}
?>
Reference:[http://php.net/manual/en/directoryiterator.getextension.php]

Related

scandir() display file found/not found error messge

I have 100 files and I am scanning them and picking the correct file out of them.
I am using the following code:
$dir = 'myDir';
$files1 = scandir($dir);
$scanned_directory = array_diff($files1, array('..', '.'));
foreach ($scanned_directory as $key => $value) {
$onlyname=explode(".", $value);
if($onlyname[0]== $name){
// echo "file found";
break;
}else{
//echo "<h2>Not Found. Please Try Later</h2>";
}
}
The problem with this is that if the file is the 10th file I get 9x not found, before I get the file found message.
What is the proper way to display error message if no file is found?
I simplified your code a bit.
First of all I get all files from your directory into an array with glob(). Then I simply grab all files which have the name $name with preg_grep() and check with count() if there is at least 1 file with that specific name.
<?php
$dir = "myDir";
$files = glob($dir . "/*.*");
if(count(preg_grep("/^$name\..*$/", array_map("basename", $files))) > 0)
echo "file found";
else
echo "<h2>Not Found. Please Try Later</h2>";
?>

PHP unlinking not working with variable

Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?
Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!

How do I know if my image exists on the server? (PHP)

I have a simple code that checks whether a file exists or not in the server.
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Problem: My local webserver says that the file does not exist even though when I copy paste the url in the browser, I can see the image.
How can I make the condition say that the file does not really exist?
Another option would be, making a request and checking for the response headers:
<?php
$headers = get_headers('http://www.example.com/file.jpg');
if($headers[0]=='HTTP/1.0 200 OK'){
return true; // file exists
}else{
return false; // file doesn't exists
}
?>
Code live example:
http://codepad.viper-7.com/qfnvme
for real path of image you can try:
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
$testImage = #file_get_contents($filename);
if ($testImage != NULL) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Use
$filename = $_SERVER["DOCUMENT_ROOT"]."/upload/productimg/IN.ZL.6L_sml.jpg";
for testing the file.

echo text if .php extentin exists in a folder using a php script

im trying to make a php script that if a .php extention exists in a folder it says check mail, if no .php files exist no mail either one or the other results show up not both.
<?php
$directory = "http://server1.bioprotege-inc.net/roleplay_realm_online/contact_us_files/";
if (glob($directory . "*.php") != true) {
echo 'Check Mail:';
}
else {
echo 'No mail Today';
}
?>
that what I got but it aint working it only shows the same result if there is a .php file in the folder or not
The glob() function returns the array of files name with extension so you can not check them with true and false.
Check the reference site: glob function in php
That's why you should use following code to check it:
<?php
$directory = "http://server1.bioprotege-inc.net/roleplay_realm_online/contact_us_files/";
// Open a known directory, and proceed to read its contents
if (is_dir($directory)) {
$arr = array();
if ($dh = opendir($directory)) {
while (($file = readdir($dh)) !== false) {
$arr[] = $file;
}
$name = implode($arr);
if(strstr($name,".php")){
echo "Check Mail:";
} else {
echo "No mail Today";
}
closedir($dh);
}
}
?>
This will be helpful to you. I hope so.
Understanding the risk, this is how it can be done
$dir="core/view/";
if(glob($dir . "*.php")!=null)
echo " New Mail";
else
echo "No Mail";
You can use glob if you have less than a 100k files, else you may get a Allowed memory size of XYZ bytes exhausted ..." error.
In that case you can change the setting in php.ini or you can use
readdir()
You can use readdir() in this manner
if ($handle = opendir('core/view')) {
$flg=0;
while (false !== ($entry = readdir($handle))) {
if(strcasecmp(pathinfo($entry, PATHINFO_EXTENSION),"php")==0){
$flg=1;
break;
}
}
echo $flg;
closedir($handle);
}

How do you check if a file is in a certain directory using PHP?

When accessing a file in PHP, it's possible to escape from the directory using ".." which can lead to a security risk. Is there any way to validate that a file is in a specified directory? There doesn't seem to be a built-in function for it.
This is not a secure way to check if the file exists in the expected location.. you should do the following.
$base = '/expected/path/';
$filename = realpath($filename);
if ($filename === false || strncmp($filename, $base, strlen($base)) !== 0) {
echo 'Missing file or not in the expected location';
}
else {
echo 'The file exists and is in the expected location';
}
There is a very good example on php.net
http://php.net/manual/en/function.file-exists.php
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>

Categories