PHP copy says file does not exist when it does - php

im using the following code;
if( ! file_exists( $path ) ) { die( "'" . $path . "' not vaild path"); }
copy( $path, ltrim($create_folder . ltrim($path, "./"), ".") );
echo "'" . $path. "' => '" . $create_folder . ltrim($path, "./") . "'<br />";
The first if statement returns true yet the copy function returns;
Warning: copy('./files/c7a628cba22e28eb17b5f5c6ae2a266a/0003.css') [function.copy]: failed to open stream: No such file or directory
'./files/c7a628cba22e28eb17b5f5c6ae2a266a/0003.css' => './224efcdebda48350056af291f64a9311/files/8b571e7fbf9bacf4473024b11f78bc0dfiles/c7a628cba22e28eb17b5f5c6ae2a266a/0003.css'
If anyone knows why it would be much appreciated.

You might notice that your second half is missing a slash. As in, one of your path elements is "8b571e7fbf9bacf4473024b11f78bc0dfiles" instead of "8b571e7fbf9bacf4473024b11f78bc0d/files". Try $create_folder . '/' . ltrim($path, "./")
But the real answer is "your file actually does not exist. No, really, PHP is correct". It's just talking about the destination not existing; the source is fine.

Related

php rename (move) from one dir to another that is two levels back

my script file path is:
C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php
I need to run code from this path that will move images from path:
C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04
to:
C:\xampp\htdocs\wordpress\wp-content\uploads\images
My problem is that I have no idea how to force "rename" go two directories back in path.
I was trying something like:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
error I'm getting atm (I've changed my folder permissions, so maybe it is something with path?):
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied
. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
edit rename code:
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
$destPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/images');
/*$srcDir = opendir($srcPath);*/
echo $srcPath ;
sleep(1);
rename($srcPath, $destPath);
From looking at it, it looks wrong
$srcPath = realpath(dirname(__FILE__) . '/../..' . '/uploads/2017/04/obrazek.png');
it should be
$srcPath = realpath(dirname(__FILE__) . '../..' . '/uploads/2017/04/obrazek.png');
You have an extra / slash at the beginning.
It seems that you're in a Windows machine, so you're using the wrong type of slashes: /../..' . '/uploads/2017/04/obrazek.png'
You could try to replace them with backslashes:\..\..\uploads\2017\04\obrazek.png'
Or you could try something like this:
$path = substr($srcPath, 0, strrpos($srcPath, '\20'));
// $path now contaits: 'C:\xampp\htdocs\wordpress\wp-content\uploads';
// Then you can do:
$srcPath = $path . '\images';
Regarding the warning error:
Warning: rename(C:\xampp\htdocs\wordpress\wp-content\uploads\2017\04\obrazek.png,C:\xampp\htdocs\wordpress\wp-content\uploads\images): access denied. (code: 5) in C:\xampp\htdocs\wordpress\wp-content\plugins\uploadsdir-manager\test.php on line 16
It seems that you try to rename a file to directory, so probably you forgot to append the file name to the new path.
$srcPath contains a file. $destPath contains a directory, but it should contain the file name.
I've finally after many hours.. find out how to fix it so here you go:
$srcPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\2017\04') . '\obrazek2.png');
$destPath = (realpath (dirname(__FILE__) . '\..\..' . '\uploads\images') . '\obrazek2.png');
rename ($srcPath , $destPath );
The key was adding file name . '\obrazek2.png' after dirname (), because if filename is inside dirname () and it is destination path in rename, then it returns empty... :)
Not sure if I'm clear enough, because of my English, but it works and hopes it will help someone.

Php copy() is not working properly

I'm trying to copy my uploaded file to another directory called img folder with following code. But it doesn't work properly. I don't know why ? can you help me plz ?
php Code:
if($image["name"] != "")
{
//$path = PATH . DS . "uploads" . DS . "products" . DS . $id;
$path = "../../uploads" . DS . "products" . DS . $id;
$path2 = "img";
if(!is_dir($path))
{
mkdir($path);
}
chmod($path, 0755);
//move_uploaded_file($image["tmp_name"], $path . DS . $image["name"]);
move_uploaded_file($image["tmp_name"], $path . DS .
$uploadImage);//exit;
copy($uploadImage, $path2);
}
Following error message show:
Warning: copy(249.jpg) [function.copy]: failed to open stream: No such file or directory in...
The copy() function needs the full file path for the source file; you're just passing the filename, not the path.
As things stand, it's looking in the current folder for the file, not finding it, and throwing the error as a result.
From the previous line of code, it looks like your full path should be $path . DS . $uploadImage, so the copy command should look like this:
copy($path . DS . $uploadImage, $path2);
hope that helps.
Your copy function is wrong...
copy($uploadImage, $path2);
As Spudley answer says, you have to use the full path of the image. Also, $path2 is a directory. You have to give a name for the new copy of the image.
So, your copy function would be as follows:
copy($path . DS . $uploadImage, $path2. DS . $uploadImage);
Try it and let us know.

Zend Framework PHP -> realpath()

im trying to create a Zend Framework custom Provider and here:
if($module == 'default'){
$modelsPath = dirname(__FILE__) . '/../application/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = dirname(__FILE__) . '/../application/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
when i im trying to create a path to the file and when its default module everything is ok, but when for ex. module is other word no matter what the realpath returns false?! where is the error?
try using the APPLICATION_PATH constant.
if($module == 'default'){
$modelsPath = APPLICATION_PATH . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = APPLICATION_PATH . '/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
Taken out of the php manual:
realpath() returns FALSE on failure, e.g. if the file does not exist.
Note:
The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.
So the problem most probably is with folder permission or the path simply is wrong!
Double check that the paths exist by simple echoing $filePath. If this checks out to be okay then ensure that the 'user' (apache, www-data, for example) have enough privileges. If you are not sure who your server is operating as you can simply debug by echoing whoami

Change file contents

We have a script, /scripts/ourscript.php and a file, /texts/elvis.txt.
How can we change contents of this file, when we run ourscript.php?
Use file_put_contents() method to set the contents of a file.
If you need just to save new data, you can do:
$elvis = 'Contents here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
If, instead of saving data, you need to change existing data, do:
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
$elvis = file_get_contents($fileName);
// Do changes to $elvis here.
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
Finally, if you need to append something new to existing contents, use:
$elvis = PHP_EOL . 'Contents to append to existing stuff here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
// Noticed FILE_APPEND as third argument?
if (file_put_contents($fileName, $elvis, FILE_APPEND) === false)
{
// Handle error here.
}
While MainMa has given you a direct answer, I'll point you to:
http://php.net/manual/en/function.file.php
Since it seems that you might have more of these questions, which could have been easily answered by looking at the documentation.
Also by figuring things out with the help of the documentation you'll learn how to solve such problems on your own, you know independence is a nice thing to have :)

Is there a glitch with opendir/readdir?

Here is my PHP code:
<?php
// Enumerate the directories in styles
$styles_dir = 'styles/';
if($handle = opendir($styles_dir))
{
while(FALSE !== ($file = readdir($handle)))
{
echo $file . '(' . is_dir($file) . ')<br>';
}
}
?>
Here are the directories in styles:
http://files.quickmediasolutions.com/php.jpg
And here is the output:
.(1)
..(1)
forest()
industrial()
Why aren't forest and industrial directories?
The path for is_dir is relative to the base file, so you really need to do a test like
is_dir($styles_dir . '/' . $file)
Note that this is masked for the . and .. "directories" as these exist everywhere.
You need to prefix the directory name to the file name as is_dir works relative to the current directory.
Change
echo $file . '(' . is_dir($file) . ')<br>';
to
echo $file . '(' . is_dir("$styles_dir/$file") . ')<br>';
Alternatively you can change the directory to $styles_dir using chdir and then your current code will work.

Categories