steviem1986
steviem1986

Reputation: 309

move_uploaded_file to folder not working

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

Answers (3)

Cornell KIIK
Cornell KIIK

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

S.Visser
S.Visser

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

  1. make an connection to your ftp
  2. search your folder
  3. right click and select file permissions
  4. Set numeric value on 775

Via direct admin

  1. go to the file manager
  2. search for your folder
  3. Fill in "set permission" with the value 775
  4. press the button.

Via linux kernel

  1. start up the kernal
  2. run chmod 7 7 5 myfolder/

Upvotes: 1

user2268032
user2268032

Reputation: 13

Right click on the folder and properties and ensure the permissions are set to read and write

Upvotes: 0

Related Questions