我目前正在使用以下内容将图像上传到文件夹中,但我发现,为了我的特定目的,我需要在上传时复制图像,并将其添加到目录中稍高的文件夹中。 / p>
示例:图像文件上传到 / folder / folder / images ,这很好。我需要那里。但是,我还需要一份具有相同文件名的副本: / folder / images
这是我用来上传图片的PHP。如何在上传新图像时将其复制并复制并添加到其他文件夹?
<?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;
?>
更新:
以下代码让我感觉很舒服:
<?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;
?>
答案 0 :(得分:2)
根据您对问题的评论:
.htaccess
答案 1 :(得分:1)
只是copy它!
$source='folder/folder/images/imageexists.jpg';
$destination='folder/images/imagedoesnotexists.jpg';
if(!copy($source, $destination)){
//copy failed
}
答案 2 :(得分:0)
最好使用copy()
函数。
if(move_uploaded_file($_FILES['file']['tmp_name'],
'images/'.$_FILES['file']['name'])){
$tmp='images/'.$_FILES['file']['name'];
echo 'images/'.$_FILES['file']['name'];
copy($tmp,'/folder/images/'.$_FILES['file']['name']);
//echo '{"status":"success"}';
exit;
}