Bushra Zaher
Bushra Zaher

Reputation: 9

Image uploads to folder with php

i have an image uploading script with php it work will in localhost server but when I upload the website to my domain server it will upload the image but not in the spathfic folder I set it well set the folders path as part of the image name like

the path is

admin\upload\novelimage

the image name will be like this

upload\novelimage\...image that uploaded

i didnt no if it is error in the script or the server but i didnt see any need for uploads permission on thier side so i guess that the problem was on the script here is the script

  //upload files
            $avatarname = $_FILES['NovelImage']['name'];
            $avatarsize = $_FILES['NovelImage']['size'];
            $avatartmp = $_FILES['NovelImage']['tmp_name'];
            $avatartype = $_FILES['NovelImage']['type'];

            $avatarAllowedextention = array("jpeg", "jpg", "png", "gif");

            //get avatar extention

            $avatarextention = strtolower(end(explode('.', $avatarname)));
              #get the varibles from the form 
              $Novelname = $_POST['Novelname'];
              $Describe = $_POST['Describe'];
              $category = $_POST['langOpt3'];
              // condtion ? true and false
                  //check username
                  $formerrors = array();
                  if (! empty($avatarname) && ! in_array($avatarextention, $avatarAllowedextention)) {
                    $formerrors[] = 'avatar extension is not allowed';
                }
                if (empty($avatarname)) {
                    $formerrors[] = 'empty avatar is not allowed';
                }
                if ($avatarsize > 4194304) {
                    $formerrors[] = 'avatar size is bigger than 4mb';
                }
                foreach ($formerrors as $error) {
                    echo '<div class="alert alert-danger">' . $error . '</div>';
                }
                if (empty($formerrors)) {
                    $avatar = rand(0, 1000000) . '_' . $avatarname;

                    move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);

                  $check = checkitem("NovelName", "novel", $Novelname);

                  if ($check == 1) {
                      $theMsg = "<div class='alert alert-danger'>sorry this user name is exit</div>";

                      redirecthome($theMsg, 'back');
                  }else{
                        //insert userinfo into database whith this info
                        $stmt = $con->prepare("INSERT INTO novel(NovelName, NovelImage, description) 
                        VALUES(:znovel, :zNovelImage, :zdescription)");
                        $stmt->execute(array(
                            'znovel' => $Novelname,
                            'zNovelImage' => $avatar,
                            'zdescription' => $Describe
                        ));
                        $lastid = $con->LastInsertId();
                        $stmt = $con->prepare("INSERT INTO book_category(novelid, categoryID) 
                        VALUES(:znovelid, :zcategoryID)");
                        foreach ($category as $cat) {
                            $stmt->execute(array(
                                'znovelid' => $lastid,
                               'zcategoryID' => $cat
                             ));   
                        }
                    }

                        
                    //erg
                    $theMsg = "<div class='alert alert-success '>" . $stmt->rowCount(). 'Record Updated</div>'; 

                    redirecthome($theMsg, 'back', 100);
                  } 

i really cant see any problem here please help

Upvotes: 1

Views: 32

Answers (1)

Boris Biswas
Boris Biswas

Reputation: 67

Your Question is not that Clear, But to confirm this "You images are not uploaded to specific path you want in server" From your path code, I'm assuming your localhost was running in Windows OS, and From the issue i think your server is Linux Server. So, Windows BackSlash('') is not recognized by Linux's Front Slash.

if (empty($formerrors)) {
    $avatar = rand(0, 1000000) . '_' . $avatarname;
    move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);
    $check = checkitem("NovelName", "novel", $Novelname);

This will be:

move_uploaded_file($avatartmp, "upload/novelimage/\" . $avatar);

You can refer to this StackOverflow link related to Windows path issue in Linux Server: Help with windows path - PHP

I hope this helps you, but your question is not quite clear to me.

Upvotes: 1

Related Questions