<?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 ;
}
}
}
?>
答案 0 :(得分:0)
为什么不能使用chmod()
?
<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);
// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>