我正在使用此网站上的教程http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL构建多文件上传器。并且它工作得很好,但是我想将图像保存为SQL中的URL而不仅仅是它们的名称,并使用图像名称作为标题。这是我目前正在使用的代码:
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>
此代码给出了以下错误:
警告:mkdir():在C:\ xampp \ htdocs \ WebDevelopment \ AfterGlowWebsite \ CMS \ functions.php上没有这样的文件或目录:mkdir(“$ desired_dir”,0700); //如果目录不存在,则创建目录#
文件正在添加到SQL中,但未上载到目录中。
答案 0 :(得分:0)
问题已经解决。这是解决方案:
/* 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 /".$ file_name)中编写了user_data而不是媒体/专辑; }