Akshay Aggarwal
Akshay Aggarwal

Reputation: 262

not able to upload files in specific directory

i have made a small image uploading system using php it was working fine but now when i tried to upload pics in a specific folder then it is giving errors and upload images in root folder only.

the php code is:

 <?php
    //checking if the user already has a directory or not.
    if($r['directory']==0){
        $query= "UPDATE user SET directory='1' where id='".$_SESSION['sess_id']."'";
        mysqli_query($dbconn,$query);
        $path='../akshay/work/'.$r['username'];
        mkdir($path) or die("unable to create folder");
    } if(isset($_POST['submit'])&& $r['directory']==1){
    //uploading users work.
        $path='../akshay/work/'.$r['username'];
        $name=$_FILES['work']['name'];
        $size=$_FILES['work']['size'];
        $type=$_FILES['work']['type'];
        $tmp_name=$_FILES['work']['tmp_name'];
        $extension=strtolower(substr($name,strpos($name,'.') + 1));
        //checking if the user has selected a file or not.
        if(isset($name)){
            if(!empty($name)){
            //checking if the uploaded file is an image or not.
                if($extension=='jpg'||$extension=='jpeg'&& $type=='image/jpeg'){
                    $location=$path;           
                    if(move_uploaded_file($tmp_name,$location.$name)){           
                        echo'uploaded succesfully';
                    }else{
                        echo'<p style=color:red;>please choose a file</p>';
                    }
                } else {
                 echo'<p style=color:red;>format of the file must be jpg/jpeg</p>';
                }
            }       
        }
    }
?>

i think the problem is in move_uploaded_file() function. plz help me fix this problem and please do tell me any other ways (if any) to do this. i am able to make directories using the first check.

the error is "undefined variable:dir_name "and prints the massage "uploaded succesfully".

i have did some editing in the code and now i am able to make a directory in the specified location but cant upload images in that folder instead it uploads the images in 'work' folder and with the username appended to the name of the image.

please help!!!

Upvotes: 0

Views: 179

Answers (2)

M Ikram Zafar
M Ikram Zafar

Reputation: 150

use this code ..

$directory ="../images/".$file_name;

move_uploaded_file($temp_name, $directory);

i think this will work for you

Upvotes: 0

Rahul Kaushik
Rahul Kaushik

Reputation: 1464

In else if part of your code, $dir_name is not defined.

So please update your code and add below:

else if(isset($_POST['submit'])&& $r['directory']==1){
    $dir_name = $r['username'];
// your code continue..

Furthermore.. the code you are using is not good. as if if condition is true then the else if part will not be executed. so update your code as below:

<?php
//checking if the user already has a directory or not.
if($r['directory']==0){
$query= "UPDATE user SET directory='1' where id='".$_SESSION['sess_id']."'";
mysqli_query($dbconn,$query);
$dir_name=$r['username'];
$r['directory']=1;
mkdir($dir_name);
}
if(isset($_POST['submit'])&& $r['directory']==1){
    $dir_name = $r['username'];
//uploading users work.
$name=$_FILES['work']['name'];
$size=$_FILES['work']['size'];
$type=$_FILES['work']['type'];
$tmp_name=$_FILES['work']['tmp_name'];
$extension=strtolower(substr($name,strpos($name,'.') + 1));
//checking if the user has selected a file or not.
if(isset($name)){
    if(!empty($name)){
        //checking if the uploaded file is an image or not.
         if($extension=='jpg'||$extension=='jpeg'&& $type=='image/jpeg'){

             if(move_uploaded_file($tmp_name,$dir_name.$name)){
                 echo'uploaded succesfully';
             }else{
                echo'<p style=color:red;>please choose a file</p>';
             }
         }
        else
        {
             echo'<p style=color:red;>format of the file must be jpg/jpeg</p>';
        }
    }

}

}
?>

Upvotes: 1

Related Questions