Kapil Arora
Kapil Arora

Reputation: 23

copy images from one folder to sub folder in php

I am facing a problem. I have some images stored in D://Images. I have to copy these images into D://Images/Modified. I tried but I did not get any output. This is my code:

define("BASE_IMAGE_PATH","D:\\");
define("IMAGE_FOLDER_NAME","2014finalfour\\");
define("IMAGE_FOLDER_NAME_MODIFIED","modified");
define("IMAGE_File_Path",BASE_IMAGE_PATH.IMAGE_FOLDER_NAME);
define("IMAGE_File_Path_Modified", BASE_IMAGE_PATH.IMAGE_FOLDER_NAME.IMAGE_FOLDER_NAME_MODIFIED);
$srcdir=constant("IMAGE_File_Path");
$destdir=constant("IMAGE_File_Path_Modified");
echo $destdir;
if (!file_exists(IMAGE_File_Path_Modified))
{
    mkdir(IMAGE_File_Path_Modified, 0777, true);
}
$srcdir=opendir($srcdir);
while($readFile = readdir($srcdir))
{
    if($readFile != '.' && $readFile != '..')
    {
        if (!file_exists($readFile)) 
        {
            if(copy($srcdir . $readFile, $destdir . $readFile))
            {
                echo "Copy file";
            }
            else
            {
                echo "Canot Copy file";
            }
        }
    }
}
closedir($srcdir);

Please help me to sought out it. It says it can not copy the file. copy() expects parameter 1 to be a valid path, resource given in C:\wamp\www\marcs\testmysql.php on line 3

Upvotes: 0

Views: 68

Answers (1)

munsifali
munsifali

Reputation: 1732

You could use the copy() function : copy('foo/test.php', 'bar/test.php');

example:

<?php
$file = 'images/folder/one.jpg';
$newfile = 'Images/folder/one_thumb.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy";
}

Makes a copy of the file source to dest. If the destination file already exists, it will be overwritten.

Upvotes: 1

Related Questions