I am currently using the following to upload an image into a folder but I am finding that for my specific purpose I need to copy the image as it is uploaded and add it to a folder a little higher up in the directory.
Example: The image file gets uploaded to /folder/folder/images and this is good. I need it there. However, I also need a copy with the same file with the same filename here: /folder/images
Here is the PHP I am using to upload the image to begin with. How can I mdify it to also copy and add to the other folder as a new image is uploaded?
<?php
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');
if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['file']['tmp_name'],
'images/'.$_FILES['file']['name'])){
$tmp='images/'.$_FILES['file']['name'];
echo 'images/'.$_FILES['file']['name'];
//echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
?>
UPDATE:
Here is the code that got things working for me:
<?php
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');
if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['file']['tmp_name'],'images/'.$_FILES['file']['name'])){
$tmp='images/'.$_FILES['file']['name'];
$new = '../images/'.$_FILES['file']['name']; //adapt path to your needs;
if(copy($tmp,$new)){
echo 'images/'.$_FILES['file']['name'];
//echo '{"status":"success"}';
}
exit;
}
}
echo '{"status":"error"}';
exit;
?>