I am doing a rename so I can move a folder. The move is successful, but I keep getting a warning:
Warning: rename(site_files/259,trash/site_files/259) [function.rename]: No such file or directory in /home/oosman/public_html/lib.php on line 79
This is my code:
$path_parts = pathinfo($file);
$d = $path_parts['dirname'];
$f = $path_parts['basename'];
$trashdir='trash/'.$d;
mkdir2($trashdir);
if(!is_dir($trashdir))
return FALSE;
rename($file, $trashdir.'/'.$f); // this is line 79 where the warning is coming from
Why am I getting this warning?
FYI the mkdir2 is just my recursive mkdir function
function mkdir2($dir, $mode = 0755)
{
if (#is_dir($dir) || #mkdir($dir,$mode)) return TRUE;
if (!mkdir2(dirname($dir),$mode)) return FALSE;
return #mkdir($dir,$mode);
}
This is just because the source or targeting folder does not exist.
This will remove the warning anyway but not the best way to solve the question:
if(file_exists($file) && file_exists($trashdir)){
rename($file, $trashdir.'/'.$f);
}
In order to find out what the problem really is, please check following questions:
1.Does the source file(site_files/259) exist? Does it have an extension like 259.txt?
From your log , I guess the absolute path of the original file should be /home/oosman/public_html/site_files/259.
2.Have you successfully created the target folder? Can you see it on the disk and get TRUE from mkdir2()?
3.I strongly suggest that you use the absolute path but not the relative path when you use rename().
rename('/home/oosman/public_html/site_files/259', '/home/oosman/public_html/trash/site_files/259');
but not
rename('site_files/259', 'trash/site_files/259');
Maybe something wrong with the relative path?
Updated 2014-12-04 12:00:00 (GMT +900):
Since it is not anything mentioned above could you please log something to help me clarify?
Please change
rename($file, $trashdir.'/'.$f);
to
echo "Before moving:\n"
echo "Orgin:".file_exists($file)."\n";
echo "Target parent folder:".file_exists($trashdir)."\n";
echo "Target file:".file_exists($trashdir.'/'.$f)."\n";
rename($file, $trashdir.'/'.$f);
echo "After moving:\n"
echo "Orgin:".file_exists($file)."\n";
echo "Target parent folder:".file_exists($trashdir)."\n";
echo "Target file:".file_exists($trashdir.'/'.$f)."\n";
If this outputs:
Before moving:
Origin:1
Target parent folder:1
Target file:0
Warning: rename(site_files/259,trash/site_files/259) [function.rename]: No such file or directory in /home/oosman/public_html/lib.php on line 83
After moving:
Origin:0
Target parent folder:1
Target file:1
exactly only once, then I am out. If it doesn't, please tell me the difference.
One possibility is simply to hide the warning:
error_reporting(E_ALL & ~E_WARNING);
rename($file, $trashdir.'/'.$f);
error_reporting(E_ALL & ~E_NOTICE);
Related
Using PHP, I wish to list the files in a small folder, along with their filesizes (and optionally their modified dates)
The file names are displayed OK, but the file sizes appear to be null.
E.G.-
$myDir = opendir("xxxxxxxx");
// get each entry
while($entryName = readdir($myDir)) {
if ($entryName != "." && $entryName != "..") {
echo $entryName . ' --- ' . filesize($entryName) . ' bytes<br/>';
$dirArray[] = $entryName;
}
}
This shows up as
file1 --- bytes
file2 --- bytes etc....
Why isn't the filesize being picked up? (I'll deal with the filedate later) ?
You're looping through the files (and you get the names correctly) since you already got the files for that specific folder. But, when you try to get the filesize, you're not typing the complete path. Remember, you're inside a directory now.
So, supply the filesize() function the whole relative path to each file and it'll work fine:
$dir = "test";
$myDir = opendir($dir);
while($entryName = readdir($myDir)) {
if ($entryName != "." && $entryName != "..") {
echo $entryName . ' --- ' . filesize($test."/".$entryName) . ' bytes<br/>';
$dirArray[] = $entryName;
}
}
As a side note, the sizes appeared as "empty" because the function was actually erring out, but you probably have error reporting off, so nothing was showing.
It's really helpful to have it on (in development only, in production you should hide errors and log them to a file or the like), since you'll immediately know that somethings wrong, and most of the times the error messages pretty much spell out the solution to the problem for you.
For instance, the error you'd have gotten with error reporting on:
Warning: filesize(): stat failed for ... in ... on line xxx
To turn error reporting on for the duration of the script, add this line right after opening the PHP tag:
error_reporting(E_ALL);
And to turn it on more permanently in your php.ini, add, modify or uncomment (depending on your current settings) this line:
error_reporting = E_ALL
Read more about error reporting in the manual
I searched everywhere for this problem and can't find the solution. I have this:
<?php
$file_name = $_GET['name'];
$file_delete = '../u/' . $file_name;
unlink($file_delete);
//header("location: $file_delete");
?>
unlink returns the error: No such file or directory, but if I try header("location: $file_delete"); it opens the file (picture in this case).
Where may I be wrong?
Get Absolute path first for the file to be deleted and check file exist before delete:
$file_name = $_GET['name'];
$base_dir = realpath($_SERVER["DOCUMENT_ROOT"]);
$file_delete = "$base_dir/your_inner_directories_path/$file_name";
if (file_exists($file_delete)) {unlink($file_delete);}
After some research, unlink() doesn't seem to allow you to use relative paths (with "../").
Here's an alternative:
<?php
$file_name = $_GET['name'];
$file_delete = dirname(__FILE__, 2) . '\\u\\' . $file_name;
unlink($file_delete);
?>
$file_delete here is the absolute path to the file you want to delete.
Reminder: / is used for Unix systems, \ for Windows.
PHP doc:
- http://php.net/manual/en/function.unlink.php
- http://php.net/manual/en/function.dirname.php
I also had same issue with my code. What I did to solve the issue is:
First execute:
var_dump($image_variable) // var_dump($file_delete) in your case.
It outputs: string(23)(my-image-path )
When I started counting string I just found 22 characters. I wondered where is the 23rd?
I checked and count carefully, at the end I found that there is space at the end of my image path. So I used php trim() function to remove white spaces. Like,
$trimed_path = trim($image_variable) // trim($file_delete) in your case.
Second: Now execute,
unlink($trimed_path).
OR CHECK LIKE
if(unlink($trimed_path))
{
echo "File Deleted";
}
else
{
echo "Error Deleting File";
}
Took me a couple of hours to figure out. As mentioned above unlink() is picky when it comes to paths.
Solution is:
1st) Define the path (this is how Wordpress does it btw):
define( 'ROOTPATH', dirname(dirname(__FILE__)) . '/' );
2) Do:
unlink(ROOTPATH.'public_html/file.jpg');
I'm having trouble trying to get attributes from files in my list. The code is:
if ($this->dir = opendir($caminho))
{
$this->itens = array();
$this->itensTotal = 0;
$tipos = array("dir" => 0, "file" => 1);
while ($item = readdir($this->dir))
{
if (!in_array($item, $this->skip))
{
$t = filetype($item);
$this->itens[$tipos[$t] . $item] = array("nome" => $item,
"size" => filesize($item),
"tipo" => $t,
"perm" => fileperms($item));
$this->itensTotal++;
}
}
}
Seeing that my script is 'file.php' and is in the folder 'www'. When it reads it's own folder (www) it works ok and lists all files and directoryies with their attributes. But when it tryies to read eg.: /www/folder/ the function filetype(), filesize() an fileperms() doesn't works! I get these warnings for all itens in the directory:
Warning: filetype() [function.filetype]: Lstat failed for bkp in
D:\UniformServer\UniServer\www\files.php on line 174
Warning: filesize() [function.filesize]: stat failed for bkp in
D:\UniformServer\UniServer\www\files.php on line 176
Warning: fileperms() [function.fileperms]: stat failed for bkp in
D:\UniformServer\UniServer\www\files.php on line 178
It's opens the folder, read it's contents but these functions doesn't woks =s
Notes:
As you can see I'm running it on Windows
$caminho has valids paths
Please, any help will be welcome cause google doesn't helped.
The file operations use the real directory names on the system, not the relative path of the website, and "/www/folder" probably doesn't exist then. From your comment, you would need either: "D:/UniformServer/UniServer/www/folder" or use a relative path from the php script.
I think $item only contain the basename of the file. You should probably prepend the directory path to it, like this: filetype(this->dir . $item).
Hope you help me... I've been at this for the past 2 days and have to admit that I'm stumped.
The OS I'm on is Ubuntu 9.10 Karmic.
I successfully installed and tested Mapserver. For my class project, I have a php script that I am using to create a layer see below....
The error I get when run the script on a cmd line prompt:
Warning: [MapServer Error]: msProcessProjection(): no system list, errno: 2
in /var/www/mapserverdocs/ms4w/apps/world/mapscripts/staticwms.php on line 16
Warning: Failed to open map file static.map in /var/www/mapserverdocs/ms4w/apps/world/mapscripts/staticwms.php on line 16
Fatal error: Call to a member function owsdispatch() on a non-object in /var/www/mapserverdocs/ms4w/apps/world/mapscripts/staticwms.php on line 18
PHP SCRIPT:
<?php
if (!extension_loaded("MapScript")) dl("php_mapscript");
$request = ms_newowsrequestobj();
foreach ($_GET as $k=>$v) {
$request->setParameter($k, $v);
}
$request->setParameter("VeRsIoN","1.0.0");
ms_ioinstallstdouttobuffer();
$oMap = ms_newMapobj("static.map");
$oMap->owsdispatch($request);
$contenttype = ms_iostripstdoutbuffercontenttype();
if ($contenttype == 'image/png') {
header('Content-type: image/png');
ms_iogetStdoutBufferBytes();
} else {
$buffer = ms_iogetstdoutbufferstring();
echo $buffer;
}
ms_ioresethandlers();
?>
I made the directory and files world wide rwx just to make sure it was not a permissions issue
Any help would be greatly appreciated!!
Thanks
Chris
As meagar said, the issue is probably that this line:
$oMap = ms_newMapobj("static.map");
is unable to find "static.map". The current working directory of PHP is very often not what you'd expect it to be. Try making the path be relative to the current script. If static.map is in the same directory as static.map, try this code:
$mapPath = dirname(__FILE__).'/static.map';
$oMap = ms_newMapobj($mapPath);
$oMap->owsdispatch($request);
if static.map is at, let's say, /var/www/mapserverdocs/ms4w/apps/world/mapfiles/static.map, then try:
$mapPath = dirname(__FILE__).'/../static.map';
$oMap = ms_newMapobj($mapPath);
$oMap->owsdispatch($request);
Notice the */../*static.map. dirname(__FILE__) will return the name of the directory of the PHP file you place that code in.
My current directory structure is as follows:
C:\xampp\htdocs\PHP_Upload_Image_MKDIR
In other words, the following directories do NOT exist at all.
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded\s002
The problem is that when I run the following script, the function is_dir always return TRUE.
Based on the manual, http://us2.php.net/manual/en/function.is-dir.php
is_dir: Returns TRUE if the filename exists and is a directory, FALSE otherwise.
Do I miss something here?
Thank you
$userID = 's002';
$uploadFolder = '/PHP_Upload_Image_MKDIR/uploaded/';
$userDir = $uploadFolder . $userID;
echo '<br/>$userDir: ' . $userDir . '<br/>';
if ( is_dir ($userDir))
{
echo "dir exists"; // always hit here!!!
}
else
{
echo "dir doesn't exist";
}
mkdir($userDir, 0700);
C:\xampp\htdocs\PHP_Upload_Image_MKDIR>dir /ah
Volume in drive C is System
Volume Serial Number is 30B8-2BB2
Directory of C:\xampp\htdocs\PHP_Upload_Image_MKDIR
File Not Found
C:\xampp\htdocs\PHP_Upload_Image_MKDIR>
//////////////////////////////////////////////////////////
Based on Artefacto's comments:
Here is the output of C:\PHP_Upload_Image_MKDIR\uploaded\s005
echo '<br/>' . realpath($userDir) . '<br/>';
Thank you for the solutions.
Best wishes
Also, it seems as if the dir you are checking is PHP_Uploaded_Image_MKDIR/uploaded/s002, which is an absolute path starting from the root filesystem.
Try prepending C:\xampp\htdocs\ to this and see if it works then. Also, check to see if the folder exists at the root of the volume.
Try file_exists() instead.
http://php.net/manual/en/function.file-exists.php
If you've run that script more than once, then is_dir($userDir) will return true because of this line (the last one) in your script:
mkdir($userDir, 0700);
You can use rmdir() or some other method to delete it.
To test is_dir(), try a directory name that has never been used / created. Something like the following should return false, when it does, you know that is_dir() works:
if ( is_dir ("/PHP_Upload_Image_MKDIR/uploaded/lkjlkjlkjkl"))