将同一图像上载到两个不同的目录

时间:2016-09-08 21:12:59

标签: php

我想将相同的图像上传到两个不同的目录,但它只上传到名为upload/的第一个目录。我也尝试过复制命令,但无法将文件上传到名为photo/的第二个目录。我怎么能做到这一点?

<?php

$uname=strip_tags($_POST['uname']);

//$uname='user1';
$session_uid='user1';

$path='upload/';
$path1='photo/';


include_once 'background.php';

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" && isset($session_uid))
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];


if(strlen($name))
{
$ext = getExtension($name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{

$actual_image_name = time().$session_uid.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
$bgSave='<div id="uX'.$session_uid.'" class="bgSave wallbutton blackButton">Image Saved</div>';
if(move_uploaded_file($tmp, $path.$actual_image_name)){

echo $bgSave.'<img src="http://localhost/testing/'.$path.$actual_image_name.'"  id="timelineBGload" class="headerimage ui-corner-all" style="top:0px"/>';

}

// try upload to the second directyory 
if(!copy ($path, $path1.$actual_image_name)){
throw new Exception('Could not move 2nd file');
exit();
}


else
{
echo "Fail upload folder with read access.";
}
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format.";
}

else
echo "Please select image..!";

exit;
}
?>

1 个答案:

答案 0 :(得分:0)

就像您在move_uploaded_file()中所做的那样,您需要提供文件的完整源路径。 $tmp = $_FILES['photoimg']['tmp_name']是源临时文件的完整路径。因此,移动后您需要使用$path$actual_image_name

提供完整的源路径
if(!copy($path.$actual_image_name, $path1.$actual_image_name)){
    throw new Exception('Could not move 2nd file');
    exit();
}