Rohit sharma
Rohit sharma

Reputation: 43

Move_upload_file() is failed to open stream and Unable to move file

Having some issue occur during uploading a image file in a folder and if file is exist then move it by current upload file... First time it uploaded fine, but during moving it's showing some error

<?php include_once('../includes/config.php');

   if(isset($_POST['add_code']))
   {
       if (($_FILES['photo']['name']!="")){
           // Where the file is going to be stored
           $target_dir = "manufacturer/";
           $file = $_FILES['photo']['name'];
           $path = pathinfo($file);
           $filename = $path['filename'];
           $ext = $path['extension'];
           $temp_name = $_FILES['photo']['tmp_name'];
           $path_filename_ext = $target_dir.$filename.".".$ext;

           // Check if file already exists
           if (file_exists($path_filename_ext)) {
               echo "Sorry, file already exists.";
           }else{
               move_uploaded_file($temp_name,$path_filename_ext);
               echo "Congratulations! File Uploaded Successfully.";
           }
       }           
       $ititle=$_POST['image_title'];
       $c_=mysqli_query($connection,"INSERT INTO `manufacturer`(`mname`, `mimg`) VALUES ('$ititle','$path_filename_ext')");
//$check_user_data=mysqli_fetch_array($check_user_);
       if($c=true){
        echo '1';
       }
   }
?>

---------------------------------------------------Showing This error--------------------------------------------------

Warning: move_uploaded_file(manufacturer/1.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\cabs_admin\action\add_manufacturer_image.php on line 19

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpC119.tmp' to 'manufacturer/1.jpg' in C:\xampp\htdocs\cabs_admin\action\add_manufacturer_image.php on line 19

Upvotes: 0

Views: 6803

Answers (1)

Bhargav Patel
Bhargav Patel

Reputation: 127

You have to verify some points as per below.

1) Ensure your form have enctype="multipart/form-data"

2) Please check your root directory you have to created folder "manufacturer"

Now I expect following files you have

form.php File

<form name="fileUpload" method="post" id="fileUpload" enctype="multipart/form-data" action="success.php">
    <input type="text" name="image_title">
    <input type="file" name="photo">
    <input type="submit" name="add_code" value="Submit">
</form>

success.php File

<?php

if(isset($_POST['add_code'])){
    if (($_FILES['photo']['name']!="")){
        // Where the file is going to be stored
        $target_dir = "manufacturer/";
        $file = $_FILES['photo']['name'];
        $path = pathinfo($file);
        $filename = $path['filename'];
        $ext = $path['extension'];
        $temp_name = $_FILES['photo']['tmp_name'];
        $path_filename_ext = $target_dir.$filename.".".$ext;

        // Check if file already exists
        if (file_exists($path_filename_ext)) {
            echo "Sorry, file already exists.";
        }else{
           move_uploaded_file($temp_name,$path_filename_ext);
           echo "Congratulations! File Uploaded Successfully.";
       }
    }

    $ititle=$_POST['image_title'];
    $c_=mysqli_query($connection,"INSERT INTO `manufacturer`(`mname`, `mimg`) VALUES ('$ititle','$path_filename_ext')");
    //$check_user_data=mysqli_fetch_array($check_user_);
    if($c=true){
        echo '1';
    }
}
?>

Please try with above listed code and i hope it works.

Upvotes: 1

Related Questions