0
<?php
$source_dir = "pimg";
$move1 = "1.png";
$target1 = "apple/boy/cat/d'!og";
$move2 = "2.png";
$target2 = "a/b";
if($moved = move_image($move1,$source_dir,$target1)) {
echo "<br> moved image = ".$moved;
}
if($moved = move_image($move2,$source_dir,$target2)) {
echo "<br> moved image = ".$moved;
}
function move_image($image, $source_dir, $target_dir) {
$target_dir_arr = explode('/',$target_dir);
$base_dir = $_SERVER['DOCUMENT_ROOT'];
$path = $base_dir;
foreach($target_dir_arr as $value) {
if(file_exists($path.'/'.$value)) {
$path.='/'.$value;
//echo "<br>".$path." exist";
}
else {
//echo "<br>".$path." does not exist. ";
if(mkdir($path.'/'.$value)) {
//echo " $value created under $path ";
$path.='/'.$value;
}
else {
echo " $value can not create under $path ";
return;
}
}
}
// move image ///////////////////////////////////
if(!empty($path)) {
if(file_exists($source_dir.'/'.$image)) {
if(copy($source_dir.'/'.$image, $path.'/'.$image)) {
//echo "<br>$image moved under $path";
unlink($source_dir.'/'.$image);
return $path.'/'.$image;
}
else {
echo "<br>$image move failed under $path";
return;
}
}
else {
echo "<br> source file $source_dir/$image does not exists. ";
return ;
}
}
}
?>
問題在哪裏? –