Reputation: 309
i am trying to create an image uploader, but i am having trouble saving the image in a folder. Should the folder be in the root of my directory named images? and if so, how come it is not saving the images to that folder when i use move_uploaded_file code?
Thank you.
//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>';
Upvotes: 0
Views: 583
Reputation: 1
Maybe you can correct it like this:
$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);
}
}
Upvotes: 0
Reputation: 4735
Put the chmod of the folder on 775
Explanation chmod
Chmod gives access rights to your folder or files.
Where stands the charater for?
The first charater stands for user The second character stands for group The thirth character stands for world
Rights
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
How to change the chmod
Via filezilla
Via direct admin
Via linux kernel
Upvotes: 1
Reputation: 13
Right click on the folder and properties and ensure the permissions are set to read and write
Upvotes: 0