将上传的文件保存在选定的目录中

saving uploaded files in selected directory

我正在构建一个多文件上传器,使用本网站上的教程,http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL。它工作得很好,但是我想将图像保存为 url in SQL 而不仅仅是它们的名称,并使用图像名称作为标题。这是我目前使用的代码:

if (isset($_POST['add_photos'])) {
include 'connect.php';
$album = (isset($_POST['album']) ? $_POST['album'] : null);
$errors= array();
foreach($_FILES['photos']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['photos']['name'][$key];
    $file_size =$_FILES['photos']['size'][$key];
    $file_tmp =$_FILES['photos']['tmp_name'][$key];
    $file_type=$_FILES['photos']['type'][$key]; 
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }
    include 'connect.php';      
    $stmt = $conn->prepare("INSERT into album_images(imageURL, AlbumID) VALUES (?, ?)");
    $stmt->bind_param('ss', $file_name,$album);
    $desired_dir="media/albums";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"user_data/".$file_name);
        }else{                                  //rename the file if another one exist
            $new_dir="media/albums/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }

       $stmt->execute();
    $stmt->close();
    }else{
            print_r($errors);
    }
}
if(empty($error)){
    echo "Success";
}

}

HTML代码

<form id="add-photos" action="" method="post" enctype="multipart/form-data">
    <h5>Add Photos</h5>
    <div class="input-short" id="select_album">                 
    <?php select_album("SELECT * FROM album"); ?>
    </div>
    <label class="myLabel" id="upload-images">
    <input type="file" name="photos[]" id="fileToUpload"  multiple="multiple"/>
    <span>Select Photos</span>
    </label> 
    <button  id="facebook-image-submit" type="submit" name="add_photos">Add Photos</button>
 </form>

这段代码给我以下错误:

Warning: mkdir(): No such file or directory in C:\xampp\htdocs\WebDevelopment\AfterGlowWebsite\CMS\functions.php on line: mkdir("$desired_dir", 0700); // Create directory if it does not exist#

正在将文件添加到 SQL 但未上传到目录中。

问题已解决。这是解决方案:

/* upload photos to gallery */
if (isset($_POST['add_photos'])) {
include 'connect.php';
$album = (isset($_POST['album']) ? $_POST['album'] : null);
$errors= array();
foreach($_FILES['photos']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['photos']['name'][$key];
    $file_size =$_FILES['photos']['size'][$key];
    $file_tmp =$_FILES['photos']['tmp_name'][$key];
    $file_type=$_FILES['photos']['type'][$key]; 
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }
    include 'connect.php';
    $imageURL = "media/albums/".$file_name;
    $stmt = $conn->prepare("INSERT into album_images(imageURL, AlbumID) VALUES (?, ?)");
    $stmt->bind_param('ss', $imageURL ,$album);
    $desired_dir="../media/albums";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir($desired_dir, 0700);      // Create directory if it does not exist
        }
        if(is_dir($desired_dir."/".$file_name)==false){
            move_uploaded_file($file_tmp,"../media/albums/".$file_name);
        }else{                                  //rename the file if another one exist
            $new_dir="media/albums/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }
    $stmt->execute();
    $stmt->close();
    }else{
            print_r($errors);
    }
}
if(empty($error)){
    echo "Success";
}

问题是我在 move_uploaded_file($file_tmp,"../media/albums/" 中写了 user_data 而不是 media/albums。 $file_name); }