1

下面的代码允许在“相册”目录中命名和创建一个新文件夹,并上传同一目录中的图像。你能告诉我如何将图像直接上传到我创建的新子文件夹中。这是表格:

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" 
    method="POST" enctype="multipart/form-data"> 
 Album name :  <input type="text" name="textfield" id="textfield"> 
 Upload image: <input type="file" name="fileup" /><br/>
  <input type="submit" name="button" id="button" value="Augsupieladet">
</form>  

和php代码

<?php
$get_folder = $_POST['textfield'];
mkdir ("./albums/" . $get_folder, 0777);
echo "Album created successfully";

$uploadpath = ("./albums");      //directory where the images are uploaded now
$max_size = 2000;          
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png', 'jpeg');        

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext);       
$err = '';         

if(!in_array($type, $allowtype)) 
$err .= 'Fails: <b>'. $_FILES['fileup']['name']. 'incorrect file type.';
if($_FILES['fileup']['size'] > $max_size*1000)
$err .= 'Max size of image: '.  $max_size. ' KB.';


if($err == '') {
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
echo 'Image: <b>'. basename( $_FILES['fileup']['name']). '</b> Upload succesful:';
}
else echo '<b>Upload unsuccesful.</b>';
}
else echo $err;
}
?> 
4

1 回答 1

1

您不能“直接”在 PHP 中选择文件上传到的位置。您想要做的是将上传的文件“移动”到正确的位置。

<?php
$get_folder = $_POST['textfield'];

// you should clean this..
$uploadpath =  "./albums/" . $get_folder.'/';

mkdir ($uploadpath, 0777);

echo "Album created successfully";
// remove the line below. variable was set previously
//$uploadpath = ("./albums");    
$max_size = 2000;          
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png', 'jpeg');        

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
    $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       
    $sepext = explode('.', strtolower($_FILES['fileup']['name']));
    $type = end($sepext);       
    $err = '';         

    if(!in_array($type, $allowtype)) 
        $err .= 'Fails: <b>'. $_FILES['fileup']['name']. 'incorrect file type.';
    if($_FILES['fileup']['size'] > $max_size*1000)
        $err .= 'Max size of image: '.  $max_size. ' KB.';


    if($err == '') {
        if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
            echo 'Image: <b>'. basename( $_FILES['fileup']['name']). '</b> Upload succesful:';
        }
        else echo '<b>Upload unsuccesful.</b>';
    }
    else echo $err;
}
?> 
于 2013-06-14T19:16:25.723 回答