Php copy() is not working properly - php

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.

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 function to search paths for filename WITHOUT extension

Right so i'm making a configuration class that will use an array of different file types in 4 main locations. Now I want to make it so that the configuration class will search these locations in order at the moment i'm using the following
if (file_exists(ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file)) {
$this->filePath = ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(ROOT . DS . "Application/Config/$file")) {
$this->filePath = ROOT . DS . "Application/Config/$file";
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file)) {
$this->filePath = CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . "Config/$file")) {
$this->filePath = CARBON_PATH . "Config/$file";
echo $this->filePath;
} else {
throw new \Exception("Unable to locate: $file, Please check it exists");
}
}
}
}
pretty messy and not very flexible.
What I want to be able to do is search the locations in the same order BY FILE NAME ONLY after finding the first match It would then return the file with the extension for the configuration class to use the correct method to parse into a php array and so on.
What is the best way to search these locations for a file name
Example
Say we want a database configuration file as you can see there are 2
ConfigLocation1/Dev/
/file.php
/database.json
ConfigLocation1/
/database.ini
/anotherfile.json
I would want to use the function like so
config::findFile('database');
and it return
$result = ConfigLocation1/Dev/database.json
but if it wasnt found here then then
$result = ConfigLocation1/database.ini
Not very good at explaining things so hope the example helps
As you mentioned you need to check for file in 4 locations, so instead of if conditions, create an array of directories and loop through.
and you can use glob, to find a file irrespective of extension. see my example below:-
//Make a array of directory where you want to look for files.
$dirs = array(
ROOT . DS . 'Application/Config/' . APP_ENV . DS,
CARBON_PATH . 'Config' . DS . APP_ENV . DS
);
function findFiles($directory, $filename){
$match = array();
foreach ($directory => $dir) {
$files = glob($dir.$filename);
foreach ($files as $file) {
$match[] = $file;
}
}
return $match;
}
// to find database
$results = findFiles($dirs, 'database.*');

php move_uploaded_file (Ubuntu)

i have problem with uploading file by move_uploaded_file function.. This return false, but $_FILES['myfile']['errors'] = 0 ... Permissions are 777 recursive for my project.. Directory's owner www-data.. How can i to see more info about problem? Help me please! I sow a lot of posts about this problem, but ewerywhere problem was incorrect permissions. I think me problem is different, because i have 777. First created directory mkdir(..., 0777), when i sow this not worked i open terminal and run sudo chmod -R 777 /var/www/myproject
Im sorry, my english isnt good.
if(isset($_FILES['path'])) {
$path = DS . 'images' . DS . 'admin' . DS . md5($_FILES['path']['name']) . DS;
if(!is_dir(ROOT_PATH . DS . 'public' . $path)) {
mkdir(ROOT_PATH . DS . 'public' . $path, 0777, true);
}
$r = move_uploaded_file($_FILES['path']['tmp_name'], ROOT_PATH . DS . 'public' . $path . $_FILES['path']['name']);
}

PHP - JPath Moving Multiple files to correct directory

See my last question as this links to it: PHP - Moving multiple files with different files names to own directory
So i decided to using the Joomla API however, the documentation was for 1.5 and 2.5 system only but i'm using 3.0. I have a number of files that look like this:
"2005532-JoePharnel.pdf"
and
"1205121-HarryCollins.pdf"
Basically I want to create a PHP code that when someone ftp uploads those files to the upload folder that it will 1) Create a directory if it doesn't exist using there name 2) Move the files to the correct directory (E.g. JoePharnel to the JoePharnel Directory ignoring the number at the beginning)
Updated: 23/10/14 - 14:05:
My new code creates the folder but won't move the file in the upload into that new folder, code is below:
<?php
define( '_JEXEC', 1);
define('JPATH', dirname(__FILE__) );
if (!defined('DS')){
define( 'DS', DIRECTORY_SEPARATOR );
$parts = explode( DS, JPATH );
$script_root = implode( DS, $parts ) ;
// check path
$x = array_search ( 'administrator', $parts );
if (!$x) exit;
$path = '';
for ($i=0; $i < $x; $i++){
$path = $path.$parts[$i].DS;
}
// remove last DS
$path = substr($path, 0, -1);
if (!defined('JPATH_BASE')){
define('JPATH_BASE', $path );
}
if (!defined('JPATH_SITE')){
define('JPATH_SITE', $path );
}
/* Required Files */
require_once ( JPATH_SITE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_SITE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.user.user');
//First we set up parameters
$searchpath = JPATH_BASE . DS . "upload";
//Then we create the subfolder called png
if ( !JFolder::create($searchpath . DS ."Images") ) {
//Throw error message and stop script
}
//Now we read all png files and put them in an array.
$png_files = JFolder::files($searchpath,'.png');
//Now we need some stuff from the JFile:: class to move all files into the new folder
foreach ($png_files as $file) {
JFile::move($searchpath. DS . ".png" . $file, $searchpath . DS. "Images" . $file);
}
//Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . DS. "Images",JPATH_COMPONENT) ) {
//Redirect with perhaps a happy message
} else {
//Throw an error
}
}
?>
Only error i get is Notice: Use of undefined constant JPATH_COMPONENT - assumed 'JPATH_COMPONENT' in /upload.php on line 70. But doesn't stop it working, am so close on this any help is greatly appreciated. I want to know where its taking the image, i think i have worked out the "DS" now.
Thanks

Upload same Image one server to another server using php

I search this question in Stackoverflow but I don't understand the answer. Sorry.
Well, I've a html upload form which handle by Php.
Following is my php upload script :
if($image["name"] != "")
{
//$path = PATH . DS . "uploads" . DS . "products" . DS . $id;
$path = "../../uploads" . DS . "products" . DS . $id;
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;
}
Now I'm trying to upload same image to Another website folder. Like : www.mysitename/folder/finalfolder. in the same time with Php.
Is there any way to upload it another website ?
The other server will need a script to handle the request. You can then POST that data to the other server's script using a CURL request. (See the link posted by #Andrey Volk)
Or, as suggested here: FTP will work too, if you have that level of access to the server.

Categories