Using rename() which works but giving warning at the same time - php

OK I'm confused. This code is showing me an error but at same time is doing what I actually needed.
$dir = new DirectoryIterator('./');
foreach ($dir as $filename) {
if (mimes_content_type($filename) == 'txt') {
rename($filename, './txt/'.$filename);
}
}
Warning: rename(txt,./txt/txt.txt): Invalid argument in ./www/session/test.php on line 78
Some advices what i did wrong ?

Related

But it still deletes how to delete a directory using PHP

I have a PHP script for deleting a directory. This deletes the target directory but shows an error message if there are more than one file but it still deletes the directory..weird..;Shown error given below:-
Warning: rmdir(uploads/dd4a96d6907035a1d011b9394d779d3c) [function.rmdir]: Directory not empty in /home/.../public_html/deletepost.php on line 21
here's php
<?php
$dir = $row['album_path'];
17 foreach(scandir($dir) as $file) {
18 if ('.' === $file || '..' === $file) continue;
19 if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
20 else unlink("$dir/$file");
21 rmdir($dir);
22 }
?>
Am I doing anything wrong with the code?
Here is an example to delete folder without warning in PHP
http://en.kioskea.net/faq/793-php-warning-rmdir-directory-not-empty

php scandir() - Invalid argument supplied for foreach() [file]

I have a server running PHP Version 5.4.16 and am trying to use scandir to list files within a directory. I am having a hard time figuring out what the issue is. I've tried both ../store/dir_to_scan and /root/store/dir_to_scan. I've also tried using both glob and scandir as you can see below both to no avail. If I remove the dir_to_scan directory it will list the directories inside of /root/store which is what I find most puzzling of all. I've also chmod'd the folders and files to 777 just to make sure it wasn't a permissions issue. I receive an error of "Array ( [type] => 2 [message] => Invalid argument supplied for foreach() [file] => /root/html/test.php [line] => 5 )" also upon running with correct directory setup.
Thanks for any help.
Directory Setup
/root/html //where php script is run
/root/store/dir_to_scan //files I need to list
PHP Script
<?
#$files = glob('../store/dir_to_scan/*', GLOB_BRACE);
$files = scandir("/root/store/dir_to_scan/");
foreach($files as $file) {
//do work here
if ($file === '.' || $file === '..') {
continue;
}
echo $file . "<br>";
}
print_r(error_get_last());
?>
this might be silly but try supplying a trailing slash at the end of:
/root/store/dir_to_scan ->
$files = scandir("/root/store/dir_to_scan/");
this should to solve your problem
$carpeta= "/root/store/dir_to_scan";
if(is_dir($carpeta)){
if($dir = opendir($carpeta)){
while(($file = readdir($dir)) !== false){
if($file != '.' && $file != '..' && $file != '.htaccess'){
echo $file; //or save file name in an array..
// $array_file[] = $file;
}
}
closedir($dir);
}
}

PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]

I've noticed that the directory on which one of the PHP script is installed have very large error_log file almost of 1GB size, mostly the errors are generated by coding on line 478 and 479.. example of the error from error_log file is below:
PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for /home/khan/public_html/folder/ZBall.jar in /home/khan/public_html/folder/index.php on line 478
PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for /home/khan/public_html/folder/ZBall.jar in /home/khan/public_html/folder/index.php on line 479
I have the following coding, line 477 to 484
foreach ($mp3s as $gftkey => $gftrow) {
$ftimes[$gftkey] = array(filemtime($thecurrentdir.$gftrow),$gftrow);
$ftimes2[$gftkey] = filemtime($thecurrentdir.$gftrow);
}
array_multisort($ftimes2,SORT_DESC,$ftimes);
foreach ($ftimes as $readd) {
$newmp3s[] = $readd[1];
}
Please help me on this.
Thanks.. :)
The stat failed error would indicate that the file /home/khan/public_html/games/ZBall.jar either doesn't exist, or can't be read due to a permission error. Make sure the file exists in the place PHP is looking, as that seems like the most like cause of the problem.
Since it comes from the array $mp3s, make sure that array contains names of files that exist and modify it if not.
ask for the file before doing something with it. checm my edit:
<?php
foreach ($mp3s as $gftkey => $gftrow) {
if (file_exists($thecurrentdir.$gftrow)) {
$ftimes[$gftkey] = array(filemtime($thecurrentdir.$gftrow),$gftrow);
$ftimes2[$gftkey] = filemtime($thecurrentdir.$gftrow);
}
}
array_multisort($ftimes2,SORT_DESC,$ftimes);
foreach ($ftimes as $readd) {
$newmp3s[] = $readd[1];
}
There is a function on PHP.net site which removes a bug on Windows about filemtime
function GetCorrectMTime($filePath) {
$time = filemtime($filePath);
$isDST = (date('I', $time) == 1);
$systemDST = (date('I') == 1);
$adjustment = 0;
if($isDST == false && $systemDST == true)
$adjustment = 3600;
else if($isDST == true && $systemDST == false)
$adjustment = -3600;
else
$adjustment = 0;
return ($time + $adjustment);
}
source: http://php.net/manual/tr/function.filemtime.php#100692

multiple file upload foreach error

I have used the following code several times before, and have recently found it to try and use again. There now seems to be an error which I cannot fix, can anyone see what I am doing wrong?
foreach ($_FILES['image']['name'] as $i => $name) {
$uploadfile = $uploaddir . basename($name);
if (!move_uploaded_file($file_post["tmp_name"][$i],$uploadfile))
{
echo set_e('error','Image ['.$i.'] not uploaded','');
}
}
The error I get is
Warning: Invalid argument supplied for foreach() in /sitefolder/functions.php on line 1096
line 1096 is the first line in the first code box
First, never use array keys without checking that they exist. Wrap you code in
if (array_key_exists('image', $_FILES))
{
// ...
}
else
{
// error handling
}
Second, even if the key exists, $_FILES['image']['name'] is supposed to be a string, you cannot feed this to foreach anyway. Better:
foreach ($_FILES as $file)
{
$uploadfile = $uploaddir . basename($file['name']);
if (!move_uploaded_file($file["tmp_name"], $uploadfile))
{
echo set_e('error','Image ['.$i.'] not uploaded','');
}
}

PHP: Using scandir(), folders are treated as files

Using PHP 5.3.3 (stable) on Linux CentOS 5.5.
Here's my folder structure:
www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt
Using scandir() against the "myFolder" folder I get the following results:
.
..
testFolder
testFile.txt
I'm trying to filter out the folders from the results and only return files:
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
The expected results are:
testFile.txt
However I'm actually seeing:
testFile.txt
testFolder
Can anyone tell me what's going wrong here please?
You need to change directory or append it to your test. is_dir returns false when the file doesn't exist.
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir("myFolder/$file"))
{
echo $file.'\n';
}
}
That should do the right thing
Doesn't is_dir() take a file as a parameter?
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
Already told you the answer here: http://bugs.php.net/bug.php?id=52471
If you were displaying errors, you'd see why this isn't working:
Warning: Wrong parameter count for is_dir() in testFile.php on line 16
Now try passing $file to is_dir()
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
If anyone who comes here is interested in saving the output to an array, here's a fast way of doing that (modified to be more efficient):
$dirPath = 'dashboard';
$dir = scandir($dirPath);
foreach($dir as $index => &$item)
{
if(is_dir($dirPath. '/' . $item))
{
unset($dir[$index]);
}
}
$dir = array_values($dir);

Categories