我正在尝试创建图像上传器,但我在将文件保存到文件夹时遇到问题。该文件夹应该位于我的目录名为images的根目录中吗?如果是这样,当我使用move_uploaded_file代码时,为什么不将图像保存到该文件夹?
谢谢。
//connect to db include('includes/connection.php');
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into store (`id`,`name`,`size`,`type`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
$desired_dir="images/";
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, "$desired_dir/$file_name");
}else{ // rename the file if another one exist
$new_dir=$desired_dir.$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
echo '<img src="images/' . $row['id'] . '</img>';
答案 0 :(得分:0)
将文件夹的chmod放在775
上解释chmod
Chmod提供对您的文件夹或文件的访问权限。
哪里有特色?
The first charater stands for user The second character stands for group The thirth character stands for world
<强>版权强>
0 No Permissions 1 Execute Only 2 Write Only 3 Write & Execute Permissions 4 Read Only 5 Read & Execute Permissions 6 Read & Write Permissions 7 Read, Write & Execute Permissions
如何更改chmod
通过filezilla
通过直接管理
通过linux内核
答案 1 :(得分:0)
右键单击文件夹和属性,确保将权限设置为读写
答案 2 :(得分:0)
也许你可以这样纠正:
$desired_dir="images";
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,"images/".$file_name);
}else{ //rename the file if another one exist
$new_dir="images/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysqli_query($conny, $sql);
}else{
print_r($errors);
}
}