Copy image file from one folder to another

Hi,
My name is John and im new, going through the book “Database driven website”. I wonder if anyone can help.
What is a simple method to copy an image in one folder on a sever to
another folder on the same server. also how to delete a image file on a server. Using PHP of course.
Thank you for your help.
Regards.
John

Hi there John, welcome to the forums :slight_smile:

Use [fphp]copy[/fphp] to copy the image, make sure you set the right permissions to destination folder.

Use [fphp]unlink[/fphp] to delete it, and again, make sure you set the right permissions.

Hi daemon,
Thanks for your prompt reply, still a bit unsure of removing a file.
bool unlink ( string filename [, resource context] )
What is the resource context ?Do I leave out string and only state the image
filename and path.
Regards,
John

The parameter between brackets is optional, you don’t have to use it.

‘string’ tells the data type of the parameter.

Ex.:


unlink("images/someimage.gif");

<?php
if(exec('xcopy c:\\\\mambo\\copy\\1 c:\\\\mambo\\copy\\2 /e/i'))
	echo "success";
else
	echo "false";
	
?> 

Thanks, got it now,
Regards,
John

can u lets us know wht u ve…

Hi, Have tried copy and unlink. My copy script is:

$file = ‘/tanner/products/nightvision.jpg’;
$newfile = ‘/tanner/special/nightvision.jpg’;
if (!copy($file, $newfile)) {
echo "failed to copy $file…
";
}

My script for unlink is:
unlink (‘/tanner/special/nightvision.jpg’);

Tried changing permission to image file, but not able.
Have changed permissions to both folders but still get the following message.

Warning: copy(): SAFE MODE Restriction in effect. The script whose uid/gid is 11032/10001 is not allowed to access / owned by uid/gid 0/0 in /usr/local/psa/home/vhosts/pieterse.me.uk/httpdocs/tanner/admin/stock_special.php on line 51
Please help!
Regards,
John

A function that copies contents of source directory to destination directory and sets up file modes.
It may be handy to install the whole site on hosting.

function copydirr($fromDir,$toDir,$chmod=0757,$verbose=false)
/*
   copies everything from directory $fromDir to directory $toDir
   and sets up files mode $chmod
*/
{
//* Check for some errors
$errors=array();
$messages=array();
if (!is_writable($toDir))
   $errors[]='target '.$toDir.' is not writable';
if (!is_dir($toDir))
   $errors[]='target '.$toDir.' is not a directory';
if (!is_dir($fromDir))
   $errors[]='source '.$fromDir.' is not a directory';
if (!empty($errors))
   {
   if ($verbose)
       foreach($errors as $err)
           echo '<strong>Error</strong>: '.$err.'<br />';
   return false;
   }
//*/
$exceptions=array('.','..');
//* Processing
$handle=opendir($fromDir);
while (false!==($item=readdir($handle)))
   if (!in_array($item,$exceptions))
       {
       //* cleanup for trailing slashes in directories destinations
       $from=str_replace('//','/',$fromDir.'/'.$item);
       $to=str_replace('//','/',$toDir.'/'.$item);
       //*/
       if (is_file($from))
           {
           if (@copy($from,$to))
               {
               chmod($to,$chmod);
               touch($to,filemtime($from)); // to track last modified time
               $messages[]='File copied from '.$from.' to '.$to;
               }
           else
               $errors[]='cannot copy file from '.$from.' to '.$to;
           }
       if (is_dir($from))
           {
           if (@mkdir($to))
               {
               chmod($to,$chmod);
               $messages[]='Directory created: '.$to;
               }
           else
               $errors[]='cannot create directory '.$to;
           copydirr($from,$to,$chmod,$verbose);
           }
       }
closedir($handle);
//*/
//* Output
if ($verbose)
   {
   foreach($errors as $err)
       echo '<strong>Error</strong>: '.$err.'<br />';
   foreach($messages as $msg)
       echo $msg.'<br />';
   }
//*/
return true;
}a

You’re using wrong paths. The full path is for example /usr/local/psa/home/vhosts/pieterse.me.uk/httpdocs/tanner/products/. Or you can use relative paths. In this case: ../products/


$file = '../products/nightvision.jpg';
$newfile = '../special/nightvision.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\
";
}

Hi, Thank you PHP deamon, How can one struggle with such a simple promblem.
I waisted a whole day (Stupid)
Thank you very much for all the help, it is very appreciated. Its working now. Have deleted the images three times and re-copied it.
Thank you again.
Regards.
John