-4

when user upload the image or doc. Image or doc is not getting stored in upload folder but remaining fields are successfully inserted in the database. I want to store it in the upload folder.

  • at least post some code – Kylie Aug 13 '13 at 05:08
  • This issue is impossible to help you with without seeing the code performing the upload. Please edit it into your post. At a wild guess, check the file permissions on the folder being uploaded to. – Bad Wolf Aug 13 '13 at 05:10
  • Several great tutorials exist on this: http://www.w3schools.com/php/php_file_upload.asp – FThompson Aug 13 '13 at 05:10
  • Just like trying to program the time on a VCR, you'll need [**the manual**](http://php.net/manual/en/function.move-uploaded-file.php) – Funk Forty Niner Aug 13 '13 at 05:13
  • show what you have already tried..show some code – chirag ode Aug 13 '13 at 05:21
  • The OP asked [**a similar question**](http://stackoverflow.com/questions/18189812/how-to-show-the-image-in-root-directory-filewampserver), which ended up being "dead in the water". As will this one. *"Some are just not cut out to code."* – Funk Forty Niner Aug 13 '13 at 05:45

2 Answers2

0

Please see

if(isset($_REQUEST['submit'])){  
 $file=$_FILES['myfile']['name'];
 $path="PATH YOUR DIRECTORY";

 if(move_uploaded_file($_FILES['myfile']['tmp_name'],$path.$file))
 {
   echo "FILE UPLOAD SUCCESSFULLY";
 }
 else{
   echo "FAILED";
 }

}

Using enctype in your <form> attribute otherwise your file not uploading in your server.

enctype="multipart/form-data"

HTML Code

<form method="post" enctype="multipart/form-data">
File: <input type="file" name="myfile" />
<input type="submit" name="submit" value="Upload" />
</form>

more info visit

http://www.learnphp.in/mypage_6_File-uploading-with-PHP.html

Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • thank you for reply chinamy sahu.iam trying your code..image is successfully upload its ok..my another doubt when i inserted in the above code in form like
    .image is not uploaded.that is the problem.tell me your mail address i will send my code chinmay shau
    – user2615543 Aug 13 '13 at 08:59
  • chinmay235@gmail.com send me with description what you want. – Chinmay235 Aug 13 '13 at 09:02
  • thank you for quick reply chimamay i had sent my code in your email address chinmay check it. – user2615543 Aug 13 '13 at 09:15
  • Done please check your email – Chinmay235 Aug 13 '13 at 09:35
  • thank you very much chinmay sahu for your quick reply to me..first you are understand my problem..its working iam using file upload code in Rules.php what ever you said its workind dude.. – user2615543 Aug 13 '13 at 10:29
  • This is ok but you are unable to show the error message if you write code in Rules.php page. Better to write to write in Application.php page. Thanks. – Chinmay235 Aug 13 '13 at 12:30
0
public function uploadImage()
{
    //define a maxim size for the uploaded images in Kb
     define ("MAX_SIZE","1000"); 


    //checks if the form has been submitted
    if(isset($_POST['picture'])) 
    {
        //reads the name of the file the user submitted for uploading
        $image=$_FILES['file']['name'];
        if ($_FILES["file"]["error"] > 0)
        {
            $this->errorMsg = $_FILES["file"]["error"] . "<br />";
            $this->errors = 1;
         }

        //if it is not empty

        //get the original name of the file from the clients machine
        $filename = stripslashes($_FILES['file']['name']);

        //get the extension of the file in a lower case format
        $extension = $this->getExtension($filename);

        $extension = strtolower($extension);
        //if it is not a known extension, we 
        //will suppose it is an error and will not  upload the file,  
        //otherwise we will do more tests
        if (($extension != "jpg") && ($extension != "jpeg") &&
                ($extension != "png") && ($extension != "gif")) 
        {
            //print error message
            $this->errorMsg = "You must choose a jpeg, jpg,
             png, or gif file";
            $this->errors=1;
        } else {
        //get the size of the image in bytes
        //$_FILES['image']['tmp_name'] is the temporary filename of the                              file
        //in which the uploaded file was stored on the server
        $size=filesize($_FILES['file']['tmp_name']);

        //Store the extension for listing in session
        $_SESSION["image_ext"] = $extension;

        //compare the size with the maxim size 
        //we defined and print error if bigger
        if ($size > MAX_SIZE*1024)
        {
            $this->errorMsg = "You have exceeded the size limit.";
            $this->errors=1;
        }


        //we will give an unique name, 
                   //for example the time in unix time format
        $image_name= $item_num.'.'.$extension;

        chdir("/home/public_html/");
        try {
        @mkdir($item_num, 0755);
        chmod($item_num, 0755);
        } catch (Exception $e) {

        } 

        //we verify if the image has been uploaded, and print error instead
        $imagePath = $_FILES['file']['tmp_name'];

        //the new name will be containing the full 
                   //path where will be stored (images folder)
        $newname= $item_num."/".$image_name;

        $copied = copy($imagePath, $newname);
        //Now use that directory andmake the image

        if (!$copied) 
        {
        echo "Failure";
        $this->errorMsg = "There was an error uploading your image.";
        $this->errors=1;
        }

    }
    //If no errors registred, print the success message
     if(isset($_POST['picture']) && !$this->errors) 
    {
                      echo "success";
            }
        }
user2128903
  • 57
  • 1
  • 7