我需要将图像上传到两个不同的文件夹中,这是我的代码在第一个文件夹中它正在移动,但是在第二个文件夹中它生成了无法移动第二个文件的异常
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['image']['name']);
$target_path1 = "thumbnails/";
$target_path1 = $target_path1 . basename($_FILES['image']['name']);
try {
//throw exception if can't move the file
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
}
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path1))
{
throw new Exception('Could not move 2nd file');
}
move_uploaded_file()
已移至$target_path
路径中的文件。 因此,您的temp
没有任何变化,第二次使用copy()
。 命令将其上传。
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
}
if(!copy ( $target_path , $target_path1 ))
{
throw new Exception('Could not move 2nd file');
}
上传后,它将删除临时文件( $_FILES['image']['tmp_name']
)。 您必须从第一次上传中复制该文件。
$success=true;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
$success=false;
}
if($success) {
if (!move_uploaded_file($target_path, $target_path1))
{
throw new Exception('Could not move 2nd file');
}
}
上载到第二个文件夹
if (!move_uploaded_file($target_path, $target_path1)){
throw new Exception('Could not move 2nd file');
}